Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello there!

I am trying to open a form inside of a form I have 2 forms in my Solution Explorer
Form1 = main form
Form2 = the one I try to make pop up when pressing (Options) in the menu strip
but when I try to declare the Form2 in my code it doesnt want to show up

C#
Form2.Show();


doesnt want to to work, sorry for lack of information. In a hurry, just looking for quick fixes. any tips? If you need any more information please do tell me.
Posted
Updated 10-Aug-21 9:30am
v2

MenuStrip?
Double click on a item and simply add this code:
C#
Form2 nForm = new Form2();
nForm.TopLevel = false;
nForm.Show();
 
Share this answer
 
Comments
BladeLogan 14-Nov-15 18:15pm    
Hey! That worked, had to change the false to true, could you explain hopw that code works more in depth?
ry sieł 14-Nov-15 18:38pm    
Top-level form is the form that haven't a parent form :) If nForm toplevel = false it haven't parent, otherwise it have parent form :) If nForm.TopLevel = true - nForm parent is Form1 :)
BladeLogan 14-Nov-15 18:44pm    
Oh okay! That makes sence! What about the nForm, WHy are we calling it (nForm) (Wich I assume is short for newForm) why not Form2 wich is the name of my other form that I wanted to make pop up, is it just beacse that name is already used by something else? I hope I'm making any sence :p
ry sieł 14-Nov-15 19:08pm    
Oh. From beggining...
You have:
Form1
Form2
MenuStrip on Form1
And when the item in MenuStrip is clicked you want to Form2 pop up?
If yes, you must set Form2 top-level to false, and show Form2
Like this:
<pre>
Form2.TopLevel = false;
Form2.Show();
</pre>
Yes?
You must set, the Form2 haven't got any parent. And show this outside the Form1. It's all :)
BladeLogan 14-Nov-15 19:11pm    
Ooh okay! I am using Form2.TopLevel = true; at the moment, but if I want a child item lets say I have 3 stuff in the MenuStrip
File | Options | About
Exit | Themes

Lets say I want to make themes clickable with another form, THEN I use Form2.TopLevel = false; right?
otherwise if its not the child item, if it is the parent I would use
Form2.TopLevel = true; am I correct?

By the way, thank you for explaining!
Assuming 'Form1 is your Main Form:
C#
private Form2 form2 = new Form2();

private void Form1_Load(object sender, EventArgs e)
{
    form2.Show();
}
Of course, this is a guess that you have failed to create an instance of 'Form2 to 'Show; however, not creating instances is a frequent mistake of people new to C#.
 
Share this answer
 
Assuming this is your menu strip you want to load the net form from:

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
    Form2 nextForm = new Form2();
    nextForm.Show();
}


You need an object reference set to an instance of an object. By creating a variable called nextForm, we are doing just that.
 
Share this answer
 
v2
Comments
Dave Kreskowiak 10-Aug-21 16:22pm    
I seriously doubt the OP is still looking for an answer six years later.

Also, your answer is really no different from Solution 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900