winforms 验证器新文本框C#

mm9b1k5b  于 2022-11-16  发布在  C#
关注(0)|答案(3)|浏览(194)

如何从textbox类创建验证器或创建新的textbox,而不是从工具箱中拖动textbox

private void Form1_Load(object sender, EventArgs e)
{
    createTextpass();
    // Creating and setting the properties of TextBox1
    TextBox textboxUsername = new TextBox();
    textboxUsername.Location = new Point(420, 50);         
    textboxUsername.Size = new Size (300,30);
    textboxUsername.Name = "text_user";
    this.Controls.Add(textboxUsername);

    TextBox textboxPassword = new TextBox();
    textboxPassword.Location = new Point(420, 80);
    textboxPassword.Size = new Size(300, 30);
    textboxPassword.Name = "text_pass";
    this.Controls.Add(textboxPassword);

    TextBox textboxMail = new TextBox();
    textboxMail.Location = new Point(420, 110);
    textboxMail.Size = new Size(300, 30);
    textboxMail.Name = "text_mail";
    this.Controls.Add(textboxMail);
}
rpppsulh

rpppsulh1#

添加文本框宽度和高度

w8rqjzmb

w8rqjzmb2#

如果我没理解错的话,你想创建一个文本框,并检查文本框中输入的文本是否有效。例如,下面的代码可以用于textboxUsername

public TextBox textboxUsername;
private void Form1_Load(object sender, EventArgs e)
{
    createTextpass();
    // Creating and setting the properties of TextBox1
    textboxUsername = new TextBox();
    textboxUsername.Location = new Point(420, 50);
    textboxUsername.Size = new Size(300, 30);
    textboxUsername.Name = "text_user";            
    this.Controls.Add(textboxUsername);
    textboxUsername.TextChanged += new EventHandler(usernameTextbox_TextChanged);

    ....
}

    
//If textboxUsername contains the # character, an alarm will be displayed
protected void usernameTextbox_TextChanged(object sender, EventArgs e)
{
   if (textboxUsername.Text.Contains('#'))
      MessageBox.Show("inavlid char in userName");
}
lfapxunr

lfapxunr3#

首先创建此函数:

private void Calltextbox()
{
    TextBox txtPass = new TextBox();
    txtPass.Location = new Point(80, 60);
    txtPass.AutoSize = true;
    txtPass.Validating += new CancelEventHandler(txtPass_Validating);
    txtPass.Name = "txtPass";
    Controls.Add(txtPass);
}

完成,未找到调用txtPass txtPass_Validating函数

相关问题