Program Tip

C # Winforms의 레이블에 힌트 또는 도구 설명을 추가하려면 어떻게해야합니까?

programtip 2020. 10. 15. 21:28
반응형

C # Winforms의 레이블에 힌트 또는 도구 설명을 추가하려면 어떻게해야합니까?


이 보인다 Label전혀 없다 Hint거나 ToolTip또는 Hovertext속성을. 그렇다면 마우스 Label가에 접근 할 때 힌트, 툴팁 또는 호버 텍스트를 표시하는 데 선호되는 방법은 무엇 입니까?


ToolTip먼저 폼에 컨트롤 을 추가 해야합니다. 그런 다음 다른 컨트롤에 표시 할 텍스트를 설정할 수 있습니다.

다음 ToolTip은 이름이 지정된 컨트롤을 추가 한 후 디자이너를 보여주는 스크린 샷 입니다 toolTip1.

여기에 이미지 설명 입력


yourToolTip = new ToolTip();
//The below are optional, of course,

yourToolTip.ToolTipIcon = ToolTipIcon.Info;
yourToolTip.IsBalloon = true;
yourToolTip.ShowAlways = true;

yourToolTip.SetToolTip(lblYourLabel,"Oooh, you put your mouse over me.");

System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip( Label1, "Label for Label1");

또 다른 방법입니다.

Label lbl = new Label();
new ToolTip().SetToolTip(lbl, "tooltip text here");

내 아이디어를 공유하기 위해 ...

Label 클래스를 상속하기 위해 사용자 지정 클래스를 만들었습니다. Tooltip 클래스와 공용 속성 TooltipText로 할당 된 전용 변수를 추가했습니다. 그런 다음 MouseEnter 대리자 메서드를 제공했습니다. 이것은 여러 Label 컨트롤로 작업하는 쉬운 방법이며 각 Label 컨트롤에 대해 도구 설명 컨트롤을 할당하는 것에 대해 걱정할 필요가 없습니다.

    public partial class ucLabel : Label
    {
        private ToolTip _tt = new ToolTip();

        public string TooltipText { get; set; }

        public ucLabel() : base() {
            _tt.AutoPopDelay = 1500;
            _tt.InitialDelay = 400;
//            _tt.IsBalloon = true;
            _tt.UseAnimation = true;
            _tt.UseFading = true;
            _tt.Active = true;
            this.MouseEnter += new EventHandler(this.ucLabel_MouseEnter);
        }

        private void ucLabel_MouseEnter(object sender, EventArgs ea)
        {
            if (!string.IsNullOrEmpty(this.TooltipText))
            {
                _tt.SetToolTip(this, this.TooltipText);
                _tt.Show(this.TooltipText, this.Parent);
            }
        }
    }

폼 또는 사용자 컨트롤의 InitializeComponent 메서드 (디자이너 코드)에서 Label 컨트롤을 사용자 지정 클래스에 다시 할당합니다.

this.lblMyLabel = new ucLabel();

또한 디자이너 코드에서 개인 변수 참조를 변경하십시오.

private ucLabel lblMyLabel;

참고 URL : https://stackoverflow.com/questions/9776077/how-can-i-add-a-hint-or-tooltip-to-a-label-in-c-sharp-winforms

반응형