2009-05-21 84 views
5

我是C#和WinForms的新手,請原諒我這是一個新手問題。如何在WinForms中顯示一個顯示跟蹤欄值的工具提示

我想添加一個工具提示到我的TrackBar控件,它顯示了當你拖動它的酒吧的價值。我實例化一個工具提示對象,並嘗試了以下處理程序代碼,但它不顯示任何提示:

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

回答

12

亞當我剛剛實施的這是一個非常簡單的版本,它的工作原理完全如預期...

下面是比較初始化代碼

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

    } 

它可以工作,因爲我把股票移動到每個額外的增量...

+0

Aaargh!你是對的,它確實有效。我在代碼的其他地方發現了一個錯誤,它阻止了trackbar滾動處理程序被調用。在發佈到堆棧溢出之前,我應該更仔細地檢查這些事情。 雖然這是一個很好的代碼示例,所以我會將其作爲公認的答案。 – 2009-05-21 11:39:24

1

你是如何初始化toolTip1類?您設置工具提示文本的方式看起來不錯,也許您在組件執行作業之前已經設置了一些常規屬性?

MSDN說

// 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; 
+0

我剛用toolTi初始化它p1.SetToolTip(trackBar1,「0」);如果您只是將鼠標懸停在滑塊上,則工具提示會顯示「0」,但只要您移動滑塊,工具提示就會永久消失。感謝您的回覆如此之快,但添加您發佈的帖子似乎沒有任何區別。 Aarr! – 2009-05-21 11:23:46