views:

1056

answers:

2

I'm new to C# and WinForms so please excuse me is this is a bit of a newbie question.

I'm trying to add a tooltip to my TrackBar control which shows the current value of the bar as you drag it. I've instantiated a ToolTip object and tried the following handler code but it doesn't show any tooltip:

private void trackBar1_Scroll(object sender, EventArgs e)
{
   toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString());
}
+1  A: 

How did you initialise the toolTip1 class? The way you set the tool tip text looks ok, maybe you have so set some general properties before the component does the job?

MSDN says

// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();

// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;
Chris Richner
I just initialized it with toolTip1.SetToolTip(trackBar1, "0"); The tooltip shows "0" if you just hover over the slider but as soon as you move the slider, the tooltip disappears permanently. Thanks for responding so quickly but adding the lines you posted doesn't seem to make any difference.
Adam Pierce
+3  A: 

Adam I've just implemented a very simple version of this and it works exactly as expected...

Here's the init code for comparison

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
        this.trackBar1 = new System.Windows.Forms.TrackBar();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
        this.SuspendLayout();
        // 
        // trackBar1
        // 
        this.trackBar1.Location = new System.Drawing.Point(12, 166);
        this.trackBar1.Name = "trackBar1";
        this.trackBar1.Size = new System.Drawing.Size(268, 42);
        this.trackBar1.TabIndex = 1;
        this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.Add(this.trackBar1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString());

    }

And it works as I move the ticker to each additional increment...

Eoin Campbell
Aaargh! You are right Eoin, it does work. I found a bug elsewhere in my code which was preventing the trackbar scroll handler from being called. I should check these things more carefully before posting to Stack Overflow.This is a good code example though, so I'll make it the accepted answer.
Adam Pierce