Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

Implementing Dynamic ToolTips: Preventing SetToolTip stack overflow

Rate me:
Please Sign up or sign in to vote.
4.17/5 (3 votes)
17 Feb 2010CPOL 9.8K   2  
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:
C#
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);
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other NOAA
United States United States
I am a commissioned officer with the NOAA Commissioned Corps.

Currently I am a GIS Analyst with the National Marine Fisheries Service. I have a Master's in Environmental Science and code more as a means to an end.

Comments and Discussions

 
-- There are no messages in this forum --