Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,533 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,788 people online right now. Registration is fast and FREE... Join Now!




Close a form

 
Reply to this topicStart new topic

Close a form

Korupt
21 Aug, 2008 - 03:19 PM
Post #1

D.I.C Head
Group Icon

Joined: 22 Jun, 2008
Posts: 58



Thanked: 1 times
Dream Kudos: 25
My Contributions
What I need to do is basically read some info from the my first form and then into my second third form (second one is for something else) and once I get that info close the first form, this is how Im reading from form to form:

csharp

//MainCalc is the third form and LogIn is the first form
public MainCalc(LogIn form)
{
InitializeComponent();

Owner = form;
_form = form;
}
private LogIn _form = null;


then I'm reading like this:

csharp
        private void MainCalc_Load(object sender, EventArgs e)
{
string account = _form.textBox1.Text;
string name = "Hello, ";
string positon = "Position: ";
string departament = "Departament: ";
TextReader reader = new StreamReader(Application.StartupPath + "\\Users\\" + account + "\\" + "Info.txt");
while (reader.Peek() != -1)
{
reader.ReadLine();
name += reader.ReadLine();
position += reader.ReadLine();
departament += reader.ReadLine();
}
}


Now once all that is read how would I be able to close the first form i tried _form1.Close() but that closes everything also tried to make it on the closing even but that gives me an error:

csharp
        private void MainCalc_FormClosing(object sender, FormClosingEventArgs e)
{
_form.Close();
}


the error is (not a compiler one, I'm getting it once I try to close the form): An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll


This post has been edited by Korupt: 21 Aug, 2008 - 04:12 PM
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Close A Form
21 Aug, 2008 - 03:49 PM
Post #2

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
You can try
CODE

//Change Form1234 to the Form that you want to close
Form1234 myForm = new Form1234();
//Close the Form
myForm.Close();


Hope this helps
User is online!Profile CardPM
+Quote Post

Korupt
RE: Close A Form
21 Aug, 2008 - 04:00 PM
Post #3

D.I.C Head
Group Icon

Joined: 22 Jun, 2008
Posts: 58



Thanked: 1 times
Dream Kudos: 25
My Contributions
nah that doesn't work because you create a new instance of the form and then just close that newly created instance, but what I need has changed a bit I'll edit
User is offlineProfile CardPM
+Quote Post

nonggiatu
RE: Close A Form
21 Aug, 2008 - 07:25 PM
Post #4

New D.I.C Head
*

Joined: 7 Jun, 2008
Posts: 3


My Contributions

I think the cause of that problem is the MainCalc form have a reference to a control on LogIn form (the text box). But when you close LogIn form, all controls on It are disposed, so the reference of textBox1.Text in MainCalc form is disposed.
You should use delegate to solve this situation.
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Close A Form
21 Aug, 2008 - 09:20 PM
Post #5

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
QUOTE(Korupt @ 21 Aug, 2008 - 05:00 PM) *

nah that doesn't work because you create a new instance of the form and then just close that newly created instance, but what I need has changed a bit I'll edit


Here Try this instead

CODE

//Change Form1234 to the Form that you want to close
Form1234 myForm = Form1234();
//Close the Form
myForm.Close();


Hope this helps


User is online!Profile CardPM
+Quote Post

zakary
RE: Close A Form
22 Aug, 2008 - 05:53 AM
Post #6

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 405



Thanked: 6 times
Dream Kudos: 175
My Contributions
Korupt where are you calling the 2nd form.Show(). It seems to me that what you need to do is initialize the 2nd form pass your data to it then show the 2nd form using form.Show() not form.ShowDialog(). ShowDialog will lock Form one until form2 closes, after you call form.Show() call form.Close();

This post has been edited by zakary: 22 Aug, 2008 - 05:54 AM
User is offlineProfile CardPM
+Quote Post

Korupt
RE: Close A Form
22 Aug, 2008 - 10:55 AM
Post #7

D.I.C Head
Group Icon

Joined: 22 Jun, 2008
Posts: 58



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(nonggiatu @ 21 Aug, 2008 - 08:25 PM) *

You should use delegate to solve this situation.


an example perhaps?

@gbertoli3 - that doesn't compile because it's using a class as a variable

@zakary - tried it, same thing happens

thanks for replaying everyone


User is offlineProfile CardPM
+Quote Post

djkitt
RE: Close A Form
22 Aug, 2008 - 11:38 AM
Post #8

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
If you just want the Login Form to close when the MainCalc form closes then just close it after MainCalc returns from a ShowDialog:

CODE

            MainCalc mc = new MainCalc(this);
            mc.FormClosed += new FormClosedEventHandler(mc_FormClosed);
            this.Visible=false;
            mc.ShowDialog();
            this.Close();


If you need something more invlolved let me know the details...

Hope this helps,

Kitt
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:14PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month