Monday, 6 February 2017
Text Box Hint in c# Windows Form Application
While developing a windows form application, most of us come around with designing its GUI(Graphical User Interface). Since the inclusive toolbox in visual studio inhibits certain features in a windows form application, hence we have to build our own logic for the same. So what's there in designing which makes us do this work as we can just develop an application to just do our intended work. Wish it was so simple but it doesn't work like this when it comes to the real world. In the real world Appearance leads to Pursuance.
Logic
So for this purpose we'll use text box enter and text box leave events which are raised whenever we enter and leave the text box respectively. Got the strike, if yes try it yousrself first before moving forward.
The logic is somewhat like initially under "Form1" constructor method(by default) after initializing components we'll give our desired text box hint using "textbox.text",turn its color to "gray text" using "textbox.forecolor" and register "textbox_enter" as well as "textbox_leave" events as shown is image below.
Text Box Hint |
.
We are almost there:
Just the definition of textbox_leave and and textbox_enter methods are to be included.
textbox_enter:
We'll check if the text box text is "Our desired hint" like for instance here it is "Username",if yes then make the text box empty and change the text color to windows.text. If it's a password text box then make "UseSystemPasswordChar" true.
textbox_leave:
We'll check if the length of text in text box is 0, if yes we'll change the text to "Our desired hint" and make its color as gray text. If it is a password text box then make "UseSystemPasswordChar" false.
Text Box Hint |
Source code:
public Form1()
{
InitializeComponent();
txt_UserName.ForeColor = SystemColors.GrayText;
txt_UserName.Text = "Username";
this.txt_UserName.Leave += new System.EventHandler(this.txt_UserName_Leave);
this.txt_UserName.Enter += new System.EventHandler(this.txt_UserName_Enter);
txt_Password.ForeColor = SystemColors.GrayText;
txt_Password.Text = "Password";
}
private void txt_UserName_Leave(object sender, EventArgs e)
{
if (txt_UserName.Text.Length == 0)
{
txt_UserName.Text = "Username";
txt_UserName.ForeColor = SystemColors.GrayText;
}
}
private void txt_UserName_Enter(object sender, EventArgs e)
{
if (txt_UserName.Text == "Username")
{
txt_UserName.Text = "";
txt_UserName.ForeColor = SystemColors.WindowText;
}
}
private void txt_Password_Leave(object sender, EventArgs e)
{
if (txt_Password.Text.Length == 0)
{
txt_Password.Text = "Password";
txt_Password.UseSystemPasswordChar = false;
txt_Password.ForeColor = SystemColors.GrayText;
}
}
private void txt_Password_Enter(object sender, EventArgs e)
{
if (txt_Password.Text == "Password")
{
txt_Password.Text = "";
txt_Password.UseSystemPasswordChar = true;
txt_Password.ForeColor = SystemColors.WindowText;
}
}
This is how it will look.......
Password Text Box empty |
Username Text Box Empty |
Both TextBox Filled |
Sometimes the text in the text box exceeds its size so in order to make GUI more user interactive we should give our textbox a tool tip that would display the text box's text whenever user hovers onto it.
Subscribe to:
Post Comments (Atom)
Search
Popular Posts
-
Troubleshooting Cisco VPN client Before starting troubleshooting, Let us see what VPN is and what it requires to perform its intended f...
-
Evolution-Mobile Phones With the development of portable technology,wireless communication has so evolved that (According to the announce...
-
File Versioning C# File versioning, saving file with unique file name in c# File versioning allows a user to have several versions of ...
-
Text Box Hint in c# Windows Form Application Text Box Hint in c# Windows Form Application While developing a windows form applicat...
-
Unable to set the Freeze Panes property of Window Class C# It is generally easy to resolve the compile time errors because the reason fo...
41F3F6AE3A
ReplyDeleteAnadoluslot Güncel Adres
Anadoluslot Yeni Adres
Anadoluslot Giriş
Anadoluslot
Trwin
Trwin
Trwin
Trwin Giriş Adresi
Trwin Güncel Adres