📌  相关文章
📜  如何在C#中高亮显示MaskedTextBox中的文本?

📅  最后修改于: 2021-05-29 23:54:44             🧑  作者: Mango

在C#中,MaskedTextBox控件为日期,电话号码等表单上的用户输入提供了一个验证过程。换句话说,它用于提供一个区分正确和不正确用户输入的掩码。在MaskedTextBox控件中,当MaskedTextBox控件失去焦点时,可以使用MaskedTextBox控件提供的HideSelection属性突出显示存在于MaskedTextBox控件中的选定文本。
如果此属性的值为true,则当控件失去焦点时,MaskedTextBox中存在的选定文本不会突出显示。并且如果此属性的值为false,则当控件失去焦点时,MaskedTextBox中存在的所选文本将突出显示。此属性的默认值为true。您可以通过两种不同的方式设置此属性:

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

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:接下来,将MaskedTextBox控件从窗体上的工具箱中拖放,如下图所示:

  • 步骤3:拖放之后,您将转到MaskedTextBox的属性,并设置MaskedTextBox控件的HideSelection属性的值,如下图所示:

    输出:

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

public bool HideSelection { get; set; }

此属性的值是System.Boolean类型,为true或false。以下步骤显示如何动态设置MaskedTextBox控件的HideSelection属性的值:

  • 步骤1:使用MaskedTextBox类提供的MaskedTextBox()构造函数创建MaskedTextBox。
    // Creating a MaskedTextBox
    MaskedTextBox m = new MaskedTextBox();
    
  • 步骤2:创建MaskedTextBox之后,设置MaskedTextBox类提供的MaskedTextBox的HideSelection属性。
    // Setting the HideSelection property
    m.Mask = "000000000";
    
  • 步骤3:最后,使用以下语句将此MaskedTextBox控件添加到表单中:
    // Adding MaskedTextBox control on the form
    this.Controls.Add(m);
    

    例子:

    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 WindowsFormsApp39 {
      
    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(413, 98);
            l1.Size = new Size(176, 20);
            l1.Text = " Example";
            l1.Font = new Font("Bell MT", 12);
      
            // Adding label on the form
            this.Controls.Add(l1);
      
            // Creating and setting the 
            // properties of the Label
            Label l2 = new Label();
            l2.Location = new Point(242, 135);
            l2.Size = new Size(126, 20);
            l2.Text = "Phone number:";
            l2.Font = new Font("Bell MT", 12);
      
            // Adding label on the form
            this.Controls.Add(l2);
      
            // Creating and setting the 
            // properties of the MaskedTextBox
            MaskedTextBox m = new MaskedTextBox();
            m.Location = new Point(374, 137);
            m.Mask = "000000000";
            m.Size = new Size(176, 20);
            m.Name = "MyBox";
            m.BorderStyle = BorderStyle.Fixed3D;
            m.HideSelection = true;
            m.Font = new Font("Bell MT", 18);
      
            // Adding MaskedTextBox 
            // control on the form
            this.Controls.Add(m);
        }
    }
    }
    

    输出: