Question MessageBox - How to get the result of a Yes/No?

lake54

Member
Joined
Sep 30, 2009
Messages
6
Programming Experience
Beginner
Hi all,

I'm good with PCs in general, so I'm not a total noob, but I've only just started learning Visual Basic .NET for A Levels.

Part of this week's 'homework' was to write a particular program that does such and such - won't bore you with the details, but please don't think I'm "one of those" that just pass on the homework to a random forum - I've been a geek too long to recognise that that doesn't get anyone anywhere.

So, the question and the details:

I've got two list boxes on a standard form - one listing cities, the other capitals.

If the user selects the correct combo, and presses a check button, a standard msg box is displayed with congrats etc.

If the user selects the wrong combo, another msg box is displayed with a Yes No selection, to the question of if they want to have the correct answer selected for them.

Now, I've got everything working apart from this last part - having the correct answer selected. Here's the snippet of one of the If parts.

Just a word of warning - yes, it'll obviously be 'messy' and probably 'wrong' to anyone who is even half decent at VB.NET, but I've only just started, and it works for me, so that's all for now :)

VB.NET:
        ElseIf lstCountry.SelectedIndex() = 4 Then
            If lstCapital.SelectedIndex() = 0 Then
                MessageBox.Show("You got it right!", "Correct!", MessageBoxButtons.OK)
            Else
                Select Case MessageBox.Show("You got it wrong - do you want to see what the correct answer was?", _
"Sorry!", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
                    Case DialogResult.Yes
                        lstCapital.SelectedItem = 0
                End Select
            End If

Where I have the 'Select' bit, that was the last thing I tried, from another post on this forum: http://www.vbdotnetforums.com/windows-forms/14372-messagebox-buttons-result.html

but I don't quite know where to put the command I want carried out.

I've tried loads of different things to no success, such as instead of 'Case' I've tried If commands with

VB.NET:
ElseIf lstCountry.SelectedIndex() = 4 Then
            If lstCapital.SelectedIndex() = 0 Then
                MessageBox.Show("You got it right!", "Correct!", MessageBoxButtons.OK)
            Else
                MessageBox.Show("You got it wrong - do you want to see what the correct answer was?", _
"Sorry!", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
                If DialogResult = Windows.Forms.DialogResult.Yes Then
                    lstCapital.SelectedItem = 0
                End If
            End If

Please could I have some guidance on how to detect what the response was on a message box, and how to act on the response depending on whether it was a Yes or a No?

Just want to stress again - I haven't given up at the first hurdle - I've been trying to figure this out for well over an hour (probably more) now. Whatever combination I try seems to be wrong - when I press Yes it just stays on the currently selected item.

A massive thank you for any help anyone can provide.

James
 
Last edited:

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
VB.NET:
If lstCapital.SelectedIndex() = 0 Then
    MessageBox.Show("You got it right!", "Correct!", MessageBoxButtons.OK)
Else
    If MessageBox.Show("You got it wrong - do you want to see what the correct answer was?", _
"Sorry!", MessageBoxButtons.YesNo, MessageBoxIcon.Error) = Windows.Forms.DialogResult.Yes Then
        lstCapital.SelectedItem = 0
    End If
End If
 

lake54

Member
Joined
Sep 30, 2009
Messages
6
Programming Experience
Beginner
Sorry just tried that in replace of the entire ElseIf to End If and it didn't work :-S

Have a feeling that I must have done something else wrong if that didn't work.

Thanks for your reply.

James
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I didnt put the first Elseif line in, to make sure you were concentrating :)
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
So what didnt work? What error did it give? Did you try debugging it to see if it was actually processing the code but just not setting the listbox item as you wanted it to?
 

lake54

Member
Joined
Sep 30, 2009
Messages
6
Programming Experience
Beginner
When you click yes, it just closes the message box, and the form itself stays the same. How do I do the debugging you're talking about? Is that the 'step into' stuff?

Thanks for persevering!

James
 

Solitaire

Well-known member
Joined
Jun 28, 2004
Messages
465
Location
New York
Programming Experience
10+
A simple example -- apply the concept to your own program:

VB.NET:
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
		Dim ans As DialogResult
		ans = MessageBox.Show("Did you pass the test?", "Math Test", MessageBoxButtons.YesNo, _
		  MessageBoxIcon.Question)
		If ans = Windows.Forms.DialogResult.Yes Then		'or	If ans = 6 Then
			txtAns.Text = "Satisfactory"
		ElseIf ans = Windows.Forms.DialogResult.No Then		'or If ans = 7 Then
			txtAns.Text = "Unsatisfactory"
		End If
	End Sub
 

lake54

Member
Joined
Sep 30, 2009
Messages
6
Programming Experience
Beginner
Thanks for your code - I tried it again but it still didn't work.

I've decided to leave the code as-is for now (just displaying that it was wrong) rather than spend half an hour or more trying to figure out where I put one small mistake. I've got too much homework for that :)

I'll come back to it in the future, but for now I've got to make a calculator :-S

Of course, you guys could make that in your sleep ;)

James
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
Of course, you guys could make that in your sleep ;)

Dont have time to sleep ;)

Anyway, seeing as you havent given up, investigate using Breakpoints to try and make your application stop at the line :-

VB.NET:
lstCapital.SelectedItem = 0

When it does stop on there, you'll know that the line is being processed and the error is on that line, not a problem with the messagebox.

When you've done that, try changing the line to :-

VB.NET:
lstCapital.SelectedIndex = 0

and all should be fine :)
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I tried it again but it still didn't work.

"My car doesnt work."
"Did you put the key in and turn it to try and start it?"
"No."

"My car still doesnt work."
"Does it have any petrol in it?"
"No."

