Question How to display variable in msg box?

BillO

Member
Joined
Sep 20, 2010
Messages
9
Programming Experience
Beginner
Hi there,

I'm fairly new to VB programming. I am struggling to find out how to use a msg box to display the value of a variable.

I've tried a number of permutations, and searched for hours to find a tip.

Any suggestions very welcome!

My code is currently:

Private Sub btnShowTotalCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowTotalCost.Click
If (radCorpTravel.Checked) Then
msg = MsgBox("Total for Corporate Vehicle Travel Costs: " + CorpCost, 0 + 48 + 0, "Corporate Vehicle Travel Costs")
ElseIf (radPrivTravel.Checked) Then
msg = MsgBox("Total for Private Vehicle Travel Costs: " + PrivCost, 0 + 48 + 0, "Private Vehicle Travel Costs")
Else
msg = MsgBox("Total for All Vehicle Travel Costs: " + AllCost, 0 + 48 + 0, "All Vehicle Travel Costs")
End If
End Sub

CorpCost, PrivCost & AllCost are the variables.

Many thanks :confused:
 
I assume that your variables are of numeric datatype (Double, Decimal etc.)
VB.NET:
        If (radCorpTravel.Checked) Then
            MessageBox.Show("Total for Corporate Vehicle Travel Costs: " & CorpCost.ToString, "Corporate Vehicle Travel Costs")
        ElseIf (radPrivTravel.Checked) Then
            MessageBox.Show("Total for Private Vehicle Travel Costs: " & PrivCost.ToString, "Private Vehicle Travel Costs")
        Else
            MessageBox.Show("Total for All Vehicle Travel Costs: " & AllCost.ToString, "All Vehicle Travel Costs")
        End If

P.S. In .NET you use MessageBox Class (System.Windows.Forms) instead MsgBox ;) To display a message box call the static method MessageBox.Show
 
In addition to using the MessageBox.Show() method instead of the legacy MsgBox, you should also be using the & concatenation operator instead of the + math operator.

The sample posted by Kulrom is the one you should use.

Also, you should always have Option Strict On. That will point out conversion incompatibilities and help prevent a lot of possible errors.
 
Further, about the meaning of numbers in MsgBox code; OK button only is default, but Exclamation icon have to be specified, so you have to use this overload of Show:
VB.NET:
MessageBox.Show("text", "caption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Notice the OK and Exclamation values are members of the MessageBoxButtons and MessageBoxIcon enumerations, when you write code intellisense provide these valid options. It is not good coding practice to use the 'magic' numbers as in code you posted, in fact when type safety is enforced with Option Strict the method will not allow that you type in such numbers.

If you refer to help for MessageBox.Show method you can see there are many versions of this method to choose from, that have different configuration options, the sample above is using this signature (displays the type list of the parameters):
Show(String, String, MessageBoxButtons, MessageBoxIcon)
 
Further further, instead of concatenating the variable with a fixed string in each of the cases you could use a string with a placeholder then use String.Format() to fill in the placeholder(s):
VB.NET:
Dim MessageStr As String = "Total for Corporate Vehicle Travel Costs: {0}" '{0} is the first (and only) placeholder in this string
Dim TitleStr As String = "{0} Vehicle Travel Costs"
Select Case True
    Case radCorpTravel.Checked : MessageBox.Show(String.Format(MessageStr, CorpCost), String.Format(TitleStr , "Corporate"))
    Case radPrivTravel.Checked : MessageBox.Show(String.Format(MessageStr, PrivCost),  String.Format(TitleStr , "Private"))
    Case Else : MessageBox.Show(String.Format(MessageStr, AllCost), String.Format(TitleStr , "All"))
End Select
 
Thank you everyone!

Thank you for the excellent advice! I haven't had an opportunity to give it a go yet. Hopefully it will work with my outdated copy of Visual Studio 5 :)

You have answered explicitly, and may have also assisted in an empty / string vs numeric text box validation question I also had.

I've been banned from sinking my teeth into ait s we're just off for a well deserved week of holiday. Let you know how it all goes once I'm back.
 
Just about everything in this thread wont work with vb5 since we have all provided vb.net solutions to your problem(s) here. For inquiries about vb6 or older I would suggest you find a vb6 and older forum since this forum is entirely .Net
 
Hopefully it will work with my outdated copy of Visual Studio 5
Understood as VS 2005 it should. You can always download VB 2010 Express for free.
 
Understood as VS 2005 it should. You can always download VB 2010 Express for free.

I assume the code posted by the OP is VB 2005. The opening line includes a Handler, which is not present in the old VB5 or VB6 code.
Based on the first post I was thinking it was something .Net, but then he said 'Visual Studio 5' which is an entirely pre-.Net program (which contains vb5), so I was letting him know that if he is indeed using Visual Studio 5 as he said then the suggestions here wont work. If he meant Visual Studio 2005, then all is well as everything suggested here will work on anything VS 2002 and newer
 
Hi All,

As guessed I did mean Visual Studio 2005.

Thanks again for the fantastic help. It worked well. In the end I just kept it simple:
VB.NET:
Private Sub btnShowTotalCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowTotalCost.Click
        Corp = FormatCurrency(CorpCost)
        Priv = FormatCurrency(PrivCost)
        All = FormatCurrency(AllCost)
        If (radCorpTravel.Checked) Then
            MessageBox.Show("Total for Corporate Vehicle Travel Costs: $" & Corp, "Corporate Vehicle Travel Costs", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf (radPrivTravel.Checked) Then
            MessageBox.Show("Total for Private Vehicle Travel Costs: $" & Priv, "Private Vehicle Travel Costs", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("Total for All Vehicle Travel Costs: $" & All, "All Vehicle Travel Costs", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
I'm a little surprised this code deson't lead to two $$ as I had thought formatcurrency would include a $. Strange my final question to hopefully put this program to bed I will address in a new thread. Again my sincere thanks!
 
Back
Top