C# 如何在 WinForms 中显示显示轨迹栏值的工具提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/892369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I display a tooltip showing the value of a trackbar in WinForms
提问by Adam Pierce
I'm new to C# and WinForms so please excuse me is this is a bit of a newbie question.
我是 C# 和 WinForms 的新手,所以请原谅我这是一个新手问题。
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:
我正在尝试向我的 TrackBar 控件添加一个工具提示,它会在您拖动时显示该栏的当前值。我已经实例化了一个 ToolTip 对象并尝试了以下处理程序代码,但它没有显示任何工具提示:
private void trackBar1_Scroll(object sender, EventArgs e)
{
toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString());
}
采纳答案by Eoin Campbell
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...
当我将自动收报机移动到每个额外的增量时,它就起作用了......
回答by Chris Richner
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?
你是如何初始化 toolTip1 类的?您设置工具提示文本的方式看起来不错,也许您在组件完成工作之前设置了一些常规属性?
MSDN says
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;