Click here to Skip to main content
14,332,555 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have 4 forms..

MAINFORM..

FORM1
FORM2
FORM3


In my mainForm .. I have 3 button and 1 panel...

I want to create application in which..

If i click button1.. PANEL will display form1..

if i click button2 .. panel will display form2..

if i click button3 .. panel will display form3...
Posted
Comments
Sergey Alexandrovich Kryukov 24-Sep-11 21:46pm
   
Why on Earth?! There are legitimate ways of navigation.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

This would be a misuse of System.Windows.Forms library and intended UI structure. A panel is designed to be hosted by a Form, nor a Form is designed to be hosted by any Control (including other Form).

[EDIT]

Even though it is technically possible to achieve using P/Invoked raw Windows API SetParent, it won't work through pure .NET code, as the properties System.Windows.Control.Parent and System.Windows.Control.Controls are specifically designed to throw an exception on an attempt to make a form a child of any control.


The properties System.Windows.Control.Parent and System.Windows.Control.Controls are specifically designed to throw an exception ArgumentException "Top-level control cannot be added to a control" on an attempt to make a form a child of any control. This can be worked around by setting Form.TopLevel to false, as Bill pointed out (see his comment below). If you try to do that, you will see that the non-client area of the child form is shown inside the parent control, so additional work around would be presenting a form without it:

Form child = ...
child.TopLevel = false;
child.ShowIcon = false;
child.FormBorderStyle = FormBorderStyle.None;
whateverPanel.Controls.Add(child);
//or child.Parent = whateverPanel;


(The same effect can also be achieved using P/Invoked raw Windows API SetParent, but I would not recommend it.
Using pure .NET API and avoiding P/Invoke is a very important factor in Forms programming. Using pure .NET keeps Forms application portable across many platforms (typically, under Mono, http://en.wikipedia.org/wiki/Mono_%28software%29[^], http://www.mono-project.com/[^]). A Form application can run on Linux, Mac and other systems without recompilation (I do it all the time), but a single P/Invoke will break such compatibility.
Anyway, there is simply no point in doing any of the above, see below.

[END EDIT]

Also, from the point of view or UI style, this would misuse a button, which should not be used for navigation purposes, because the button has no state reflecting "current position" in navigation.

The suggested behavior is simply never needed. The similar effect could be achieved if you replace FORM1, FORM2 and FORM3 with Panels. Instead of three buttons three list items or tree nodes could be used by a navigation-controlling ListBox or a TreeView. A selected panel could be made visible and other panels hidden using the property System.Windows.Forms.Control.Visible. However, using this navigation style when only three panels should be switched looks impractical (but could be very beneficial if the number items controlling switching panels would be so big that they might not fit the form). Instead, System.Windows.Forms.TabControl should be used. It's ready-to-use, convenient and require very little coding.

—SA
   
v5
Comments
BillWoodruff 25-Sep-11 4:40am
   
Adding to SA's final suggestion above: re: using a TabControl: you could make what are now your 'Forms' into UserControls, and put each UserControl into its own TabPage, or make each TabPage contain the Controls that are now on Forms 1~3.

best, Bill
BillWoodruff 25-Sep-11 4:58am
   
Hi SA,

While we both agree, I believe, that it is not a good idea to put a Form inside any other container, and we both agree that using MDI style WinForms architecture is no longer a good idea for many reasons (and is officially 'deprecated'), I must object, again, to your on-going mis-statements of fact about Windows Forms:

SAK: "Even though it is technically possible to achieve using P/Invoked raw Windows API SetParent, it won't work through pure .NET code, as the properties System.Windows.Control.Parent and System.Windows.Control.Controls are specifically designed to throw an exception on an attempt to make a form a child of any control."

Absolutely wrong. Here's proof:

// assume I have a Panel on a Form named 'ContentPanel'
// and this code is called at run-time in my WinForms project
Form f1 = new Form();
f1.TopLevel = false;

// this works !
//ContentPanel.Controls.Add(f1);

// and this works !
f1.Parent = ContentPanel;

f1.Show();
f1.BringToFront();

The effect of the above code, whether setting the Parent property of the newly created Form to an existing Control, or adding it to the ControlCollection of an existing Control is: to show the new Form inside the Panel.

There is nothing 'impure' about this example of .NET, although we'd both advise people not to do this.

disputatur in spiritu fratribus, Bill
Sergey Alexandrovich Kryukov 25-Sep-11 11:49am
   
Bill, thank you very much for fixing my mistake.

You're absolutely right, what I wrote was incorrect. I did not pay attention for TopLevel. The role of this property is not property explained on its help page http://msdn.microsoft.com/en-us/library/system.windows.forms.form.toplevel.aspx, but frankly, I simply never paid attention to it.

So, I've tested what you suggested, added some code, fixed my post above and credited you for this information in my solution. Thanks to you, now I know how it really works.

Best,
--SA
BillWoodruff 25-Sep-11 17:16pm
   
"All's well that ends well" Shakespeare
sumair_coolboy 25-Sep-11 12:26pm
   
Thx guys for the reply......

I m new in C sharp .net programming..

These days i m making Snake game...

Actually i want my application start with SNAKE GAME INTRO TILE ..
WHich have "NEW GAME" "OPTION" "HELP" three buttons..

When someone click NEW BUTTON ..GAME START ..


I knw how to link forms.. HIDE FIRST form and open new form..

But i think thats not a good idea... I m looking for something ..CHANGE forms under the same window...
Sergey Alexandrovich Kryukov 27-Sep-11 10:27am
   
What you change "under the same window" is not called a form, it's likely some other control, usually a container of other controls; otherwise everything's right :-)
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

This sounds like a design scenario where you could, also, consider using a TabControl: then make what are now your 'Forms' into UserControls, and put each UserControl into a TabPage, or make each TabPage contain the Controls that are now on Forms 1~3.

I mention this as an alternative to SA's suggestion above to use Panels.

best, Bill
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

I do agree with bill, Using tab page will do what you need.
You could use controls such as Janus, because it has some beautiful interfaces.
Best, Hamid
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100