Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 06-21-2006, 01:36 PM   #1 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default C# Form Close

Ok, I am using Visual Studio 2k5 Express Edition...
Now, if you've ever used it, you'll know it generates Form code VERY cleanly. This aside, I have the 'Start' Form that starts up from Main.
Then I have a button that leads to Form2. This button is supposed to:
-Close(not hide) the Start Form.
-Create a new instance of Form2.
Code? I tried[in start as the button is in that form]:
Code:
Form2 ff = new Form2();
ff.Show();
this.Close();
Then it compiled fine... but the button closed both the forms upon clicking.

-Storm
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Old 06-21-2006, 03:36 PM   #2 (permalink)
 
Join Date: Feb 2003
Posts: 33
Default

Well since Form1 is your mainform you cannot close it without closing the application. Thats the reason why both forms are closed. Even if you do not define Form1 as the parent form for Form2.

So you only can hide Form1.
The reason can be found in the hidden part of the partitial class of Form1.
Ther should be some line like.

void main()
{
Application.Run(new Form1());
}
swtrse is offline   Reply With Quote
Old 06-21-2006, 05:58 PM   #3 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
Default

Form2 ff = new Form2();
ff.Show();
this.Hidden = true;

try that
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 06-21-2006, 07:19 PM   #4 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by Sorious
Form2 ff = new Form2();
ff.Show();
this.Hidden = true;

try that
This would work but isn't what he said he wanted,

Quote:
Originally Posted by Storm33229
-Close(not hide) the Start Form.
Now, what could be done to do this is to only have a this.Close(); statement in the Button's OnClick, then have Application.Run(new Form2()); in the Main() method after the Application.Run(new Form1());

Of course, some trickery would need to be involved if you wanted this to happen only when the Button is used and not when the form is closed in some other way, but this is a (hackish) way to do this.
Sep102 is offline   Reply With Quote
Old 06-22-2006, 12:37 AM   #5 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default

ok but if I hide it and I have a "Back" button, then i dont want to create a new instance of Form1.
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Old 06-22-2006, 01:56 AM   #6 (permalink)
Forum Expert
 
mordero's Avatar
 
Join Date: Nov 2003
Location: Illinois, USA
Age: 21
Posts: 2,911
Default

then the back button could just close/hide the second form and make Form1 visible...
mordero is offline   Reply With Quote
Old 06-22-2006, 06:32 AM   #7 (permalink)
 
Join Date: Feb 2003
Posts: 33
Default

Quote:
Originally Posted by Sep102
Now, what could be done to do this is to only have a this.Close(); statement in the Button's OnClick, then have Application.Run(new Form2()); in the Main() method after the Application.Run(new Form1());

Of course, some trickery would need to be involved if you wanted this to happen only when the Button is used and not when the form is closed in some other way, but this is a (hackish) way to do this.
It's not recommendet to manualy change code generated by designers, because the designers changes the code back sometimes.

Well since you only do not want to not make a new instance why not call Form2 as a modal dialog with that code (pseudocode)
Code:
funktion showmeForm2()
{
    HideForm1();
    ShowModalDialog(Form2);
    UnhideForm1();
}
This way if you call Form2.Close() within Form2, Form1 will automatically reapier without creating a new instance.

Since If you want to hide/clode Form1 I see no need to use an unmodal dialog.

Last edited by swtrse; 06-22-2006 at 06:35 AM.
swtrse is offline   Reply With Quote
Old 06-22-2006, 07:31 PM   #8 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by swtrse
It's not recommendet to manualy change code generated by designers, because the designers changes the code back sometimes.
Really, if you want to be fair about that statement, you shouldn't be changing any of the code for the class it derives for you from the Form class to represent your GUI, since that's also all generated code (especially before, when there were no partial classes, whew, scary). Also, as far as I know, none of the code in the Main method are ever generated or modified by the designer, only the code that actually has to do with GUI that the designer is editing.

Its parts that are not marked as being designer generated code most likely have about as much chance of being arbitrarily changed by the designer as the Main method does (note that I'm talking about logic, not about the identifiers). The only parts that one would really have to worry about being changed would be the code in the (FileName).Designer.cs part that (FileName).cs is split into, and even in that the code that is routinely changed is marked off by a region (except for the field declarations).

Anyways, I never once said I would recommend doing it that way, only that it was a way that could accomplish what he wanted in some fashion, I even acknowledged that it was a hackish way to do it, I was just presented some form of a solution that met his restrictions in his first post.

Last edited by Sep102; 06-22-2006 at 07:43 PM.
Sep102 is offline   Reply With Quote
Old 06-23-2006, 02:22 AM   #9 (permalink)
 
Join Date: Feb 2003
Posts: 33
Default

Wellwtih designer generated code I was refering to
.NET 1.0+1.1: the region within the generated class telling you that this is gernerated code and should not be changed.
.NET 2.0: parts of partitial classes or clases marked with the GeneratedCodeAttribute.

Hope that makes it clear.
swtrse is offline   Reply With Quote
Old 06-23-2006, 01:07 PM   #10 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by swtrse
Wellwtih designer generated code I was refering to
.NET 1.0+1.1: the region within the generated class telling you that this is gernerated code and should not be changed.
.NET 2.0: parts of partitial classes or clases marked with the GeneratedCodeAttribute.

Hope that makes it clear.
Neither of which contains the Main method, which is where I was talking about changing, I just wanted to point that out as you seemed to be saying that it would be wrong to edit it as the designer would possible change it. This was all that I was attempting to point out. Perhaps you just meant it as a general guide earlier when you stated that you should not change the designer generated code? Unless of course you were talking about my use of the Button's OnClick method, however that is not part of the code that the designer changes and is not marked.
Sep102 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5