Click here to Skip to main content
15,300,827 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick
Posted 17 Feb 2010

Tagged as

Stats

9.4K views
2 bookmarked

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
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)

Share

About the Author

William Winner
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 --