MsgBox

MsgBox is more or less the equivalent of the old VB6 Msgbox. Over the years Apple, and more recently Microsoft have moved away from the simple responses (yes/no, abort/retry/cancel) because users found them confusing. The better way is to have a button with an action. Delete/Cancel, Open/Cancel, or whatever you want.

Here’s what we’ve used for years (we also have a training video on how to create this):

[code]Function Alert(Type as AlertTypes, Title as string, msg as string, btn1txt as string, btn2txt as string, btn3txt as string, w as window = nil) As integer
Dim d as New MessageDialog //declare the MessageDialog object
Dim b as MessageDialogButton //for handling the result

select case Type
case AlertTypes.kCaution
d.icon=MessageDialog.GraphicCaution //display warning icon
case AlertTypes.kNote
d.icon = MessageDialog.GraphicNote
case AlertTypes.kStop
d.icon = MessageDialog.GraphicStop
case AlertTypes.kPlain
d.icon = MessageDialog.GraphicNone
case AlertTypes.kQuestion
d.icon = MessageDialog.GraphicQuestion
end

d.ActionButton.Caption= btn1txt

if btn2txt <> “” then
d.CancelButton.Visible= True //show the Cancel button
d.CancelButton.Caption = btn2txt
else
d.CancelButton.Visible = false
end

if btn3txt <> “” then
d.AlternateActionButton.Visible= True //show the “Don’t Save” button
d.AlternateActionButton.Caption = btn3txt
else
d.AlternateActionButton.Visible = false
end

d.Message = Title
d.Explanation = msg
if w = nil then
b=d.ShowModal //display the dialog
else
b = d.ShowModalWithin( w )
end

Select Case b //determine which button was pressed.
Case d.ActionButton
return 1
Case d.AlternateActionButton
return 3
Case d.CancelButton
return 2
End select
End Function
[/code]

And if used in code it’s like this:

dim res as integer = BKS.Alert( kAlertCaution, "Title Message.", _ "Detailed message", _ "Button1Text", "Button2Text", "Button3Text", self.trueWindow) if res = 1 then return

[quote=99696:@Anthony Dellos]Here’s a little sample app that demonstrates the issue . Notice how the second time the dialog is shown, the value of the property “state” has changed.

Easy to see what’s happening in the sample. Much harder when you have lots of controls, nested containers, etc…[/quote]

Thank you for sharing this example.

What code have you tried for your MsgBox call? The Language Reference page describes how have to calculate the integer value to display the different combinations of buttons, icons and defaults. Personally, I find all that to be a hassle so I just use MessageDialog.

We only discourage the use of message boxes for debugging purposes because of the reasons others have described above.

[quote=99557:@Djamel AIT AMRANE]hello,
is it possible to use the MsgBox function like in Vb6:

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

With Xojo i use only msgbox(“message”), but not possible msgbox(“message”,buttons,title).[/quote]

// // The text "Title" in the following example is displayed in the MsgBox Window "Title Bar". // See LR for details. // Dim n As Integer n = MsgBox("Do you want to rebuild this mailbox?", 36, "Title") If n = 6 Then //user pressed Yes MsgBox "User clicked Yes" ElseIf n = 7 Then //user pressed No MsgBox "User clicked No" End If

[quote=99695:@Djamel AIT AMRANE][code]
Sayed, this code is almost equivalent to Vb6 msgBox, unfortunately we can display only the msg and the icon, nothing else.[/quote]

The text “nothing else” requires qualification please. The “MsgBox” in Xojo can only do what is documented in the LR.

When the code from the last example of “MsgBox” in LR is copied and when pasted here, it has the following format.

Dim n As Integern = MsgBox("Do you want to rebuild this mailbox?", 36)If n = 6 Then  //user pressed YesElseIf n = 7 Then  //user pressed NoEnd If

It was corrected in a hurry and resulted in the indicated errors. Thank you for pointing those out.

My goal it’s to find if is possible to return same as Vb6, every thing in one line. with MsgBox Function.
Xojo is different to Vb6 in many things, with DialogMessage it’s running but is more heavy then Vb6 msgBox().

[quote=99723:@Djamel AIT AMRANE]My goal it’s to find if is possible to return same as Vb6, every thing in one line. with MsgBox Function.
Xojo is different to Vb6 in many things, with DialogMessage it’s running but is more heavy then Vb6 msgBox().[/quote]

If you want to have a one-line msgbox like in VB, you can write a method, lets call it messagebox, where parameters let you call all parameters, such as

Function MessageBox(prompt as string, buttons as integer, title as string)

Within the method you put all is necessary for the MessageDialog. Actually, you could make buttons a string with input like the VB6 constants (MsgBoxStyle.OKOnly, etc.) and through a Select Case set the buttons value.

Then you call MessageBox in your code the same way as MsgBox in VB6…

@Michel Bujardet - Happy to share. I’ve also been burnt hard in the past by “smart” control subclasses that I (being less “smart” than they) had completely forgotten were executing code on lost focus.

