Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# winforms

Options
  • 09-02-2006 7:27pm
    #1
    Registered Users Posts: 361 ✭✭


    Im quite new to this so go easy on me. I have two simple winforms. A button on one I want linking to the second winform.. I have tried so much.. Just im just goin in sane now.

    I have form1.cs and form2.cs in the one project. This right?
    However It only allows me to enter in one program.cs.

    And in the FormDesigner when i double click on the button i entered this in the button click method.

    private void button1_Click(object sender, EventArgs e)
    {

    Form1 f2 = new Form1();
    f2.Show();

    }

    If I change form1 to form2 the program does not recognise it and I get an error (missing a directive or an assembly reference)

    Any help at all would be greatly appreciated. It probably is something small.

    Cheers.


Comments

  • Closed Accounts Posts: 35 Boco


    Redegg wrote:
    Im quite new to this so go easy on me. I have two simple winforms. A button on one I want linking to the second winform.. I have tried so much.. Just im just goin in sane now.

    I have form1.cs and form2.cs in the one project. This right?
    However It only allows me to enter in one program.cs.

    And in the FormDesigner when i double click on the button i entered this in the button click method.

    private void button1_Click(object sender, EventArgs e)
    {

    Form1 f2 = new Form1();
    f2.Show();

    }

    If I change form1 to form2 the program does not recognise it and I get an error (missing a directive or an assembly reference)

    Any help at all would be greatly appreciated. It probably is something small.

    Cheers.
    Have you got a link to your code anywhere? Technically what you are trying to do seems ok, but you are probably missing something important that you havent mentioned in the post. Are you sure both Form1 and Form2 are in the same project and not in two seperate projects within the same solution? If they are in seperate projects you will need to add a reference to Form2s project to Form1s project (and set any using statements as required).


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Like Boco said, what you are trying should work fine (assuming both forms are in the same project). Is the error you get a compile time or runtime one ?

    Also, I'm not sure what you mean by "However It only allows me to enter in one program.cs. " ?


  • Registered Users Posts: 361 ✭✭Redegg


    Thanks! But i still have a problem with the 2 program.cs files and cant have both in same project! Any help?


  • Registered Users Posts: 361 ✭✭Redegg


    Thanks! But i still have a problem with the 2 program.cs files and cant have both in same project! Any help?

    edit:
    steve, i mean when I add existing files(form 2.cs files) only one program.cs can exist with a project!


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    I think that could be where your problem lies, both should really be in the same project (you can use forms across different projects but it's best not to worry about that yet if you're just starting out). Sometimes Visual Studio seems to mess up projects, so it might be best to just scrap the one you have and create a new one. Create a new windows forms project in VS, then delete the form1 that VS automatically creates in it. Copy your form1 and form2 files (.cs, .resx and .designer.cs if applicable) into your new projects folder , right-click on the project in solution explorer and choose 'add existing', pick your form1 and form2 .cs files. Hopefully it'll let you add both.


  • Advertisement
  • Registered Users Posts: 361 ✭✭Redegg


    Yea worked! Now my problem is that when ever my new window opens the previous one remains on the screen. I want it to close as soon as my new one opens. I have searched the help option but all it gives is :

    Application.Close(); which closes the entire thing!?!

    Any idea how to close the previous window.?


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Look up the "this" keyword.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Form.Hide ( ) - might be what your after, it will just hide the form, but it'll still be in memory. If you want to completely shut it down then there should be a Close ( ) or similar function for the form that will completely destroy it (Form.Destroy ( ) perhaps will call its destructor ).

    MSDN should be able to help you out.


  • Registered Users Posts: 361 ✭✭Redegg


    Well. I have tried pretty much everything. Hide seemed to be the only one that seemed to do anything. It kinda went to load up the new window and then I got an exception. Ugh!


  • Closed Accounts Posts: 5,064 ✭✭✭Gurgle


    this.close();


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    I think your problem is because Form1 is your startup form. It then 'owns' Form2 when it's opened, so when Form1 closes Form2 gets closed aswell as Form2 only exists within Form1. What you need to do is either use another form to open Form1 and then Form2, or else have a main startup function ( public static void main(), or whatever the C# syntax for it is) to do the same.


  • Registered Users Posts: 361 ✭✭Redegg


    stevenmu wrote:
    I think your problem is because Form1 is your startup form. It then 'owns' Form2 when it's opened, so when Form1 closes Form2 gets closed aswell as Form2 only exists within Form1. What you need to do is either use another form to open Form1 and then Form2, or else have a main startup function ( public static void main(), or whatever the C# syntax for it is) to do the same.


    yea Form.Close does work but does not close the main window!

    Is it ok to hide the main window? So it gives the illusion of only one window. I do realise thats bad programming.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    I wouldn't do it unless you're showing form2 modally, i.e. using showdialog() instead of show(). Your application should really be closed from form1, so you'll need to unhide it when form2 is closed, there are other ways of doing it, but modal is probably the best.

    me.hide();
    f2.showdialog();
    me.show();

    If you are showing form2 modally you may not even need to hide form1 as the user can't go back to it untill they close form2 anyway.


Advertisement