代码之家  ›  专栏  ›  技术社区  ›  Adam Pierce

如何在WinForms中显示显示轨迹栏值的工具提示

  •  5
  • Adam Pierce  · 技术社区  · 16 年前

    我是C#和WinForms的新手,所以请原谅,这是一个有点生疏的问题。

    我正在尝试向我的轨迹栏控件添加工具提示,当您拖动它时,它会显示该栏的当前值。我实例化了一个ToolTip对象,并尝试了以下处理程序代码,但它没有显示任何ToolTip:

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
       toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString());
    }
    
    2 回复  |  直到 16 年前
        1
  •  16
  •   Eoin Campbell    16 年前

    Adam我刚刚实现了一个非常简单的版本,它完全按照预期工作。。。

    下面是用于比较的init代码

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

    当我移动股票代码到每一个额外的增量。。。

        2
  •  2
  •   Chris Richner    16 年前

    您是如何初始化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;