I know this is just a simple question but I could hardly find the value for the buttons in msgbox for code execution.I tried to use codes I did in vb6 and was able to execute the codes properly but when I started vb2008 I encountered logical error for these codes.Let me take you alook for the codes: ******************************* MsgBox("Do you want to delete the records?",MsgBoxStyle.YesNo) If vbYes = 6 Then
-----Statement1-------
Else
-----Statement2-------
End If ******************************** When you click the yesbutton it executes the Statement1 but if you click the nobutton it should only execute Statement2.
Questions: 1.Why is it when you click the nobutton it would still execute the Statement1 instead of Statement2?
2.What would be the right If..Then...Else statement should be used to execute it properly? | | Mitkram Friday, October 16, 2009 5:24 AM |
It's generally better to assign the result of the dialogbox to a variable and check the variable. That way if you decide later that you want to add a further option, such as Cancel you don't have to rewrite the routine.
You should also always use the DialogResult enumeration for the results, forget about using the values, they just make your code hard to understand.
So I would recommend a combination of the two methods Joe has shown you.
Dim Result As DialogResult = MessageBox.Show("Click a button" , "Test" , MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Select Case Result
Case Windows.Forms.DialogResult.Yes
' do something
Case Windows.Forms.DialogResult.No
' do something else
End Select
That's the ticket to ride.
Mark the best replies as answers. "Fooling computers since 1971." - Marked As Answer byMitkram Saturday, October 17, 2009 8:32 AM
- Unmarked As Answer byMitkram Saturday, October 17, 2009 8:32 AM
- Marked As Answer byMitkram Saturday, October 17, 2009 8:33 AM
-
| | Rudedog2 Friday, October 16, 2009 7:32 PM | the Yes button enumeration is 6, so you are saying if 6 = 6, which is, of course, always True here are 2 ways to do it, either using Yes or 6 note the different way of showing the MessageBox in VB.NET
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If MessageBox.Show("Click a button", "Test", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("you clicked yes")
Else
MessageBox.Show("you clicked no")
End If
'or this way
If MessageBox.Show("Click a button", "Test", MessageBoxButtons.YesNo) = 6 Then
MessageBox.Show("you clicked yes")
Else
MessageBox.Show("you clicked no")
End If
End Sub
or you can create a variable of type DialogResult like this (and as above, you can use the VB enumeration 6 or the Windows.Forms.DialogResult = OK)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim result As DialogResult
result = MessageBox.Show("click a button", "test", MessageBoxButtons.YesNo)
If result = 6 Then
MessageBox.Show("you clicked Yes")
ElseIf result = 7 Then
MessageBox.Show("you clicked No")
End If
End Sub
- Proposed As Answer byDave299 Friday, October 16, 2009 9:58 AM
-
| | jwavila Friday, October 16, 2009 5:37 AM |
I know this is just a simple question but I could hardly find the value for the buttons in msgbox for code execution.I tried to use codes I did in vb6 and was able to execute the codes properly but when I started vb2008 I encountered logical error for these codes.Let me take you alook for the codes: ******************************* MsgBox("Do you want to delete the records?",MsgBoxStyle.YesNo) If vbYes = 6 Then
-----Statement1-------
Else
-----Statement2-------
End If ******************************** When you click the yesbutton it executes the Statement1 but if you click the nobutton it should only execute Statement2.
Questions: 1.Why is it when you click the nobutton it would still execute the Statement1 instead of Statement2?
2.What would be the right If..Then...Else statement should be used to execute it properly?
i agree completely with "jwavila".... thats the way of soing it... n if ur hving problems determing the the number that should come in the statement "if result = " then try this... if result = vbYes Then or if result = vbNo then like dat.. if ur using the visual studio od the visual basic explress editions.. the code editor will help u do dat.. jst try it.. hope it works...
theres nothing better than prime nos...!! | | kool_Pragy Friday, October 16, 2009 7:37 AM | It's generally better to assign the result of the dialogbox to a variable and check the variable. That way if you decide later that you want to add a further option, such as Cancel you don't have to rewrite the routine.
You should also always use the DialogResult enumeration for the results, forget about using the values, they just make your code hard to understand.
So I would recommend a combination of the two methods Joe has shown you.
Dim Result As DialogResult = MessageBox.Show("Click a button", "Test", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Select Case Result
Case Windows.Forms.DialogResult.Yes
' do something
Case Windows.Forms.DialogResult.No
' do something else
End Select
- Proposed As Answer byRudedog2 Saturday, October 17, 2009 12:21 PM
-
| | Dave299 Friday, October 16, 2009 9:58 AM |
It's generally better to assign the result of the dialogbox to a variable and check the variable. That way if you decide later that you want to add a further option, such as Cancel you don't have to rewrite the routine.
You should also always use the DialogResult enumeration for the results, forget about using the values, they just make your code hard to understand.
So I would recommend a combination of the two methods Joe has shown you.
Dim Result As DialogResult = MessageBox.Show("Click a button" , "Test" , MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Select Case Result
Case Windows.Forms.DialogResult.Yes
' do something
Case Windows.Forms.DialogResult.No
' do something else
End Select
That's the ticket to ride.
Mark the best replies as answers. "Fooling computers since 1971." - Marked As Answer byMitkram Saturday, October 17, 2009 8:32 AM
- Unmarked As Answer byMitkram Saturday, October 17, 2009 8:32 AM
- Marked As Answer byMitkram Saturday, October 17, 2009 8:33 AM
-
| | Rudedog2 Friday, October 16, 2009 7:32 PM | Thanks guys,all answers are correct,all of you are such a great help. I greatly appreciated your response.
| | Mitkram Saturday, October 17, 2009 8:35 AM | If all answers were correct, which they are not, then you should mark them all and not mark the one response which didn't contain an answer. | | Dave299 Saturday, October 17, 2009 10:08 AM |
If all answers were correct, which they are not, then you should mark them all and not mark the one response which didn't contain an answer.
You see, this is exactly what I was talking about on this thread. OPs marking as answer a side/mere remark referencing another (correct) answer while bypassing the response that answered their question. @Mitkram, It is Dave299 that answered your question. Pls mark his response as the answer because this board serves as reference material for millions of programmers accross the globe. They need to be properly guided when they reference this thread and others.
Only performance counts! | | Sylva Saturday, October 17, 2009 12:17 PM |
|