senzacionale
senzacionale

Reputation: 20916

CustomControl textBox Validating event and ComboBox

My CustomControl is created with TextBox and ComboBox. And i want to use Validating event for this control. But if i use innerTextBox.Validating this means that will work for TetBox which is ok. But i do not want that this event will fire when i will click on ComboBox which is also part of this UserControl. I want that this UC will be as one. So i can click on TextBox and Combobox and no event will fire becouse they are one together...

innerTextBox is TextBox

innereComboBox is ComboBox

this is my code code event for Validating. What to do that event will not fire when i click on ComboBox?

public new event System.ComponentModel.CancelEventHandler Validating
        {
            add
            {
                innerTextBox.Validating += value;
            }

            remove { innerTextBox.Validating -= value; }
        }

Hope you understand my problem.

Upvotes: 0

Views: 1127

Answers (2)

LarsTech
LarsTech

Reputation: 81620

I think you have to do this yourself. Turn off the CausesValidation property for your inner controls so they DON'T fire, and then run your validating code for the UserControl:

public UserControl1() {
  InitializeComponent();
  innerTextBox.CausesValidation = false;
  innerComboBox.CausesValidation = false;
}

For example, this control requires a non-empty TextBox and a selected item from the ComboBox:

protected override void OnValidating(CancelEventArgs e) {
  if (innerTextBox.Text == string.Empty)
    e.Cancel = true;
  else if (innerComboBox.SelectedIndex == -1)
    e.Cancel = true;

  base.OnValidating(e);
}

Upvotes: 1

Marco
Marco

Reputation: 57583

Did you try adding combobox to validating event?

public new event System.ComponentModel.CancelEventHandler Validating
{
    add
    {
        innerTextBox.Validating += value;
        innerComboBox.Validating += value;
    }

    remove 
    { 
        innerTextBox.Validating -= value; }
        innerComboBox.Validating -= value; }
    }
}

Upvotes: 1

Related Questions