sree aneev
sree aneev

Reputation: 139

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

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

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

I Want that Dialog Box

Upvotes: 0

Views: 1069

Answers (1)

MrFox
MrFox

Reputation: 5106

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.

Upvotes: 2

Related Questions