Results 1 to 5 of 5

Thread: [RESOLVED] Custom MessageBox returns result

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    42

    Resolved [RESOLVED] Custom MessageBox returns result

    I've created a custom MessageBox (a Form), that has buttons Yes and No.
    There is a function popUp() to show the MessageBox.

    I would call the MsgBox in a parent as follows:
    Code:
    CustomMessageBox.popUp()
    Now I want the message box to return a value:
    Code:
    Dim a as Intger = CustomMessageBox.popUp()
    But this should only return once the user clicks a button in the message box.

    My popUp sub:


    Code:
        Dim thread As New Threading.Thread(AddressOf GetResult)
    
        Public Function PopUp(Optional ByVal ErrorMessage As String = "", Optional ByVal Title As String = "", Optional ByVal ButtonType As ButtonType = ButtonType.OK, Optional ByVal MessageType As MessageType = MessageType.None)
            'The following 5 lines do the GUI stuff and shows the box.
            AddMessageText(ErrorMessage)
            AddTitle(Title)
            AddButtons(ButtonType)
            AddIcon(MessageType)
            Show()
            thread.Start()
            Return result
        End Function
    
        Private Sub GetResult()
            While (Not gotResult)
            End While
        End Sub
    gotResult is a Boolean = false. It the user clicks a button gotResult is changed to true.
    result is a integer containing the number of the button clicked.

    When I run this code, the result is returned, even if the user doesn't click a button. I understand why, because the main thread of the form is not "paused" while the user has not clicked a button.

    How can I pause the message box itself, untill a button was clicked?
    I've tried this:
    Code:
        Public Function PopUp(Optional ByVal ErrorMessage As String = "", Optional ByVal Title As String = "", Optional ByVal ButtonType As ButtonType = ButtonType.OK, Optional ByVal MessageType As MessageType = MessageType.None)
            'The following 5 lines do the GUI stuff and shows the box.
            AddMessageText(ErrorMessage)
            AddTitle(Title)
            AddButtons(ButtonType)
            AddIcon(MessageType)
            Show()
            While (Not gotResult)
            End While
            Return result
        End Function
    But now the hole message box is caputered in a loop and the message box is like disabled, because it waits for the loop to end, so the user can't click anything.

    The answer is maybe easy, but I can't think of anything.

  2. #2
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Custom MessageBox returns result

    You can use showDialog() instead of show(). ShowDialog() will pause the code until the form exits.
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    42

    Re: Custom MessageBox returns result

    ok. thank you.
    But how will I return a result when a button is pressed??

  4. #4
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Custom MessageBox returns result

    Where do you declare these?

    "gotResult is a Boolean = false. It the user clicks a button gotResult is changed to true.
    result is a integer containing the number of the button clicked."

    If result is declared at class level (outside any sub or function) it should be available to all your subs and functions. So, in your button_click procedures, simply assign a value to result. When the form is closed, code below showDialog() will start executing, and the value of result should still be available for you to return.
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    42

    Re: Custom MessageBox returns result

    Ahhhh. thank you very much.
    Worked 100%

Tags for this Thread

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