Please help : Click event not raised when clicking button!!!

P

Pascal

Hi,

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
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi 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
 
P

Pascal

Hi Stoitcho,

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\) said:
Hi 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]
Hi,

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!
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Pascal,
Sorry about the wrong solution I gave you. That was the last post I made
yesturday and I missed to notice that this is not the line that solves the
problem (how I said it looks meaningfull and it is :)). I have left one
line in the textbox *Validating* event that actually does the trick. So here
is my validate event handler

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 said:
Hi Stoitcho,

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]" <[email protected]> wrote in message
Hi 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]
Hi,

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!
 
P

Pascal

Hi Stoitcho,

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\) said:
Hi Pascal,
Sorry about the wrong solution I gave you. That was the last post I made
yesturday and I missed to notice that this is not the line that solves the
problem (how I said it looks meaningfull and it is :)). I have left one
line in the textbox *Validating* event that actually does the trick. So here
is my validate event handler

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 said:
Hi Stoitcho,

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]" <[email protected]> wrote in message
Hi 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]

Pascal wrote:
Hi,

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top