Create User Input function

mattkw80

Well-known member
Joined
Jan 3, 2008
Messages
49
Location
Ontario, Canada
Programming Experience
Beginner
Hey Guys and Girls,

Trying to imitate a Messagebox - in that, a sub will not continue until the user "deals with" the pop up.

For example, in a sub, I want a Form to pop up, and ask a user a question, and give them 3 options. I want to make it so the user must click an option, and then the sub that called it can carry on it's way.

I believe I need a function to make this happen.

Can anyone point me in the right direction on how to do this?
 
Put in some thought and a little effort, and it will come to you. :)
 
Here is a sample using MessageBoxes with various arguments. Also included is a sample with an InputBox.


VB.NET:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ans As DialogResult
        Dim reply As String
        ans = MessageBox.Show("Answer me", "Caption", MessageBoxButtons.YesNoCancel)
        MessageBox.Show(ans.ToString)
        ans = MessageBox.Show("Answer me", "Caption", MessageBoxButtons.AbortRetryIgnore)
        MessageBox.Show(ans.ToString)

        ans = MessageBox.Show("Did you pass the test?", "Math Test", MessageBoxButtons.YesNo, _
    MessageBoxIcon.Question)
        If ans = DialogResult.Yes Then
            MessageBox.Show("Satisfactory")
        ElseIf ans = DialogResult.No Then
            MessageBox.Show("Unsatisfactory")
        End If

        reply = InputBox("Enter your own name:", "InputBox", "Bob")
        MessageBox.Show("Your name is " & reply)
    End Sub
 
Hey Guys and Girls,

Trying to imitate a Messagebox - in that, a sub will not continue until the user "deals with" the pop up.

For example, in a sub, I want a Form to pop up, and ask a user a question, and give them 3 options. I want to make it so the user must click an option, and then the sub that called it can carry on it's way.

I believe I need a function to make this happen.

Can anyone point me in the right direction on how to do this?

like johnH said use the ShowDialog Method. if you are using VS.net, you can go to add item and add a Dialog, it's a template for what you need. Heres a screenshot from 2010.

Untitled.png

example code for using showDialog
        Dim dialog As New Dialog2
        Dim result As DialogResult = dialog.ShowDialog()

        If result = Windows.Forms.DialogResult.Yes Then 'instead of using a variable dialog.ShowDialog() can be used instead of result here

        Else

        End If


in your form the one that is going to be opened, continuing with the previous code ex it would be called Dialog2. Just set the dialogresult and close it, just like the template.

ex:
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Yes
        Me.Close()
    End Sub
 
So far so good - but I can only get the dialog box to ask Yes, No, Cancel, etc. using MessageBoxButtons.YesNoCancel.

Is there anyway I can pass string variables, so I'm asking other questions?

ie:

What is your favorite color?

(buttons appear) Blue, Yellow, Red
 
You can create a form and add any controls to it you want, then to display it as dialog use the ShowDialog method.
For example a Label can present the question, and the options could be radiobuttons.
 
Back
Top