2009-05-21 12 views
5

私はC#とWinFormsの新機能ですので、これは初心者の質問です。WinFormsのトラックバーの値を示すツールチップを表示するには

ドラッグバーの現在の値を表示するトラックバーコントロールにツールチップを追加しようとしています。私はToolTipオブジェクトをインスタンス化し、次のハンドラのコードを試してみましたが、それは任意のツールチップを表示しませんしました:

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!あなたは正しいEoinです、それは動作します。トラックバーのスクロールハンドラが呼び出されないようにしていたコードのどこかにバグがありました。 Stack Overflowに投稿する前に、これらのことをより慎重にチェックする必要があります。 これは良いコード例ですが、私はそれを受け入れられる答えにします。 –

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"が表示されます。すぐに応答していただきありがとうございますが、あなたが投稿した行を追加しても差はありません。 –

関連する問題