Implementing Dynamic ToolTips: Preventing SetToolTip stack overflow






4.17/5 (3 votes)
First, why would you want to change the ToolTip text during the PopUp? Why not hook the MouseHover of the control that you want to change the ToolTip? Dynamically setting the ToolTip does not require hooking the PopUp event.How about: public partial class Form1 : Form { ...
First, why would you want to change the
ToolTip
text during the PopUp
? Why not hook the MouseHover
of the control that you want to change the ToolTip
? Dynamically setting the ToolTip
does not require hooking the PopUp
event.
How about:
public partial class Form1 : Form
{
Random rnd;
ToolTip toolTip1;
String[] tipText;
public Form1()
{
InitializeComponent();
InitializeToolTips();
tipText = File.ReadAllLines(@"..\..\Form1.cs");
rnd = new Random();
}
private void InitializeToolTips()
{
toolTip1 = new ToolTip();
toolTip1.SetToolTip(button1, "Fixed text");
toolTip1.SetToolTip(button2, "Variable text");
button1.MouseHover += button_MouseHover;
}
private String NewTipText()
{
Int32 idx = rnd.Next(0, tipText.Length);
return tipText[idx];
}
private void button_MouseHover(object sender, System.EventArgs e)
{
String proposed = NewTipText();
if (!String.IsNullOrEmpty(proposed)) {
toolTip1.SetToolTip((Control)sender, proposed);
}
}
}