i need help. i am learning VB and am trying to display a 'welcome' message when the form appears to the screen using the messagebox.show
this is a very simple application consisting of only one form. a simple calculator
i need help. i am learning VB and am trying to display a 'welcome' message when the form appears to the screen using the messagebox.show
this is a very simple application consisting of only one form. a simple calculator
Good start from — MessageBox.Show will do the job (as suggested) and correctly pointed out this is VB.NET. A couple of practical improvements make the welcome experience smoother and the calculator code more robust.
Prefer the Form.Shown event instead of Load so the window is painted before a modal dialog appears. Example:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
MessageBox.Show("Welcome to the calculator.", "Welcome", MessageBoxButtons.OK)
End Sub For a less intrusive experience, show an in-form label that auto-hides with a Timer (no modal dialog). Add a Label (welcomeLabel) and a Timer (welcomeTimer, Interval = 3000) and start the timer in Shown:
' designer: welcomeLabel.Visible = False, welcomeTimer.Interval = 3000
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
welcomeLabel.Text = "Welcome to the calculator."
welcomeLabel.Visible = True
welcomeTimer.Start()
End Sub
Private Sub welcomeTimer_Tick(sender As Object, e As EventArgs) Handles welcomeTimer.Tick
welcomeTimer.Stop()
welcomeLabel.Visible = False
End Sub Improve the calculator logic by avoiding exception-driven parsing. Use Decimal for money and TryParse to validate inputs, then format the output:
Dim price As Decimal
Dim qty As Integer
If Not Decimal.TryParse(pricetxt.Text, price) Then
MessageBox.Show("Enter a valid price.")
pricetxt.Focus()
Exit Sub
End If
If Not Integer.TryParse(quantitytxt.Text, qty) Then
MessageBox.Show("Enter a valid quantity.")
quantitytxt.Focus()
Exit Sub
End If
Dim total As Decimal = price * qty * 1.085D
Totallbl.Text = total.ToString("C2") To show the welcome only on first run, store a Boolean user setting (e.g., ShowWelcome default True) and clear it after the first display:
If My.Settings.ShowWelcome Then
MessageBox.Show("Welcome!")
My.Settings.ShowWelcome = False
My.Settings.Save()
End If Summary: prefer Shown for modal dialogs, prefer an in-form label + Timer for non-blocking UX, and use Decimal.TryParse / settings for safer, cleaner behavior.
Jump to Post— hkdani 39MessageBox.Show
You're in the wrong forum. But that's ok, VB.Net is about the same as VB6.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show("Welcome, to my Calculator", "Welcome", MessageBoxButtons.OK) End Sub
this is how my code looks so far.
Public Class Form1
Private Sub calculatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculatebtn.Click
Dim Resultsng As Single
Dim Pricesng As Single
Dim Quantityint As Integer
Try
'Get data from user
Pricesng = CSng(pricetxt.Text)
Quantityint = CInt(quantitytxt.Text)
'Calculate results
Resultsng = CSng(Pricesng * Quantityint * 1.085)
Totallbl.Text = CStr(Resultsng)
Catch ex As InvalidCastException
MessageBox.Show("Please type numbers only")
Catch ex As OverflowException
MessageBox.Show("Please enter a smaller number")
Catch
MessageBox.Show("An error occured due to data type")
End Try
End Sub
Private Sub resetbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resetbtn.Click
pricetxt.Clear()
quantitytxt.Clear()
Totallbl.Text = String.Empty
'Return cursor to price box after clearing
pricetxt.Focus()
End Sub
Private Sub quitbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quitbtn.Click
Me.Close()
End Sub
End Class MessageBox.Show
You're in the wrong forum. But that's ok, VB.Net is about the same as VB6.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show("Welcome, to my Calculator", "Welcome", MessageBoxButtons.OK)
End Sub MessageBox.Show
You're in the wrong forum. But that's ok, VB.Net is about the same as VB6.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show("Welcome, to my Calculator", "Welcome", MessageBoxButtons.OK) End Sub
The topic starter uses VB.NET already lood at his codes you'll see the diff between VB6 and .NET
Anyway, i guess the this is solved by your code above.
:)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.