0

If I wanted to send either MessageBoxButtons.Ok or MessageBoxButtons.OkCancel to a MessageBox and I wanted to store the correct value in a variable, to what type should I Dim it?

Dim MyVariable As ______ 
If <condition> Then
   MyVariable = MessageBoxButtons.Ok
Else
   MyVariable = MessageBoxButtons.OkCancel
End If

MessageBox.Show("Message", "Title", MyVariable)
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
Dave C
  • 37
  • 2
  • 7
  • This doesn't look like VBA. The messagebox show function is `MsgBox` and the buttons are specified with the `VbMsgBoxStyle` enum. If this is .NET you can dim as MessageBoxButtons. If this is VBA you can dim as `VbMsgBoxResult` – Brad Apr 03 '14 at 22:42
  • Actually it is Visual Basic in Visual Studio 2010 but I didn't see an appropriate tag and I'm not big enough to make a new one. :-/ – Dave C Apr 03 '14 at 22:55
  • You have the right tag - `VB.Net` is what your after. – OneFineDay Apr 03 '14 at 23:28
  • It is an enum, MessageBoxButton type. You should not worry about that and let the compiler *infer* the type. So you can simply write *Dim*. It the option isn't on yet then, perhaps, you need to put `Option Infer On` at the top of the source code file. That option has been turned on for quite a while already, all you could do wrong is using an ancient version of VS. – Hans Passant Apr 03 '14 at 23:37

1 Answers1

1
If MessageBox.Show("some text", "caption", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
  'they picked ok
End If

Or:

Dim result As DialogResult
result = MessageBox.Show("some text", "caption", MessageBoxButtons.OKCancel)
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • If I used the 2nd way, I could later do: If result = DialogResult.Cancel then... ? – Dave C Apr 03 '14 at 23:57
  • Am I seeing a pattern here? If I want to store a constant that's in the form: somename.option, I dim the variable as somename? – Dave C Apr 04 '14 at 00:04
  • Yes you can store a class level variable by the datatype you need then set it later and call it whenever/wherever since it does not lose it's scope. – OneFineDay Apr 04 '14 at 00:10