Try/Catch block within a Tab Page validating event

C

chadwick

Hi everyone -
I have a form that contains a TabControl object with 2 tab pages,
TabPage1 and TabPage2. TabPage1 contains two text boxes and a cancel
button, which closes the form.
I would like to validate the data contained in two text boxes on
TabPage1 before the user moves to TabPage2, using TabPage1's
validating event. My code looks like this:

Public Class TabPageValidationTester

Private Sub ValidateTextBoxes()
'Otherwise complex processing simplified here.
If TextBox1.Text = TextBox2.Text Then
Dim ex As New System.Exception("Strings in _
TextBox1 and TextBox2 must be different.")
Throw ex
End If
End Sub

Private Sub TabPage1_Validating(ByVal sender As System.Object, _
ByVal e As
System.ComponentModel.CancelEventArgs) _
Handles TabPage1.Validating
Try
ValidateTextBoxes()
Catch ex As Exception
MessageBox.Show("Error. " & ex.Message)
e.Cancel = True
End Try
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class
 
C

chadwick

Hi everyone -
I have a form that contains a TabControl object with 2 tab pages,
TabPage1 and TabPage2. TabPage1 contains two text boxes and a cancel
button, which closes the form.
I would like to validate the data contained in two text boxes on
TabPage1 before the user moves to TabPage2, using TabPage1's
validating event. My code looks like this:

Public Class TabPageValidationTester

Private Sub ValidateTextBoxes()
'Otherwise complex processing simplified here.
If TextBox1.Text = TextBox2.Text Then
Dim ex As New System.Exception("Strings in _
TextBox1 and TextBox2 must be different.")
Throw ex
End If
End Sub

Private Sub TabPage1_Validating(ByVal sender As System.Object, _
ByVal e As
System.ComponentModel.CancelEventArgs) _
Handles TabPage1.Validating
Try
ValidateTextBoxes()
Catch ex As Exception
MessageBox.Show("Error. " & ex.Message)
e.Cancel = True
End Try
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class

Sorry - accidentally posted before finishing my question. My problem
is this: if the user enters text in the boxes, the event successfully
cancels and the focus remains on TabPage1, but the Cancel button on
TabPage1 loses its functionality! That is, when the Cancel button is
clicked, the btnCancel.Clicked event is no longer raised. I have no
idea what cause this. I would like my user to be able to hit cancel
and leave the form after attempting to enter information in the text
box and failing. Any help out there would be greatly appreciated.
 
C

Cor Ligthert[MVP]

Chadwick,

AFAIK is there is no tabpage closing event only a tabpage changed event, so
this will in my idea be inpossible, however don't be afraid, the user won't
see it as you set the selected page back to the one it was.

Cor
 
C

chadwick

Hi Steve - thanks for your reply. I have tried setting
btnCancel.CausesValidation = False, but it didn't seem to work.

An interesting note: the code works perfectly if I comment out the
MessageBox.Show command:

Public Class TabPageValidationTester

Private Sub ValidateTextBoxes()
'Otherwise complex processing simplified here.
If TextBox1.Text = TextBox2.Text Then
Dim ex As New System.Exception("Strings in " _
& "TextBox1 and TextBox2 must be different.")
Throw ex
End If
End Sub

Private Sub TabPage1_Validating(ByVal sender As System.Object, _
ByVal e As
System.ComponentModel.CancelEventArgs) _
Handles TabPage1.Validating
Try
ValidateTextBoxes()
Catch ex As Exception
'MessageBox.Show("Error. " & ex.Message)
e.Cancel = True
End Try
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e
_
As System.EventArgs) Handles
btnCancel.Click
Me.Close()
End Sub
End Class

I think my issue is related to the fact that the MessageBox.Show
command takes focus away from the controls on TabPage1, but I can’t
figure out the details. I could use an ErrorProvider, which would
probably work, but ErrorProviders aren't stark enough for my purposes
and I'd prefer to us a MessageBox. Any further ideas out there?
 

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