TextBox validating e.cancel=true does not cancel button event
-
Hi all, In my .NET 1.1 Windows application I am using the validating event to check the correct field contents of a TextBox. When the contents was wrong I used e.cancel=true; and set the Focus on the TextBox again. I am leaving the TextBox by a button click event. I would expect that the validating event comes before a button click event occurs, but that's wrong. The button click will not be canceled. What must be done to cancel the button click when the e.cancel event of the TextBox becomes true ? Or can I check in my button click function if a cancel has happend anywhere ? Thanks in advance Frank
-
Hi all, In my .NET 1.1 Windows application I am using the validating event to check the correct field contents of a TextBox. When the contents was wrong I used e.cancel=true; and set the Focus on the TextBox again. I am leaving the TextBox by a button click event. I would expect that the validating event comes before a button click event occurs, but that's wrong. The button click will not be canceled. What must be done to cancel the button click when the e.cancel event of the TextBox becomes true ? Or can I check in my button click function if a cancel has happend anywhere ? Thanks in advance Frank
-
Hi all, In my .NET 1.1 Windows application I am using the validating event to check the correct field contents of a TextBox. When the contents was wrong I used e.cancel=true; and set the Focus on the TextBox again. I am leaving the TextBox by a button click event. I would expect that the validating event comes before a button click event occurs, but that's wrong. The button click will not be canceled. What must be done to cancel the button click when the e.cancel event of the TextBox becomes true ? Or can I check in my button click function if a cancel has happend anywhere ? Thanks in advance Frank
Just tried this and it works okay, add a textbox and button to a form:
private void textBox1_Validating(object sender, CancelEventArgs e) { if (string.Compare(textBox1.Text, "foo") != 0) { e.Cancel = true; } } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("bar!"); }
If text box hasn't got foo then there is no bar. Doesn't matter if you tab or click away, the focus stays with the unvalidated control. Hope this helps, -
Just tried this and it works okay, add a textbox and button to a form:
private void textBox1_Validating(object sender, CancelEventArgs e) { if (string.Compare(textBox1.Text, "foo") != 0) { e.Cancel = true; } } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("bar!"); }
If text box hasn't got foo then there is no bar. Doesn't matter if you tab or click away, the focus stays with the unvalidated control. Hope this helps,Thank you for your answer, this is what I would appreciate to have. But in my Application the MessageBox was shown !!! This is what i don't understand. the Button is on a Tab of the tabcontrol and the TextBox is on a panel of the same tab, don't know if this metters ? An idea ? Thanks frank
-
Thank you for your answer, this is what I would appreciate to have. But in my Application the MessageBox was shown !!! This is what i don't understand. the Button is on a Tab of the tabcontrol and the TextBox is on a panel of the same tab, don't know if this metters ? An idea ? Thanks frank
I can only think that your event is not bound to the text control. If the validating event is cancelled then the focus will stay on the text control. In the textbox's properties check the events and at the bottom look at Validating it should be (in the example) textBbox1_Validating