Compare those to :-

"My car doesnt work. I put the key in and turned it, the engine goes round but it wont start. Could it be low on petrol?"
"Probably. Try putting some more petrol in and see what happens."
"OK, I'll try that."

:)
 
Joined
Feb 8, 2006
Messages
7
Location
Oregon USA
Programming Experience
10+
Here is a working solution done in VS2008 Framework 3.5.

I use a XML literal data source for data and for the question I extend My.Application. Hope this is of use to you.

Place two Listboxes, one checkbox and one label on a form followed by the code below.

VB.NET:
Public Class Form1
   Private document As XDocument = _
   XDocument.Parse(<?xml version="1.0" encoding="utf-8"?>
                   <States>
                      <State>
                         <Name>Delaware</Name>
                         <Capital>Dover</Capital>
                      </State>
                      <State>
                         <Name>Illinois</Name>
                         <Capital>Springfield</Capital>
                      </State>
                      <State>
                         <Name>Oregon</Name>
                         <Capital>Salem</Capital>
                      </State>
                      <State>
                         <Name>Vermont</Name>
                         <Capital>Montpelier</Capital>
                      </State>
                   </States>.ToString)
   Private Sub Form1_Load(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles MyBase.Load

      ListBox1.DataSource = (From item In document...<State> _
                Select Name = item.<Name>.Value, _
                       Capital = item.<Capital>.Value).ToList

      ListBox1.DisplayMember = "Name"
      ListBox1.ValueMember = "Capital"

      ListBox2.DataSource = (From item In document...<State> _
                             Select Capital = item.<Capital>.Value _
                             Order By Capital Descending).ToList

   End Sub

   Private Sub ListBox2_MouseClick(ByVal sender As Object, _
                                   ByVal e As System.Windows.Forms.MouseEventArgs) _
                                   Handles ListBox2.MouseClick

      If ListBox2.SelectedValue.ToString = ListBox1.SelectedValue.ToString Then
         Label1.Text = "Correct"
      Else
         Label1.Text = "Wrong"
         If CheckBox1.Checked Then
            If My.Application.Question("Show correct answer?") Then
               MsgBox(ListBox1.SelectedValue)
            End If
         End If
      End If
   End Sub
End Class

Add a code module with the following code

VB.NET:
Namespace My
    'PartialClasses\KSG_Additions.vb
    ''' <summary>
    ''' This is a water down version of my additions to My Application and My Computer
    ''' classes.
    ''' </summary>
    ''' <remarks></remarks>
    Partial Friend Class MyApplication
        ''' <summary>
        ''' Ask question with NO as the default button
        ''' </summary>
        ''' <param name="QuestionText"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Question(ByVal QuestionText As String) As Boolean
            Return (Windows.Forms.MessageBox.Show(QuestionText, My.Application.Info.Title, Windows.Forms.MessageBoxButtons.YesNo, Windows.Forms.MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
        End Function
        ''' <summary>
        ''' Ask question, NO is default button
        ''' </summary>
        ''' <param name="QuestionText"></param>
        ''' <param name="Title"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Question(ByVal QuestionText As String, ByVal Title As String) As Boolean
            Return (Windows.Forms.MessageBox.Show(QuestionText, Title, Windows.Forms.MessageBoxButtons.YesNo, Windows.Forms.MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
        End Function
        ''' <summary>
        ''' Ask question
        ''' </summary>
        ''' <param name="QuestionText">Question to ask</param>
        ''' <param name="Title">Text for dialog caption</param>
        ''' <param name="DefaultButton">Which button is the default</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Question(ByVal QuestionText As String, ByVal Title As String, ByVal DefaultButton As MsgBoxResult) As Boolean
            Dim db As Windows.Forms.MessageBoxDefaultButton
            If DefaultButton = MsgBoxResult.No Then
                db = Windows.Forms.MessageBoxDefaultButton.Button2
            End If
            Return (Windows.Forms.MessageBox.Show(QuestionText, Title, Windows.Forms.MessageBoxButtons.YesNo, Windows.Forms.MessageBoxIcon.Question, db) = MsgBoxResult.Yes)
        End Function
        ''' <summary>
        ''' Shows text in dialog with information icon
        ''' </summary>
        ''' <param name="Message">Message to display</param>
        ''' <remarks></remarks>
        Public Sub InformationDialog(ByVal Message As String)
            Windows.Forms.MessageBox.Show(Message, My.Application.Info.Title, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)
        End Sub
        ''' <summary>
        ''' Shows text in dialog with information icon
        ''' </summary>
        ''' <param name="Message">Message to display</param>
        ''' <param name="Title"></param>
        ''' <remarks></remarks>
        Public Sub InformationDialog(ByVal Message As String, ByVal Title As String)
            Windows.Forms.MessageBox.Show(Message, Title, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)
        End Sub
        ''' <summary>
        ''' For displaying error/exception text with Error icon
        ''' </summary>
        ''' <param name="ErrorMessage"></param>
        ''' <remarks></remarks>
        Public Sub ExceptionDialog(ByVal ErrorMessage As String)
            Windows.Forms.MessageBox.Show(ErrorMessage, My.Application.Info.Title, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error)
        End Sub
        ''' <summary>
        ''' For displaying error/exception text with Error icon
        ''' </summary>
        ''' <param name="ErrorMessage"></param>
        ''' <param name="Title"></param>
        ''' <remarks></remarks>
        Public Sub ExceptionDialog(ByVal ErrorMessage As String, ByVal Title As String)
            Windows.Forms.MessageBox.Show(ErrorMessage, Title, Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error)
        End Sub
    End Class

End Namespace
 
Top Bottom