hi iam new to programming and i want to know how i tell vb.net if it was replied to a msgbox with (yes ) then do something
please help me about that

Dani AI

Generated

Short answer: in a Windows Forms app show a dialog and inspect its return value; the modern pattern is to use MessageBox.Show and check the DialogResult enum. Several replies here pointed that direction — and demonstrated the basic idea and showed the older MsgBox style. The example below uses MessageBoxButtons.YesNo and tests for DialogResult.Yes, which is the clearest way to detect a Yes response.

Example (WinForms):

Dim result As DialogResult = MessageBox.Show(Me, "Save changes now?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
    ' perform save
Else
    ' user chose No
End If

If you need three choices use MessageBoxButtons.YesNoCancel and a Select Case on the DialogResult. Prefer MessageBox.Show from System.Windows.Forms for new WinForms work; MsgBox (in Microsoft.VisualBasic) still exists but is legacy. See the docs for details: MessageBox.Show and MsgBox. The DialogResult values are documented here: DialogResult enum.

Extra tips:

  • When reading numeric text (like incrementing a button label) use Integer.TryParse to avoid exceptions:
    Dim n As Integer
    If Integer.TryParse(Button1.Text, n) Then Button1.Text = (n + 1).ToString()
  • MessageBox.Show must be called on the UI thread; calling it from a background thread requires Invoke.
  • For ASP.NET/web scenarios a server-side MessageBox will not work — use client-side confirm() instead (see MDN: Window.confirm).

Recommended Answers

All 3 Replies

Try this:

If MessageBox.Show("Button Pressed " & CStr(Int(Button1.Text) + 1) & " times") = Windows.Forms.DialogResult.OK Then
            Button1.Text = CStr(Int(Button1.Text) + 1)
        End If

or this:

Dim result As Integer = MessageBox.Show("This is a message")
        If result = vbOK Then Button1.Text = "Something"
If MsgBox("Your Confirm Message ?", MsgBoxStyle.OKCancel, "Exit confirm") = MsgBoxResult.OK Then
         'add your code to do something if yes button pressed
Else
         ' Do nothing when cancel was pressed
End If

OK.

hi try this

dim a as string
a=msgbox("do u want to save",MsgBoxStyle.YesNo)
if a= vbyes then
do this
else
do this
end if
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.