Results 1 to 4 of 4

Thread: [RESOLVED] Call event handler sub

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] Call event handler sub

    Hi Folks

    I have an TextBox.KeyPress event handler sub that essentially does the following
    VB Code:
    1. Private Sub IdBox_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles IdBox.KeyPress
    2.         ' Handle any "Enter"-key presses in the Id TextBox when the entered text length is greater than 0.
    3.  
    4.         If Asc(e.KeyChar) = Keys.Enter And IdBox.Text.Length > 0 Then
    5.             Dim Id As String = IdBox.Text
    6.  
    7.             ' Indicate that the keypress is handled.
    8.             e.Handled = True
    9.  
    10.             Try
    11.                 ' Create a new widget in the widget collection.
    12.                 widgets.Add(newWidgetKey, New Widget(IdBox.Text))
    13.  
    14.             Catch ex As AlreadyInUseException
    15.                 ' Exception if ID is already in use.
    16.                 ' Ask the user if they would like to cancel the ID already in use .
    17.                 Dim userReply As DialogResult = MessageBox.Show("Could not use that ID as it is listed as being already in use." _
    18.                                                                 + vbNewLine + "Would you like to cancel the currentID?", _
    19.                                                                 "ID in use", MessageBoxButtons.YesNo, MessageBoxIcon.Error, _
    20.                                                                 MessageBoxDefaultButton.Button2)
    21.  
    22.                 If userReply = Windows.Forms.DialogResult.Yes Then
    23.                     ' If the user wishes to cancel the ID,
    24.                     ' list the widgets with that ID from the collection of current widgets.
    25.                     Dim duplicateWidgets = (From widget In widgets
    26.                                          Where widget.Value.Id = IdBox.Text
    27.                                          Select widget.Key).ToList
    28.                     ' Check if the list has any items.
    29.                     If duplicateWidgets.Count > 0 Then
    30.                         ' Cancel each listed widget.
    31.                         For Each widgetKey As UInteger In duplicateWidgets
    32.                             widgets.Item(widgetKey).CancelWidget()
    33.                             widgets.Remove(widgetKey)
    34.                         Next
    35.                     End If
    36.  
    37.                     Try
    38.                         ' Try creating the new widget in the widget collection again.
    39.                         widgets.Add(newWidgetKey, New Widget(IdBox.Text))
    40.                     Catch exRe As Exception
    41.                         ' If there were still exceptions, make the user start again.
    42.                         MessageBox.Show("Still could not use that Id" + vbNewLine + _
    43.                                         exRe.Message, "Id could not be used", _
    44.                                         MessageBoxButtons.OK, MessageBoxIcon.Error)
    45.                         Exit Sub
    46.                     End Try
    47.                 Else
    48.                     ' If the user wishes to try another Id instead,
    49.                     ' Select the text in the Id box, ready for attempting a new Id.
    50.                     IdBox.SelectAll()
    51.                     Exit Sub
    52.                 End If
    53.  
    54.             Catch ex As Exception
    55.                 ' Exception if .Add() failed or New() failed for unknown reason - This should never happen.
    56.                 MessageBox.Show("Id validation failed for an unknown reason." + vbNewLine + _
    57.                                 "Please try again.","Id validation failed")
    58.                 IdBox.SelectAll()
    59.                 Exit Sub
    60.             End Try
    61.  
    62.         End If
    63.     End Sub

    In plain English, the user enters and ID into the TextBox and presses the enter key, the event handler tries to create a widget with that ID, but will fail if there is already another one with the same ID.
    The user then is presented with a choice, cancel the older widget and continue creating the new one, or try another ID.
    If they chose to cancel the old one and continue creating the new one, the handler has to try again... Except this leads to a nested Try (of which I'm not fond) which essentially does the same thing the preceding code just did...

    Is there a way I can remove the nested Try and just call the Handler again here? Or is there a better way of handling the situation altogether?
    This would mean that the nested Try exception could also be handled more gracefully

    I could leave it as is, but I have this situation cropping up in other places as well so would like to find a neater way of doing it
    Thanks
    Last edited by wolf99; Aug 19th, 2013 at 05:27 AM.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width