Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Default Button In Messagebox 2

Status
Not open for further replies.

SgtJarrow

Programmer
Apr 12, 2002
2,937
US
I am prompting a user to confirm an action. By default, the actions should be No. (It is a check to add records if records already exists...we don't really want to do it most of the time)

I have Option Strict on for my applications, so this code does not work. I can't figure out how to CType it or do what needs to be done to give me a messagebox with a Yes and No button and make the No button the defaulted button. Anyone help??? Thanks.

Code:
If MsgBox("There are currently surveys created for this time period.  Are you sure " _
& "you wish to create more?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "Surveys Exist") = MsgBoxResult.No Then
    Exit Sub
End If

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
First suggestion - don't use the old style MsgBox:

Code:
    Dim msg As String = "This is a message"
    Dim msgdefbtn As MessageBoxDefaultButton = MessageBoxDefaultButton.Button2
    Dim msgbtns As MessageBoxButtons = MessageBoxButtons.YesNoCancel
    Dim msgicon As MessageBoxIcon = MessageBoxIcon.Question
    Dim msgoptns As MessageBoxOptions = MessageBoxOptions.DefaultDesktopOnly
    Dim msgtitle As String = "This is the title"

    Dim rslt As DialogResult = MessageBox.Show(msg, msgtitle, msgbtns, msgicon, msgdefbtn, msgoptns)
    MessageBox.Show("Yes pressed " + rslt.ToString)

Hope this helps.

[vampire][bat]
 
Second suggestion - if you want to continue using MsgBox:

Code:
    Dim msg As String = "This is a message"
    Dim msgstyle As MsgBoxStyle = MsgBoxStyle.YesNoCancel Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Question Or MsgBoxStyle.ApplicationModal
    Dim msgtitle As String = "This is the title"

    Dim rslt As MsgBoxResult = MsgBox(msg, msgstyle, msgtitle)
    MsgBox("You pressed " + rslt.ToString)

By the way, I also always work with Option Explicit On and have tested both sugestions.


Hope this helps.

[vampire][bat]
 
Three things:

First - That worked perfectly! Have a Star!

Second - You are a major asset to these forums. Thanks for being there for everyone.

Third - [sarcasm]Thanks so much for this.[/sarcasm] [smile] I did not know these feature and now have more work to go back and fix my older crappy code....At least at some point. This will be included future forward and when I can I will update my old stuff. This seems to be a much better messagebox.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thanks, its a pleasure to help those who appreciate it - unfortunately too many people never bother to even reply to a solution / suggestion or are fixated with their own "idea" as to how it should be done regardless of the validity of any suggestions.

Best of luck updating your code - post if you have any problems.

When I started with .Net, I made a determined effort to ignore (as far as was possible) all my previous VB5/6 and VBA knowledge and to treat this as a new language.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top