C#中的UserControl TextBox验证

时间:2013-03-07 16:56:05

标签: c# winforms user-controls textbox dialog

我有一个TextBox UserControl。我为MaximumLength的Textbox创建了一个动态属性。

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...,
    }

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

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

现在我想验证用户何时更改该属性中的值...,

I Want that Dialog Box

1 个答案:

答案 0 :(得分:2)

实现一个自定义setter,检查该值是否有效。

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

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

编辑:所以实现一个getter。