Results 1 to 11 of 11

Thread: What to do when user click msgbox button

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question What to do when user click msgbox button

    Will anyone help me on how to catch a button when a user click on a msgbox()? Let's say I have this:

    Code:
    msgbox("You've entered the wrong password", MsgBoxStyle.Critical)
    Now when the user click the "OK" button on that msgbox(), I want to catch that and do something else with it.

    Any help is appreciated.

    ljCharlie

  2. #2
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    For example (cut from an app I work on):
    VB Code:
    1. Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
    2.     Handles MyBase.Closing
    3.             Select Case MsgBox("This character has changed since it was last saved.  Do you want to save changes before exiting?", _
    4.              MsgBoxStyle.YesNoCancel, "Warning")
    5.                 Case MsgBoxResult.Yes
    6.                     'Save first
    7.                     If Globals.strCharFileName = "" Then
    8.                         If Me.dlgSave.ShowDialog = DialogResult.OK Then
    9.                             Globals.SaveCharacter(Me.dlgSave.FileName)
    10.                         End If
    11.                     Else
    12.                         Globals.SaveCharacter(strCharFileName)
    13.                     End If
    14.                     Me.Close()
    15.                 Case MsgBoxResult.No
    16.                     'Quit without saving
    17.                     Globals.Dirty = False
    18.                     Me.Close()
    19.                 Case MsgBoxResult.Cancel
    20.                     'Aborting
    21.                     e.Cancel = True
    22.             End Select
    23.     End Sub

    ...although it has been pointed out that the .NET function is MessageBox.Show, you (and I also!) shouldn't be using msgbox any more. It works the same way though.

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    like this...
    VB Code:
    1. If MessageBox.Show("You've entered the wrong password", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
    2.             '/// do stuff because OK was clicked
    3.         End If
    or with the old vb6 method of MsgBox ...
    VB Code:
    1. If MsgBox("You've entered the wrong password", MsgBoxStyle.OKOnly) = MsgBoxResult.OK Then
    2.             '/// do stuff
    3.         End If
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Thumbs up

    Thank you very much! I didn't know that VB.NET change msgbox() to Message.Show(). And crucial point I missed is the DialogResult.OK or MsgBoxResult.OK.

    Well, I'll give that try. I'm very sure it will work.

    Again, thank you.

    ljCharlie

  5. #5
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    All DialogResult is is a built in enumeration of the different messagebox returns, you could just as easily use the number represented by each messagebox button (although I don't know what they are since I use DialogResult).

    Public Enum MsgBoxResult As Integer
    Member of: Microsoft.VisualBasic

    Summary:
    The MsgBoxResult enumeration contains constants used to identify which button was pressed on a message box displayed using the MsgBox function. These constants can be used anywhere in your code.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Yes, I remember using numbers in VB6 during my college years. However, it's quite hard to remember those numbers though. Well, it worked out okay now.

    ljCharlie

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    By the way, is there an equivalent event for the .lostfocus in VB.NET? I thouht there is the .Leave but it wasn't working. Here's my situation.

    In the txtUserName textbox, I set a KeyUp event to capture the Enter key. If the user hit the enter key, then move the focus to the txtPassword textbox. And in the txtPassword textbox I also setup a KeyUp event to catch the Enter key. If in the txtPassword the user hit the Enter key, then performclick on the btnOK button. Now the problem is, if the user type in the wrong password and the error messagbox popup and the user hit the Enter key again, the error messagebox kept on poping up. That is because the focus is still on the txtPassword textbox. So my question is, is there a way to lose the focus on that txtPassword textbox?

    ljCharlie

  8. #8
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    That would be a very good reason NOT to use the enter key to move to the next field...use tab.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    Thanks for the response, but almost 99% people don't use the Tab key. Normally people would use the Enter key. So I'm stock.

    ljCharlie

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Easy way:
    Check for Enter in the passwordBox keypress event, and call your error message box there if they fail. There's no reason to check it in the Leave event is there?

    Just to give you a more complex solution:

    In our production enterprise application, the login form returns a userNominee. This is a user that is attempting authentication. The calling nominee is returned back to a class subroutine that actually called the login form.

    If the user failed authentification, and has not attempted more than 3 times on this running instance, we create another login form, and they can try again.

    Perhaps you could go that route.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Thumbs up

    I see what you're saying now. I'll give that a try and see what happens. Thanks!

    ljCharlie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width