Pulling Results from a Dialog form

Pulling Results from a Dialog form

A very common problem with forms is getting the result of a dialog form.  Dialogs are used to ask a question or to get a response.  If it’s a simple question with a Yes/No or OK answer, then the standard dialog form is  quick and easy to create, and simple to use.  But if the user must enter some value or select from an option, getting the information from the form requires extra code.

A dialog box is created using the Dialog template in the Add Windows Form option under the Project menu item. By Default it will be called Dialog1, and will include OK and Cancel buttons.  All that’s needed to use it like that is to add a label with the question to the dialog form, and then call it like this:

Dim MyDialog As Dialog1 = New Dialog1 If MyDialog.ShowDialog = Windows.Forms.DialogResult.OK Then     ‘Processing for “OK” result goes here End If

If the user needs to enter a value or make a selection then extra code is required.  This example will assume that three radio buttons were added to the dialog, and the calling program wants to know which one the user selected.

a. Add the radiobuttons to the dialog form, and add this code to the dialog (at the Class level, that is, not in a function or a Sub). Make sure one of the RadioButtons is checked (set its Checked property in the desinger).

 Public ReadOnly Property UserChoice() As Integer
   Get
     Dim Answer As Integer = 0 ‘Assume button 1
     If RadioButton2.Checked Then Answer = 2
     If RadioButton3.Checked Then Answer = 3
     Return Answer
   End Get
 End Property

b. Change the code in the calling form to look like this:

 Dim MyDialog As Dialog1 = New Dialog1
 If MyDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
   Select Case MyDialog.UserChoice
     Case 1       ‘button 1 was selected
     Case 2       ‘button 2 was selected
     Case 3       ‘button 3 was selected
     End Select
 End If

Note that the test for “OK” is still there, because you are not interested in the user selection if the user pressed Cancel.

The code in the UserChoice property ‘Get’ would need to be adjusted for the type of user input (eg, changed to a text property if the user was required to input some text), and the processing following the ShowDialog obviously needs to be tailored to the type of value returned.

This procedure would be regarded as robust because:
– The calling form has created the dialog as a specific Type – it has a reference to the dialog it created, and there can be no question about whether ot not the properties it needs really exist in that form.
– The dialog has no need to know anything about how it was invoked.  It could be shown from anywhere in the program, for any reason, and it will work properly.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s