Subclasses where the parent class has “stock” functionality in the LostFocus event are a great way to get burnt. :stuck_out_tongue:

[quote=99738:@Anthony Dellos]@Michel Bujardet - Happy to share. I’ve also been burnt hard in the past by “smart” control subclasses that I (being less “smart” than they) had completely forgotten were executing code on lost focus.

Subclasses where the parent class has “stock” functionality in the LostFocus event are a great way to get burnt. :P[/quote]

Yes. That and code where activate is used, as MsgBox is in fact a window and deactivates the current window.

It is good to put that in evidence.

[quote=99723:@Djamel AIT AMRANE]My goal it’s to find if is possible to return same as Vb6, every thing in one line. with MsgBox Function.
Xojo is different to Vb6 in many things, with DialogMessage it’s running but is more heavy then Vb6 msgBox().[/quote]

“same as Vb6, every thing in one line.”. Maybe as follows?

  //
  // Number and Type of Buttons. 
  //
  // 0 - Display OK button only.
  // 1 - Display OK and Cancel buttons.
  // 2 - Display Abort, Retry, and Ignore buttons.
  // 3 - Display Yes, No, and Cancel buttons.
  // 4 - Display Yes and No buttons only.
  // 5 - Display Retry and Cancel buttons.
  //
  // Icon to be Displayed. 
  //
  // 0 - No icon.
  // 16 - Stop sign icon.
  // 32 - Question icon.
  // 48 - Caution triangle icon.
  // 64 - Note Icon.
  //
  // Selection of Default Button. 
  //
  // 0 - First button of Group 1 list is the default.
  // 256 - Second button of Group 1 list is the default.
  // 512 - Third button of Group 1 list is the default.
  // 768 - No button is the default.
  //
  // Result.
  //
  // 1 - OK Pressed
  // 2 - Cancel Pressed
  // 3 - Abort pressed
  // 4 - Retry pressed
  // 5 - Ignore pressed
  // 6 - Yes pressed
  // 7 - No pressed
  //
  //
  //
  //
  // 3 - Display Yes, No, and Cancel buttons.
  // 16 - Stop sign icon.
  // 768 - No button is the default.
  //
  // 7 - No pressed
  //
  If MsgBox( "Show this message to user", 3 + 16 + 768, "This Text in 'MsgBox' Title") = 7 Then
    MsgBox( "User selected 'No'")
  Else
    MsgBox( "User did not select 'No'")
  End If

Sayed, very good, it’s almost like Vb6, thank you.

The biggest difference I have found between VB and Xojo’s MSGbox is that Xojo’s msgbox does not like integers thrown at it. If you want to display a number amongst a string, you need to convert the number using STR() or similar.

hi bob, what is this AlertTypes?? is it a class or property or constant??

[quote=99697:@Bob Keeney]MsgBox is more or less the equivalent of the old VB6 Msgbox. Over the years Apple, and more recently Microsoft have moved away from the simple responses (yes/no, abort/retry/cancel) because users found them confusing. The better way is to have a button with an action. Delete/Cancel, Open/Cancel, or whatever you want.

Here’s what we’ve used for years (we also have a training video on how to create this):

[code]Function Alert(Type as AlertTypes, Title as string, msg as string, btn1txt as string, btn2txt as string, btn3txt as string, w as window = nil) As integer
Dim d as New MessageDialog //declare the MessageDialog object
Dim b as MessageDialogButton //for handling the result

select case Type
case AlertTypes.kCaution
d.icon=MessageDialog.GraphicCaution //display warning icon
case AlertTypes.kNote
d.icon = MessageDialog.GraphicNote
case AlertTypes.kStop
d.icon = MessageDialog.GraphicStop
case AlertTypes.kPlain
d.icon = MessageDialog.GraphicNone
case AlertTypes.kQuestion
d.icon = MessageDialog.GraphicQuestion
end

d.ActionButton.Caption= btn1txt

if btn2txt <> “” then
d.CancelButton.Visible= True //show the Cancel button
d.CancelButton.Caption = btn2txt
else
d.CancelButton.Visible = false
end

if btn3txt <> “” then
d.AlternateActionButton.Visible= True //show the “Don’t Save” button
d.AlternateActionButton.Caption = btn3txt
else
d.AlternateActionButton.Visible = false
end

d.Message = Title
d.Explanation = msg
if w = nil then
b=d.ShowModal //display the dialog
else
b = d.ShowModalWithin( w )
end

Select Case b //determine which button was pressed.
Case d.ActionButton
return 1
Case d.AlternateActionButton
return 3
Case d.CancelButton
return 2
End select
End Function
[/code]

And if used in code it’s like this:

dim res as integer = BKS.Alert( kAlertCaution, "Title Message.", _ "Detailed message", _ "Button1Text", "Button2Text", "Button3Text", self.trueWindow) if res = 1 then return[/quote]

It’s an enum.

Public Enum AlertTypes kCaution = 2 kNote = 1 kPlain = 3 kStop = 0 kQuestion = 4

thanks bob…got it working now.

first time i use Enum