Never underestimate the power of Passion!

Monday, 6 February 2017

On 20:57 by Vardan Kumar in    No comments
Text Box Hint in c# Windows Form Application

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 C# Windows form
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 C# Windows form Application
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.......

Text Box Hint C#
Both Text box Empty


Text Box Hint C# Windows Form Application
Password Text Box empty


Text Box Hint
Username Text Box Empty


Text Box design C# Windows Form Application
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.








0 comments:

Post a comment