473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Try/Catch block within a Tab Page validating event

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
Jun 27 '08 #1
5 1929
On Apr 16, 2:14 pm, chadwick <chad.mi...@gmail.comwrote:
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.
Jun 27 '08 #2
chadwick wrote:
> 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

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.
The issue is that if you cancel in the validating event of TabPage1, the focus
can't move to the Cancel button. A cancelled validation is basically modal, and
must be corrected before you can do anything.

Fortunately, the fix is easy. Set btnCancel.CausesValidation = False. :)



Jun 27 '08 #3
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

"chadwick" <ch********@gmail.comschreef in bericht
news:22**********************************@q10g2000 prf.googlegroups.com...
On Apr 16, 2:14 pm, chadwick <chad.mi...@gmail.comwrote:
>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.
Jun 27 '08 #4
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?
Jun 27 '08 #5
chadwick wrote:
Hi Steve - thanks for your reply. I have tried setting
btnCancel.CausesValidation = False, but it didn't seem to work.
Odd. I copy/pasted your code, and it worked fine for me.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
9
by: Mark | last post by:
Hello I'm trying to use a Server.Transfer in a try-catch (I cannot put it outside the Try-Catch as it is nested deep within a component that is called in a try-catch loop) The problem is that the...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
3
by: Fred Chateau | last post by:
To catch an exception associated with resources within a Using block, does the Catch block go within the scope of the Using block or following it? If within the scope of the Using block, what...
5
by: Morten Snedker | last post by:
The use of Try in the Finally part - is that overkill?. I think of the code failing before opening sqlCon - that would generate an error in the Finally part. How would Finally handle that? Try...
4
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.