473,769 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DialogRe sult wokrs ok only for the first time (in designer generated
code and any longer)
Aug 3 '08 #1
5 4983
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
If MessageBox.Show ("do you want to close", Me.Text,
MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
Windows.Forms.D ialogResult.OK Then
'TODO : place your close code here
End If
End Sub

"Chris" <Ch***@discussi ons.microsoft.c omwrote in message
news:86******** *************** ***********@mic rosoft.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.DialogRe sult wokrs ok only for the first time (in designer
generated
code and any longer)
Aug 3 '08 #2
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
If MessageBox.Show ("do you want to close", Me.Text,
MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
Windows.Forms.D ialogResult.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.DialogR esult=DialogRes ult.Ok;
'end of designer generated code

and then:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
If MessageBox.Show ("do you want to close", Me.Text,
MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
Windows.Forms.D ialogResult.OK Then
button1.DialogR esult=DialogRes ult.Ok;
Else
button1.DialogR esult=DialogRes ult.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***@discussi ons.microsoft.c omkirjoitti viestissä
news:F4******** *************** ***********@mic rosoft.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.Sh owDialog.ToStri ng)

And in Form2 I have another button and this code:
If MsgBox("Close this form?", MsgBoxStyle.Yes No Or MsgBoxStyle.Que stion) =
MsgBoxResult.Ye s Then
Me.DialogResult = Windows.Forms.D ialogResult.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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
If MessageBox.Show ("do you want to close", Me.Text,
MessageBoxButto ns.OKCancel, MessageBoxIcon. Question) =
Windows.Forms.D ialogResult.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*****@hotmai l.comwrote in message
news:iJ******** ***********@rea der1.news.sauna lahti.fi...
>
"Chris" <Ch***@discussi ons.microsoft.c omkirjoitti viestissä
news:F4******** *************** ***********@mic rosoft.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.Sh owDialog.ToStri ng)

And in Form2 I have another button and this code:
If MsgBox("Close this form?", MsgBoxStyle.Yes No Or MsgBoxStyle.Que stion) =
MsgBoxResult.Ye s Then
Me.DialogResult = Windows.Forms.D ialogResult.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***@discussi ons.microsoft.c omwrote in message
news:86******** *************** ***********@mic rosoft.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.DialogRe sult 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(ByV al sender As System.Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing

If MessageBox.Show ("Are you sure to exit?", "Exit",
MessageBoxButto ns.YesNo, MessageBoxIcon. Question) = DialogResult.Ye s 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
122577
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 frmLogin();
1
1898
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 back ground. The easiest way to reactivate the application, is selecting it from the taskbar. When my app is deactivated, its from pushing a button that launches a form (in its own thread), I suspect the thread has something to do with it but...
5
7447
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 run if the form is open and the suer just closes access all together, is there a way around this? Thanks
4
5258
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 be asked if he/she wants to save the form (actually, I hope the user could decide if he or she want to add the new data to a database). I chose "prompt" in "Save" option of close macro. But there is no difference if I chose "yes" in "save"...
2
2803
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 register form, the menu form will close or hiden. I use the close () method, but it close everything (both forms) but if i use hide() method.. even when I close all the forms using the X button
6
5125
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. Can anyone tell me how I can do this? Thanks for any help Bob
3
10577
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 other one is Next button which i created to open another form "frmSummary" which displays back the records submitted. (i used the wizard to open form and find specific records to display. All these works superb until a review by users where they...
22
2496
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 the Orders form for a particular Customer). I have a button on the 2nd form that says "return to the first form" which really is just sitting beneath the 2nd form. When the button is clicked it simply closes the 2nd form so you can see the 1st form...
2
2086
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 design a combobox in a form such that when I scroll down I get to see both the ID's. (b) I have a (Close Form) button on a form. Let's say I am adding a new record from a form and I am half way filling up everything and suddenly if I change my...
0
1610
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 on this button, it runs a macro to close the form and open the main menu form. On the 'close' form it has the has the following: Object Type: form Object Name: DataEditInventory Save:Prompt From what I have read about the 'Save:Prompt',...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.