How to display ToolTip for disabled Controls

G

Guest

Hi

How can I display a ToolTip for a disabled (Enabled = false) Control
(Button, CheckBox, ...) with C# 2005?

Thanks a lot
andersch
 
L

Linda Liu [MSFT]

Hi Andersch,

When a control is disabled, tooltip will not display on the control. But we
could force the tooltip to show by calling the ToolTip.Show method.

We should call this method to show tooltip when the mouse is hovering over
the control. However, since the control is disabled, all events of this
control won't be fired. So it's no use for us to handle the MouseHover
event of the control. Instead, we could handle the MouseMove event of the
form. In the form's MouseMove event handler, we check if the control
captures the mouse. If yes, we call the ToolTip.Show method.

The following is a sample. It requires you to add a button called button1
and a tooltip called toolTip1 onto the form and set the 'ToolTip on
toolTip1' property of the button1 to a text, e.g 'this is the tooltip for
button1'.

bool IsShown = false;

void Form1_MouseMove(object sender, MouseEventArgs e)
{
Control ctrl = this.GetChildAtPoint(e.Location);

if (ctrl != null)
{
if (ctrl == this.button1 && !IsShown)
{
string tipstring =
this.toolTip1.GetToolTip(this.button1);
this.toolTip1.Show(tipstring, this.button1,
this.button1.Width /2, this.button1.Height / 2);
IsShown = true;
}
}
else
{
this.toolTip1.Hide(this.button1);
IsShown = false;
}
}

Because the MouseMove event is raised for several times when the mouse
stays on the button1, I use a flag 'IsShown' to prevent the tooltip from
showing once again.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Linda

Thank you for your answer. But the code doesn't work for me. I use a
CheckBox in a UserControl. :-(

Do I have to consider something when I use a UserControl?

Thanks and Regards,
andersch
 
L

Linda Liu [MSFT]

Hi Andersch,

If the checkbox is in a usercontrol, you could handle the MouseMove event
of the usercontrol to display the tooltip.

The following is a sample. It requires you to create a usercontrol and add
a checkbox onto the usercontrol.

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(UserControl1_MouseMove);
}
bool IsShown = false;

void UserControl1_MouseMove(object sender, MouseEventArgs e)
{
Control ctrl = this.GetChildAtPoint(e.Location);

if (ctrl != null)
{
if (ctrl == this.checkBox1 && !IsShown)
{
string tipstring =
this.toolTip1.GetToolTip(this.checkBox1);
this.toolTip1.Show(tipstring, this.checkBox1,
this.checkBox1.Width / 2, this.checkBox1.Height / 2);
IsShown = true;
}
}
else
{
this.toolTip1.Hide(this.checkBox1);
IsShown = false;
}
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

Hi Linda

Unfortunately it does not work yet! :-(
What can be still wrong?

Thanks and Regards,
Andersch
 
L

Linda Liu [MSFT]

Hi Andersch,

Have you run my sample program on your machine? Is there any problem in my
sample program?

If my sample program doesn't work on your machine, please feel free to let
me know.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

Hi Linda

Sorry, but I doesn't received your sample! :-(

Can you please send it again?

Thanks and Regards,
andersch
 
L

Linda Liu [MSFT]

Hi Andersch,

To ensure you could receive my sample project, I'd like to send it to you
by email. Could you please tell me your email address?

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Andersch,

I am sorry for that.

You may send me an email to let me know your email address. To get my
actual email address, remove 'online' from my displayed email address.

After I receive your email, I will send you my sample project.

I look forward to your reply ; )


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top