4

I'm trying to create a MsgBox() that has both the MsgBoxStyle.Critical style and the MsgBoxStyle.RetryCancel button style. What I've tried so far is this:

Private Sub DoSomething()
    Dim Answer as MsgBoxResult
        Answer = MsgBox("Error", MsgBoxStyle.RetryCancel & MsgBoxStyle.Critical, _
        "Some sort of error.")
    If Answer = MsgBoxResult.Retry Then
        'REM: Try code again
    Elseif Answer = MsgBoxResult.Cancel Then
        Exit Sub
    End If
End Sub

This is what the buttons currently look like:

Should be Retry/Cancel

There is no Critical icon on the message box.

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 2,576
  • 6
  • 34
  • 52

3 Answers3

11

Bitwise "combine" is called Or.

MsgBoxStyle.RetryCancel Or MsgBoxStyle.Critical

MsgBoxStyle.RetryCancel & MsgBoxStyle.Critical evaluates to "5" + "16", which evaluates to "516", which evaluates to 516, which magically equals MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton3 and is therefore interpreted as such.

Don't use the string concatenation operator & for bitwise logic.

GSerg
  • 76,472
  • 17
  • 159
  • 346
4

Use MessageBox instead of MsgBox. Try this:

    Dim _result As DialogResult = MessageBox.Show("Do you want to retry", "Confirm Retry", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
    If _result = Windows.Forms.DialogResult.Retry Then
        ' try again code
    Else

    End If

MessageBox Sample

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ambrose
  • 501
  • 3
  • 13
-1

Using msgbox: Instead of the & use + sign, e.g.

MsgBoxStyle.RetryCancel + MsgBoxStyle.Critical

should work, you could also try just the number so 5 (RetryCancel) + 16 (Critical) = 21. e.g.

MsgBox("Error", 21, "Some sort of error.") should work.

msgboxStyle codes:
OKOnly  = 0
OKCancel = 1
AbortRetryIgnore = 2
YesNoCancel = 3
YesNo = 4
RetryCancel = 5
Critical = 16
Question = 32
Exclamation = 48
Information = 64
DefaultButton1 = 0
DefaultButton2 = 256
DefaultButton3 = 512
Draken
  • 3,134
  • 13
  • 34
  • 54
  • 1
    It is a very bad advice to replace the constants [with their numeric values](https://stackoverflow.com/q/47882/11683). – GSerg Jul 31 '19 at 13:50