2009-05-21 3 views
5

저는 C# 및 WinForms에 익숙하지 않으므로이 항목은 초보자 용 질문입니다.WinForms에서 트랙 막대의 값을 보여주는 툴팁을 표시하려면 어떻게해야합니까?

막대 바를 드래그 할 때 막대의 현재 값을 표시하는 툴바 컨트롤에 툴팁을 추가하려고합니다. 나는 도구 설명 개체를 인스턴스화 다음과 같은 핸들러 코드를 시도했지만 어떤 툴팁이 표시되지 않습니다했습니다

private void trackBar1_Scroll(object sender, EventArgs e) 
{ 
    toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString()); 
} 
당신이 toolTip1 클래스를 초기화 않았다 어떻게

답변

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

? 툴팁 텍스트를 설정하는 방법은 괜찮아 보입니다. 컴포넌트가 작업을하기 전에 일부 일반적인 속성을 설정했을 수도 있습니다.

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"으로 표시되고 도구 설명은 영구적으로 사라집니다. 너무 빨리 응답 해 주셔서 감사합니다.하지만 게시 한 행을 추가해도 아무런 차이가없는 것으로 보입니다. –