Never underestimate the power of Passion!

Wednesday 8 February 2017

On 23:06 by Vardan Kumar in    No comments
ToolTip c# WinForm

ToolTip for controls 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.

So what exactly a ToolTip does?

It is a clue more precisely an info used to brief what a tool does. This tool is used in association with the mouse cursor usually a pointer or sometimes an I-cursor for certain controls like text box without clicking it. After successfully implementing a tool tip in your GUI(Graphical user Interface) a small rectangular pop-up window must appear next to your control which is being hovered to displaying what this control does.

Now let us see its implementation in c#....

1. Create and initialize an instance of a class called as ToolTip. It is a class under System.Windows.Forms.ToolTip.

ToolTip tt = new ToolTip();

2. Associate ToolTip Text with specified control.

                     //Fetches path of current directory and assign to label

lbl_Path.Text = System.IO.Directory.GetCurrentDirectory();
//SetToolTip(Control name,"Hibt you want to give");
                       tt.SetToolTip(lbl_Path, lbl_Path.Text);

ToolTip C# Windows Form Application on label control
ToolTip on label control(Text greater than Window Form size)

3. You can use same instance(here tt) to provide hit to various other control.

tt.SetToolTip(txt_UserName, txt_UserName.Text);
                      tt.SetToolTip(txt_Host, txt_Host.Text);

Text Box ToolTip windows form application c#
ToolTip on TextBox(Text greater than TextBox size)

If you want to change your ToolTip for a text box as the text in the same changes then register TextChnaged event for the same.

 private void txt_UserName_TextChanged(object sender, EventArgs e)
            tt.SetToolTip(txt_UserName, txt_UserName.Text);

        private void txt_Host_TextChanged(object sender, EventArgs e)
            tt.SetToolTip(txt_Host, txt_Host.Text);

Generally you might want to associate ToolTip with control under Form1_Load.

One of the major application of ToolTip apart from displaying what a control does  is displaying the text on control for which our text associated with that control is greater than are window size.


Post a Comment