📌  相关文章
📜  如何在C#中的MaskedTextBox中设置文本?

📅  最后修改于: 2021-05-29 21:28:35             🧑  作者: Mango

在C#中,MaskedTextBox控件为日期,电话号码等表单上的用户输入提供验证过程。换句话说,它用于提供可区分正确和不正确用户输入的掩码。在MaskedTextBox控件中,可以使用将显示给用户的Text属性设置MaskedTextBox中的文本。它是MaskedTextBox类的默认绑定属性。从此属性检索的字符串使用诸如Mask,TextMaskFormat等格式属性进行格式化。您可以通过两种不同的方式设置此属性:

1.设计时:这是在MaskedTextBox中设置文本的最简单方法,如以下步骤所示:

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

  • 步骤3:拖放之后,您将转到MaskedTextBox的属性,并在MaskedTextBox中设置文本,如下图所示:

    输出:

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

public override string Text { get; set; }

此属性的值是System.String类型,将显示给用户。此属性的默认值为空。以下步骤显示如何动态设置MaskedTextBox中的文本:

  • 步骤1:使用MaskedTextBox类提供的MaskedTextBox()构造函数创建MaskedTextBox。
    // Creating a MaskedTextBox
    MaskedTextBox m = new MaskedTextBox();
    
  • 步骤2:创建MaskedTextBox之后,设置MaskedTextBox类提供的MaskedTextBox的Text属性。
    // Setting the text
    m.Text = "GeeksforGeeks";
    
  • 步骤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 WindowsFormsApp38 {
      
    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 MaskedTextBox
            MaskedTextBox m = new MaskedTextBox();
            m.Location = new Point(374, 137);
            m.Text = "GeeksforGeeks";
            m.TextAlign = HorizontalAlignment.Right;
            m.Size = new Size(176, 20);
            m.Name = "MyBox";
            m.BorderStyle = BorderStyle.Fixed3D;
            m.BackColor = Color.LightBlue;
            m.ForeColor = Color.HotPink;
            m.Font = new Font("Bell MT", 18);
      
            // Adding MaskedTextBox
            // control on the form
            this.Controls.Add(m);
        }
    }
    }
    

    输出: