Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy Question! Try/Catch?

Status
Not open for further replies.

richself

Technical User
Jun 27, 2005
37
US
I'm requiring a user to enter data into various text boxes on a form. All is within a Try/Catch which simply shows a message box if they try to submit the data without filling out all of the fields. Right now, all the text boxes clear and the focus reset to the first box when the catch executes.
How do I retain all the already entered data in the text boxes so the user does not have to re enter everything when this happens? Should I use something other than a Try/Catch?

Thanks
 
After going back to my code... I wasn't totally accurate in the above question. I had a clear function after the Try/Catch. When I comment that out, it retains the data. What I need is to reset focus to the text box that did not have data entered into it.

 
Hmm... better use the error provider tool. For your application when the user will not fill in all the textboxes this means he can't continue. You add only one error provider component and as you check each one textbox you do something like this: errorProvider1.setError(...).

I believe that try/catch block is not a good idea.

If the form has other textboxes that you don't need-want to check do something like that:

dim x as control
for each x in me.controls
if typeof x is textbox and x.name.toupper.startswith("C") then
' code does here
end if
next


A little explanation on the "x.name.toupper.startswith("C")". We usually name textboxes like this: txt + a_name.
To those textboxes you need to check and a "c" or "C" in front of the name them. Doesn't matter if letter c is in capitals or not because i use the .toypper property.



Hope Helped you with may long documentation.
:)
 
forgot...

you can avoid the "and" and only check if it is textbox, if you and the textboxes on a panel. So the For each becomes now:

for each x in me.panel1.controls
...
next
 
Dim goodDataBoolean As Boolean = False


cRow = DataSet1.Tables(0).NewRow()

Try

'Add Personel info
cRow("Supervisor") = supervisorTextBox.Text
cRow("RF") = rfTextBox.Text
cRow("GL") = glTextBox.Text
cRow("Others") = othersTextBox.Text

....Omitted code
....Continues on with adding to Dataset.....

goodDataBoolean = True

Catch ex As Exception
MessageBox.Show("Please fill in All ", "Data Input Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)

supervisorTextBox.Focus()

goodDataBoolean = False

End Try

If goodDataBoolean Then
DataSet1.Tables(0).Rows.Add(cRow)
'Call clear function
clear()
End If

End Sub


Is what I have so far... works fine but resets focus to first text box instead of first empty text box
 
In the following I've simply assigned a few variables to represent your datagrid columns, but I think it will do what you need.

Code:
  Private Function ValidateTextBox(ByVal tb As TextBox) As String

    If tb.Text.Length > 0 Then
      Return tb.Text
    Else
      tb.Focus()
      Throw New Exception(tb.Name + " must not be blank")
    End If

  End Function

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim tb1 As String
    Dim tb2 As Integer
    Dim tb3 As String

    Try
      tb1 = ValidateTextBox(TextBox1)
      tb2 = CInt(ValidateTextBox(TextBox2))
      tb3 = ValidateTextBox(TextBox3)
    Catch ex As Exception
      MessageBox.Show(ex.Message)
    End Try

  End Sub

  Private Sub TextBox2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Leave

    If Not IsNumeric(TextBox2.Text) Then
      MessageBox.Show("Number Required")
      TextBox2.Text = ""
      TextBox2.Focus()
    End If

  End Sub

The ValidateTextBox function returns a string (because that's what's in a TextBox), so you will need to carry out any necessary conversion functions, additionally you will need to validate (I've used Leave, but you could also use LostFocus) those TextBoxes.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top