By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

how to close a form in VB.Net (VS 2005)

P: n/a
I am showing a form by ShowModal(). I put a button on that form. When a user
clicks the button a MsgBox will show with question "do you want to close?"
yes/no.
How to handle the situation:
- user clicks "yes" - the form closes
- user clicks "no" - nothing happens

I have problem with that. It seems that assigning property of
button.DialogResult wokrs ok only for the first time (in designer generated
code and any longer)
Aug 3 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If MessageBox.Show("do you want to close", Me.Text,
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) =
Windows.Forms.DialogResult.OK Then
'TODO : place your close code here
End If
End Sub

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
I am showing a form by ShowModal(). I put a button on that form. When a
user
clicks the button a MsgBox will show with question "do you want to close?"
yes/no.
How to handle the situation:
- user clicks "yes" - the form closes
- user clicks "no" - nothing happens

I have problem with that. It seems that assigning property of
button.DialogResult wokrs ok only for the first time (in designer
generated
code and any longer)
Aug 3 '08 #2

P: n/a
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If MessageBox.Show("do you want to close", Me.Text,
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) =
Windows.Forms.DialogResult.OK Then
'TODO : place your close code here
End If
End Sub

'TODO : place your close code here - that uis EXACTLY my question. What
shoulb be here to close the form???????
Me.Close() does NOT work - nothing happens

The form closes only when the DialogResult property of the clicked button is
set to DialogResult.OK

The problem is that even if I have written the code below:

'designer generated code
button1.DialogResult=DialogResult.Ok;
'end of designer generated code

and then:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If MessageBox.Show("do you want to close", Me.Text,
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) =
Windows.Forms.DialogResult.OK Then
button1.DialogResult=DialogResult.Ok;
Else
button1.DialogResult=DialogResult.No;
End If
End Sub

It always closes the form (even the user clicks "No")

That IS the problem

Aug 3 '08 #3

P: n/a

"Chris" <Ch***@discussions.microsoft.comkirjoitti viestissä
news:F4**********************************@microsof t.com...
>
The form closes only when the DialogResult property of the clicked button
is
set to DialogResult.OK

It always closes the form (even the user clicks "No")

That IS the problem
I'm not sure if I understood your problem correctly.

I tried this kind of scenario:

In Form1 I have a button and this code:
MsgBox(Form2.ShowDialog.ToString)

And in Form2 I have another button and this code:
If MsgBox("Close this form?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question) =
MsgBoxResult.Yes Then
Me.DialogResult = Windows.Forms.DialogResult.Yes
End If

My button in Form2 doesn't have any DialogResult set in design mode. Now it
works so that if I click yes Form2 returns Yes. If I click no Form2 stays
open and I can click the button again.

Is this what you wanted to achieve? If it is, just remove the DialogResult
property in design mode and assign it in code.

Hopefully you find this helpful.

-Teemu

Aug 3 '08 #4

P: n/a
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If MessageBox.Show("do you want to close", Me.Text,
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) =
Windows.Forms.DialogResult.OK Then
Me.Dispose(True) 'this close the form
Else
' the user click no
'so i suppose he do not want to close the prog
End If
End Sub
"Teemu" <ts*****@hotmail.comwrote in message
news:iJ*******************@reader1.news.saunalahti .fi...
>
"Chris" <Ch***@discussions.microsoft.comkirjoitti viestissä
news:F4**********************************@microsof t.com...
>>
The form closes only when the DialogResult property of the clicked button
is
set to DialogResult.OK

It always closes the form (even the user clicks "No")

That IS the problem

I'm not sure if I understood your problem correctly.

I tried this kind of scenario:

In Form1 I have a button and this code:
MsgBox(Form2.ShowDialog.ToString)

And in Form2 I have another button and this code:
If MsgBox("Close this form?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question) =
MsgBoxResult.Yes Then
Me.DialogResult = Windows.Forms.DialogResult.Yes
End If

My button in Form2 doesn't have any DialogResult set in design mode. Now
it works so that if I click yes Form2 returns Yes. If I click no Form2
stays open and I can click the button again.

Is this what you wanted to achieve? If it is, just remove the DialogResult
property in design mode and assign it in code.

Hopefully you find this helpful.

-Teemu
Aug 3 '08 #5

P: n/a

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
>I am showing a form by ShowModal(). I put a button on that form. When a
user
clicks the button a MsgBox will show with question "do you want to close?"
yes/no.
How to handle the situation:
- user clicks "yes" - the form closes
- user clicks "no" - nothing happens

I have problem with that. It seems that assigning property of
button.DialogResult wokrs ok only for the first time (in designer
generated
code and any longer)
The form itself has a FromClosing event an event just like a Button-click
event for a Button, but its for the form, which is the (lighting bolt) on
the form's Property page that shows all the events for the form. You find
the FormClosing event and you double click it to get the event established
in the code.

When the user clicks the X icon to close the form, it's going to firer the
FromClosing event where you ask the question. If the response is yes, the
closing of the form is done. If the response is no, the form closing is
cancelled and the form will not close.
Private Sub FormClosing(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

If MessageBox.Show("Are you sure to exit?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If

End Sub

Aug 3 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.