📜  如何在C#中制作气球工具提示窗口?

📅  最后修改于: 2021-05-30 00:24:13             🧑  作者: Mango

在Windows窗体中,工具提示表示一个很小的弹出框,当您将指针或光标放在控件上时,该框就会出现,该控件的目的是提供有关Windows窗体中存在的控件的简短说明。在工具提示中,可以使用IsBalloon属性来更改工具提示窗口的形状。
借助此属性,可以将“工具提示”窗口从标准矩形窗口更改为气球窗口。您可以通过两种不同的方式设置此属性:

1.设计时:这是设置IsBalloon属性值的最简单方法,如以下步骤所示:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:将工具提示从工具箱中拖放到窗体上。当您将此ToolTip拖放到窗体上时,它将自动添加到当前窗口中存在的每个控件的属性(在ToolTip1上命名为ToolTip)。

  • 步骤3:拖放之后,您将转到工具提示的属性并设置IsBalloon属性的值。

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式设置ToolTip的IsBalloon属性:

public bool IsBalloon { get; set; }

在这里,此属性的值是System.Boolean类型。如果此属性的值设置为true,则“工具提示”窗口属于气球状窗口。并且如果此属性的值设置为false,则“工具提示”窗口属于标准矩形窗口。此属性的默认值为false。

以下步骤显示如何动态设置工具提示的IsBalloon属性:

  • 第1步:使用ToolTip类提供的ToolTip()构造函数创建ToolTip。
    // Creating a ToolTip
    ToolTip t = new ToolTip();
    
  • 步骤2:创建工具提示后,设置由工具提示类提供的工具提示的IsBalloon属性。
    // Setting the IsBalloon property
    t.IsBalloon = true;
    
  • 步骤3:最后使用SetToolTip()方法将此ToolTip添加到控件中。此方法包含控件名称和要在“工具提示”框中显示的文本。
    t.SetToolTip(box1, "Name should start with Capital letter");

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp34 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
      
            // Creating and setting the 
            // properties of the Label
            Label l1 = new Label();
            l1.Location = new Point(140, 122);
            l1.Text = "Name";
      
            // Adding this Label control to the form
            this.Controls.Add(l1);
      
            // Creating and setting the 
            // properties of the TextBox
            TextBox box1 = new TextBox();
            box1.Location = new Point(248, 119);
            box1.BorderStyle = BorderStyle.FixedSingle;
      
            // Adding this TextBox
            // control to the form
            this.Controls.Add(box1);
      
            // Creating and setting 
            // the properties of Label
            Label l2 = new Label();
            l2.Location = new Point(140, 152);
            l2.Text = "Password";
      
            // Adding this Label 
            // control to the form
            this.Controls.Add(l2);
      
            // Creating and setting the
            // properties of the TextBox
            TextBox box2 = new TextBox();
            box2.Location = new Point(248, 145);
            box2.BorderStyle = BorderStyle.FixedSingle;
      
            // Adding this TextBox 
            // control to the form
            this.Controls.Add(box2);
      
            // Creating and setting the 
            // properties of the ToolTip
            ToolTip t = new ToolTip();
            t.Active = true;
            t.AutoPopDelay = 4000;
            t.InitialDelay = 600;
            t.IsBalloon = true;
            t.SetToolTip(box1, "Name should start with Capital letter");
            t.SetToolTip(box2, "Password should be greater than 8 words");
        }
    }
    }
    

    输出: