I have a very simple form which is opened with ShowDialog. It has a
TextBox that should not accept blank values and a Close button. The
CausesValidation property of the Close button is set to False; textbox
validation is fired using the Validate method (I know this seems a bit
silly, but it would take me too long to explain why I need to do it
like this).
When the textbox is empty and I click the Close button the first time,
the Close event is cancelled, which is fine. However, when I click
the Close button again, nothing happens : the Click event is not event
raised. Only when I click the Close button again, the Click event is
raised and the Close event is cancelled again. Why do I need to click
twice???
I use the following 3 procedures :
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Not Me.Validate Then e.Cancel = True
End Sub
Private Sub TextBox1_Validating(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs) Handles
TextBox1.Validating
If Me.TextBox1.Text = "" Then
MsgBox("Empty not allowed...")
e.Cancel = True
End If
End Sub
Any help would be greately appreciated!
Pascal
Nothing's wrong with your code. The problem is in the framework. The whole
sequence of Enter-Leave-Validating- Validated, etc is bad implemented in the
framework.
When cancel the vaidation the active(focused) control is set by the
framework to be the control that failed the validation (at the moment of
clicking the active is the button) It seems like after seting the active
control in this case the inernal state of the control is not set correctly.
The control resets its state when you click on the button and it becomes
active, but it doesn't fire the event. So I came up with a workaround, which
I tested and it works.
(My workaround is in c#, but I don't thing you'll have any problems to
translate it)
Form Closing event:
private void MyDlg_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(!this.Validate())
{
this.ActiveControl = this.ActiveControl;
e.Cancel = true;
}
}
The line
this.ActiveControl = this.ActiveControl;
doesn't do anything meaningful, but it resets the control internal state
--
HTH
B\rgds
100 [C# MVP]
Thanks for your quick reply. Unfortunately your workaround does not
seem to solve my problem. I added the code Me.ActiveControl =
Me.ActiveControl in the form Close event, but I still need to press
the Close button twice! With the explanation I got from you I tried a
couple of variants : Me.ActiveControl.Focus, Me.ActiveControl.Select,
... but none seem to help.
Any other ideas would be (even more) appreciated!!
Regards
Pascal
"Stoitcho Goutsev \(100\) [C# MVP]" <1...@100.com> wrote in message news:<#SAPVWSE...@TK2MSFTNGP10.phx.gbl>...
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(this.textBox1.Text == "")
{
MessageBox.Show("Empty not allowed...");
this.ActiveControl = (Control)sender;
e.Cancel = true;
}
}
and the line that resets the control is
this.ActiveControl = (Control)sender;
That should help you. If not, I'll keep trying to find solution for you.
--
HTH
B\rgds
100 [C# MVP]
"Pascal" <Pascal...@mbsbelgium.com> wrote in message
news:7705a51d.04032...@posting.google.com...
Works like a charm!!! Considering that I searched for about 2 days on
this issue, the solution is surprisingly simple.
Thanks very much for your help!
Pascal
"Stoitcho Goutsev \(100\) [C# MVP]" <1...@100.com> wrote in message news:<#2i8JpaE...@TK2MSFTNGP09.phx.gbl>...