473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
5 4966
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
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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vivek Sharma | last post by:
How can I close a form in C#? Here is the code that I am using but closes everything? private void frmSplash_Click(object sender, EventArgs e) { this.Close(); Form frmLogin = new...
1
by: dwilliamjoe | last post by:
VB.net, launch procedure, close form, the whole APP is deactivated. When I say deactivated, other applications (Wind Explorer, My computer, Ect...) come to the foreground and my app goes to the...
5
by: AP | last post by:
IS there a way to run a procedure if the users close access directly rather than closing a menu screen that I have built? There is an event that works on close for this form, but it doesnt seem to...
4
by: Mindy | last post by:
I have two questions here: (1) what is the difference of close form and close table? (2) How "Prompt" works I used close form macro in my database. I hope when a user close the form, he/she will...
2
by: Claudia Fong | last post by:
Hello everybody, I have a Menu form where I have a button. The user should click the button and the program should open another form call register form. I want that when the program show the...
6
by: Robert Dufour | last post by:
On my form if the user clicks the upper right hand corner to close the form I want to trap that event and do a check to see if closing is allowed, if not, I want to stop the form closing action. ...
3
by: kev | last post by:
Hi folks, I have a form for registration (frmRegistration) whereby i have two buttons. One is Save which saves record using the OnClick property. I used wizard to create the save button. The...
22
AccessIdiot
by: AccessIdiot | last post by:
Hello all, I have a form (frm_Entrainment) with a button that opens a 2nd form (frm_Specimen_Entrainment). The 2nd form shares an ID field with the first form (Entrainment_ID - its like opening...
2
by: Hulas | last post by:
Guys, I have two questions. (a) How do I add two fields of the same table to a single combo box. For example if I have two fields ID1 and ID2 in a table called Identification, than how should I...
0
by: ncsthbell | last post by:
I have an MS2007 access database. On one of the forms that is used to edit/change data, there is a button 'Return to main menu'. Once the user has finished making the changes on the form and clicks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.