Results 1 to 27 of 27

Thread: how to close form1 when i open form2?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    how to close form1 when i open form2?

    Can any one knows how to close form1 when i open form2, like when i heave a login form and when a log on i like to close login form!!!
    what code i need to write in form1 when i kcick the button "OK"?

  2. #2
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: how to close form1 when i open form2?

    VB Code:
    1. this.Close();
    2. Form2 newform = new Form2();
    3. newform.Show();

    I beleive
    Last edited by ..:RUDI:..; Dec 14th, 2006 at 09:20 AM.



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  3. #3
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    Quote Originally Posted by ..:RUDI:..
    VB Code:
    1. this.Close();
    2. Form2 newform = new Form2();
    3. newform.Show();

    I beleive
    It does not work. It will close both form because this code must be in Button_Click of form1 so that when you call This.Close() --> form1 will be Close with all of this code will be close, too.

    Any ideas?
    !Have fun!

  4. #4
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: how to close form1 when i open form2?

    You need to change the start up form to form2.

  5. #5
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Quote Originally Posted by fifo
    It does not work. It will close both form because this code must be in Button_Click of form1 so that when you call This.Close() --> form1 will be Close with all of this code will be close, too.

    Any ideas?
    VB Code:
    1. this.Hide();
    2. Form2 newform = new Form2();
    3. newform.Show();

    This way the form's still open (in theory), however calling application.exit(); to close everything, will close that form too.

  6. #6
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    As for you, this form had loaded in memory and you don't use it.
    I think you waste resources to store this form, anyway.

    better ways??
    !Have fun!

  7. #7
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Quote Originally Posted by fifo
    As for you, this form had loaded in memory and you don't use it.
    I think you waste resources to store this form, anyway.

    better ways??
    Not that I know of.

    Or perhaps on Form2:
    VB Code:
    1. Owner.Close();
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  8. #8
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    Oh, no

    I did follow your code But I got a error, you can see in my attach Image.
    Attached Images Attached Images  
    !Have fun!

  9. #9
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Quote Originally Posted by fifo
    Oh, no

    I did follow your code But I got a error, you can see in my attach Image.
    According to some tutorials that is the preferred method.

    I got the same error as you.

    Try digging into your mind
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  10. #10
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: how to close form1 when i open form2?

    read this: http://vbforums.com/showthread.php?t=313050
    its written for vb.net, but the ideas are the same...

    basically you want to declare the reference to the new form as a public variable, something which cannot be done inside a method.

    Code:
    public Form newform = null;
    
    public static void Main() {
    ...
    then you can use the code that was first posted (slightly modified):

    Code:
    // newform has already been declared as a public variable
    newform = new Form2();
    newform.Show();
    this.Close();
    since the new form is no longer a child of the loginform, it won't disappear when the loginform is closed.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  11. #11
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Great idea, and works perfectly.

    I see why it needs to be declared as a public form, but surely there must be another way. Because it seems that for each time we need a new form in a public variable in each form when we want to close a form.

    I've never really thought about it to be honest, just hidden the form and used Application.Exit(); on the close event.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  12. #12
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Ohh nevermind it's in the Program.cs file, d'oh.

    That's great I guess!
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  13. #13
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Quote Originally Posted by tr333
    read this: http://vbforums.com/showthread.php?t=313050
    its written for vb.net, but the ideas are the same...

    basically you want to declare the reference to the new form as a public variable, something which cannot be done inside a method.

    Code:
    public Form newform = null;
    
    public static void Main() {
    ...
    then you can use the code that was first posted (slightly modified):

    Code:
    // newform has already been declared as a public variable
    newform = new Form2();
    newform.Show();
    this.Close();
    since the new form is no longer a child of the loginform, it won't disappear when the loginform is closed.


    I've tried placing a public declaration of a form EVERYWHERE, and it just doesn't work.

    I've put it in a class, before a class, before main, and got a million errors.

    It works when I put it in a class, however because I have to recreate the class in the form, it closes that form and the current form also:
    VB Code:
    1. class variables
    2.     {
    3.         public string windowsuser;
    4.         public mainos osform = new mainos();
    5.     }

    In form1:
    VB Code:
    1. timer4.Enabled = false;
    2.             vars.osform = new mainos();
    3.             vars.osform.Show();
    4.             this.Close();
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  14. #14
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: how to close form1 when i open form2?

    i looked at one of my old projects as to how i did this, and it was surprisingly simple...

    Code:
    public System.Windows.Forms.Form LoginForm = new frmLogin // create new login form
    public System.Windows.Forms.Form MainForm; // don't create yet
    
    public static void Main() {
        MainForm = new frmMain; // create new main form
        Application.Run(LoginForm);
        Application.Run(MainForm);
        Application.Exit();
    }
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  15. #15
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    i looked at one of my old projects as to how i did this, and it was surprisingly simple...

    Code:

    public System.Windows.Forms.Form LoginForm = new frmLogin // create new login form public System.Windows.Forms.Form MainForm; // don't create yet public static void Main() { MainForm = new frmMain; // create new main form Application.Run(LoginForm); Application.Run(MainForm); Application.Exit(); }
    Maybe, you misunderstand this question.
    This question in here :
    Can any one knows how to close form1 when i open form2, like when i heave a login form and when a log on i like to close login form!!!
    what code i need to write in form1 when i kcick the button "OK"?
    It mean you creat a button in form 1 and click it to creat a form 1 and show form 2.

    I think we should referent var form1 to form2, and in form 2 we could call form1.close();

    Code:
    // In class form1 
    public form1 myform1 = new form1;
    void button1_Click(sender, e){
      public form2 myfrm2 = new form2;
      form2.show(myform1);
    }
    // In class form 2 
    // using declare form2(parameter form1)
    public void form2(form1 frm1){
            frm1.close();
            // do another thing;
    }
    better??
    !Have fun!

  16. #16
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: how to close form1 when i open form2?

    the code i posted previously was converted from a VB.NET project which has the code inside a module. i believe you could get the same effect by placing the code (main() method, etc) inside its own class since C# doesnt have modules.

    as an addon to the previous code, the button on the login form would just close the form (and possibly process some data from the form):

    Code:
    button1_Click(Object sender, EventArgs e) {
        this.close();
    }
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  17. #17
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: how to close form1 when i open form2?

    Quote Originally Posted by RudiVisser
    VB Code:
    1. this.Hide();
    2. Form2 newform = new Form2();
    3. newform.Show();

    This way the form's still open (in theory), however calling application.exit(); to close everything, will close that form too.
    the error with this code is that you should be declaring 'newform' with the type 'Form' not the type 'Form2:

    Code:
    this.Hide();
    Form newform = new Form2();
    newform.Show();
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  18. #18
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    Sorry, I don't totally understand your idea.

    Can you post whole code for this problem? Thanks
    !Have fun!

  19. #19
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Quote Originally Posted by tr333
    the error with this code is that you should be declaring 'newform' with the type 'Form' not the type 'Form2:

    Code:
    this.Hide();
    Form newform = new Form2();
    newform.Show();
    No, it's not.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  20. #20
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: how to close form1 when i open form2?

    Quote Originally Posted by RudiVisser
    No, it's not.
    Follow-up: Because there was no error..
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  21. #21
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: how to close form1 when i open form2?

    Quote Originally Posted by RudiVisser
    Follow-up: Because there was no error..
    (i thought i saw earlier in this thread that someone had an error with the code. didn't test it myself )
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  22. #22
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    :cry
    !Have fun!

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    18

    Re: how to close form1 when i open form2?

    My english is pure but i will try to explain by my words!
    I use VisualStudio 2003 and i am beginer in C# but this problem i solved with some tricks:
    form1 i make as container so main form
    in form 2 i put th login form
    when i open project in form 1 load i do this:i create new item class1 in this i declare one variable string but public, this variable in begining is null and in form 1 load i import the variable(Class1 a=new Class1()i put the "if( a.the_name_of_varable_of_class1==null) then if is that true do this Form2 f2=new Form2(); f2.show(); this.hide(); to hide form1.If is false i open the child of this MDI form. In beginin allvays the public variable is null so must go in form2.In form2 vhen i click ok if th username and pass egsist in database i put the code to show form1 and this form2 i close becasuse this don't close my aplication so is not a primary form.
    i excuze for this long text but in this way i solve my problem!
    Any idea!

  24. #24
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: how to close form1 when i open form2?

    Quote Originally Posted by fifo
    Sorry, I don't totally understand your idea.

    Can you post whole code for this problem? Thanks
    The code is written in VB.NET, but you should be able to understand it.

    VB Code:
    1. Module programModule
    2.  
    3.     Friend loginForm as New frmLogin
    4.     Friend mainForm as frmMain
    5.  
    6.     Friend Sub Main()
    7.  
    8.         mainForm = New frmMain
    9.         Application.Run(loginForm)
    10.         Application.Run(mainForm)
    11.         Application.Exit()
    12.  
    13.     End Sub
    14.  
    15. End Module

    What this code does, is the Main method for the program loads up the login form. When the login form is closed, the program will continue on and load up the main form. Since the form variables are declared inside a Module instead of a class, this allows them to stay active unlike if they were declared inside a Class. This could pose a problem since C# doesn't have modules, and i'm not quite sure how a VB Module should be implemented in C#.


    FYI: the code is from a small e-mail program i wrote a while ago (possibly contains bits of vb6 code here and there)...
    you can take a look at the code to see if i've missed anything important here.
    Attached Files Attached Files
    Last edited by tr333; Dec 20th, 2006 at 08:12 PM.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  25. #25
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    oh, I see.

    you mean Login form will be first and when you pass this form then Main Form will be shown.

    BUT as for me, the Main form and Login form should be shown together. Main form being like Background. I mean Login form is being Modalless.

    I think you code can not solve this problem (please see the first post again).
    Code:
    C# code
    
           theMainform = new Form1();
           theLoginform = new Form2();
           theLoginform.show(theMainform); // <--- Show form Modalless
    !Have fun!

  26. #26
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: how to close form1 when i open form2?

    Quote Originally Posted by fifo
    oh, I see.

    you mean Login form will be first and when you pass this form then Main Form will be shown.

    BUT as for me, the Main form and Login form should be shown together. Main form being like Background. I mean Login form is being Modalless.

    I think you code can not solve this problem (please see the first post again).
    Code:
    C# code
    
           theMainform = new Form1();
           theLoginform = new Form2();
           theLoginform.show(theMainform); // <--- Show form Modalless
    sorry i can't help you further with this. hope you get the problem solved
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  27. #27
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: how to close form1 when i open form2?

    :cry

    thanks so muck, anyway
    !Have fun!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width