简体   繁体   中英

UserControl TextBox Validation in C#

I have a TextBox UserControl. I create a Dynamic Property for the Textbox for MaximumLength.

public int MaximumLength { get; set; }

    private void txtLocl_KeyPress(object sender, KeyPressEventArgs e)
    {
        txtLocl.MaxLength = MaximumLength;//txtLocl is a Usercontrol Textbox..,
        //txtLocl maxLength should be given by the user in WindowsForm
        //that should be come to here...,
    }

I show you the Image of the UserControl Properties in Windows Form

我向您展示Windows窗体中UserControl属性的图像。

Now i want to verify when user change the value in that property...,

我想要那个对话框

Implement a custom setter that checks if the value is valid.

public int MaximumLength
{
  get
  {
    return this.maximumLength;
  }

  set
  {
    if(value <= 4)
    {
      MessageBox.Show("Value is too small.");
    }
    else this.maximumLength = value;
  }
}

Edit: So implement a getter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM