Question MessageBox Have to click 2 times to close

keigo

Active member
Joined
Oct 27, 2008
Messages
29
Programming Experience
Beginner
VB.NET:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

        Select Case MessageBox.Show("Please select an Item", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Case DialogResult.OK

                CheckBox1.Checked = False

        End Select
    End Sub

When user clicks the CheckBox1, the message box should pop up, but it should also disappear (the checkbox also unchecked) when user clicks OK, once. Now I am facing this issue whereby, user will have to click 2 times in order for the message box to disappear. I really do not understand. First click, it(messagebox) blinks -- together with the checkbox unchecked. But it will not close. Second click, it will disappear. Can we make it so we only need to click once and it achieves the result which it closes the message box and unchecks the checkbox?



Any idea is greatly appreciated. Thanks
 
Analyze what occurs:

1) You click the CheckBox1
2) CheckBox1.CheckedChanged occurs
3) The MessageBox appears
4) You click Ok on the MessageBox
5) You enter the case DialogResult.Ok
6) You execute CheckBox1.Checked = False
7) CheckBox1.CheckedChanged occurs (same as point "2")
and so on, the 3,4,5... repeat

I hope I make it clear because I'm french and english is not my "cup of tea" :D
 
Analyze what occurs:

1) You click the CheckBox1
1.1) CheckBox1.CheckedChanged occurs
1.2) The MessageBox appears

2) You click Ok on the MessageBox
2.1) As a result of the case DialogResult.Ok I entered
2.1.1) The program should execute CheckBox1.Checked = False



I have modified your message, Sehnsucht. Thank you for asking. The sequence above is what I am trying to accomplish. Is it clearer now? As for your part 7) I do not understand how you would have come into that sequence.


Thank you. :)


I forgot to mention, the CheckBox after 2.1.1) is clear, but I need to click OK 2 times, for the MessageBox to close.

1 time, the check cleared, message box still there.
2 time, the message box close.

I am trying to figure out how to click only once, so both the CheckBox is cleared and the Message Box would close.
 
Last edited:
It's not that you have to click the OK button twice on the MessageBox. It's that you have to click the OK button on two different MessageBoxes. Sehnsucht has already explained why. By setting the Checked proiperty back to False you're causing the CheckedChanged event to be raised a second time, so you're causing a second MessageBox to be displayed. That's why it "blinks as you say. That's the first MessageBox being dismissed and the second one being displayed.

Now, your code really doesn't make much sense. When the user checks a box you tell them to select and item and then you uncheck the box. What's the point of the box at all? You need to make some changes so there's no point providing a solution to this problem specifically when it probably won't apply when you change the code to actually do something useful.

So, please explain what you're trying to achieve with the CheckBox and Messagebox and let's see if we can come up with a complete solution.
 
Now, your code really doesn't make much sense. When the user checks a box you tell them to select and item and then you uncheck the box. What's the point of the box at all? You need to make some changes so there's no point providing a solution to this problem specifically when it probably won't apply when you change the code to actually do something useful.

Actually user has to check a radio button before checking that check box. That is how I come up with the idea that when user check the check box before selecting a radio button, I would pop up a message box asking them to select an item first(from the radio button).

The radio button shows the main product, while the check box is the add-on.(a smaller product in addition to the main--radio button)

I hope that makes a little sense now?

Thank you very much for looking into this.

Thank you.
 
To see if any radio has been checked (when no radio is checked initially) handle the CheckedChanged event for all radiobuttons and save this info to variable, example:
VB.NET:
Private radiochecked As Boolean

Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
    radiochecked = True 'any option has been checked
End Sub
This info can be used for validation of checkbox, though I would not use the Validating event here because I think it would be more confusing for user. I would also not use a messagebox, but use the ErrorProvider component to show the error icon and message next to the checkbox like this:
VB.NET:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If Not radiochecked Then
        Me.CheckBox1.Checked = False
        Me.ErrorProvider1.SetError(Me.CheckBox1, "You have to select an option first.")
    Else
        Me.ErrorProvider1.SetError(Me.CheckBox1, String.Empty)
    End If
End Sub
 
Hi JohnH,

Thank you so much for your help. But as I used your code, this part is not displaying.

Me.ErrorProvider1.SetError(Me.CheckBox1, "You have to select an option first.")

There is only the red exclamation mark. I have tried expanding the space of the check box I am using (for validation) but the text message is no where to be seen. Please tell me I am missing something so I can continue working on it. Thank you so much, John



To everbody who have replied, I am very thankful, just that I am rushing something else and did not reply as specifically to you does not mean I neglected your kind help.
 
Last edited:
The red blinking icon is where the user is supposed to point the mouse to see what the problem is, then the message will display as tooltip.
 
If the user MUST check a RadioButton before checking the CheckBox then why is the CheckBox event enabled? Set the Checked property of all three controls to False and the Enabled property of the CheckBox to False as well. That way the user simply isn't able to check the CheckBox first. You then use a single method to handle the CheckedChanged event of both RadioButtons and set the Enabled property of the CheckBox to True.
 
If the user MUST check a RadioButton before checking the CheckBox then why is the CheckBox event enabled? Set the Checked property of all three controls to False and the Enabled property of the CheckBox to False as well. That way the user simply isn't able to check the CheckBox first. You then use a single method to handle the CheckedChanged event of both RadioButtons and set the Enabled property of the CheckBox to True.

Yes, I think you are right. I am still digesting into how I am going to really change it. I would do it later. Thank you.
 
Back
Top