Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# windows form, open a form from another form in same window

Posted on 2011-03-07
17
Medium Priority
?
2,005 Views
Last Modified: 2013-12-17
hi all, just creating my first windows app have the below code

only problem  is that it opens the form in a new window, i would like to have the form open in the same window

that possible?

Thanks
void _CompaniesFrm(object sender, ExecuteEventArgs e)
        {
            App.Companies.FrmCompanies obj = new App.Companies.FrmCompanies();
            obj.Show();
        }

Open in new window

0
Comment
Question by:awilderbeast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +3
17 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35057178
No. Forms in a Windows application are seperate entities, they don't all draw within the same window as you would with a web page.

You can house the forms in an MDI window if you wanted to, but each seperate window would still display separately.
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35057196

so an MDI window would give the apperance of them looking like their in one window though?

child forms go in the same window though dont they?

Thanks
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35057302
The MDI window acts as a parent for the child windows. So each child will appear within the bounds of the MDI parent form, but they will still be separate windows.

This is worth a read to help clarify things:

    http://www.c-sharpcorner.com/UploadFile/ggaganesh/DevelopingMDIAppplicationsinCSharp11272005225843PM/DevelopingMDIAppplicationsinCSharp.aspx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
systan earned 668 total points
ID: 35057417
I have created a small project for you to open a form from another form, using the panel.
try to see it.
just open first;
newFormFromForm.csproj
then ran to see it, then look at the code.
 newFormFromForm.zip
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35057424
Hi awilderbeast;

Are you looking to do something like this.

Fernando
ScreenShot002.png
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 668 total points
ID: 35057560
Hi awilderbeast;

If so you will need to use a container control to hold the new form otherwise it will consume as much space as it needs. In my sample I placed the new form within a Panel control with AutoScroll on and placed the following code on the button click event of the button marked "Create New Form In Panel" :

// New Form to be placed in the Panel
// control of another form
NewForm nf = new NewForm( );
// Need to set to false otherwise you
// will not be able to add it to a control
nf.TopLevel = false;
// Add the new Form as a control to the Panel control
panel1.Controls.Add( nf );
// Then Show it.
nf.Show( );

This can be done because a Form is just another control.

Fernando
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35057598
that example is exactly what i needed, got it sorted now

is there a way i can hide/remove the title bar and menu so it looks like its the same page?
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35057711
also is there a way i can change main forms

im guessing close one open another, but is there a way of that happening without screen flicker?

Thanks
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35057902
i ahve the below but its being cheeky and showing up still

in form load i did the below both ways round (as i read it makes a difference) and it comes back with it almost but not quite each way
this.FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;

Open in new window

almost.PNG
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 664 total points
ID: 35058079
I think what you're describing is to switch between forms - e.g. a user clicks a button, Form1 is replaced by Form2.  You can achieve that by, in the appropriate button's Click event handler, Hiding Form1 and showing Form2.  And in Form2's "FormClosing" event make sure you make Form1 visible again.

There are other methods that can do basically the same thing, for example you can implement your own ApplicationContext and make it such that your application to continues to run so long as any form is open, which means you're free to .Close() and .Show() forms all you want.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.Hide();
			new Form2().Show();
		}
	}
}

Open in new window

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public partial class Form2 : Form
	{
		public Form2()
		{
			InitializeComponent();
		}

		private void Form2_FormClosing(object sender, FormClosingEventArgs e)
		{
			Application.OpenForms["Form1"].Show();
		}
	}
}

Open in new window

tgerbert-417119.flv
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35058138
Hi awilderbeast;

To remove the title bar us the property FormBorderStyle  as shown below

            NewForm nf = new NewForm( );
            nf.TopLevel = false;
            // Remove Border / Title bar from the form
            nf.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            panel1.Controls.Add( nf );
            nf.Show( );

Open in new window


Fernando
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35058531
i dont like the animation between forms, i would like to click and it just be there, without looking like one is openeing and closing

can an application context do this?

Thanks

also thanks to fernado for the hiding border :)
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35058592
If the forms aren't too complex, another option is to use one form with 2 panels on it.  Put all the controls for the first form inside panel1, and all the controls for the second form inside panel2, put the two panels on top of each other, then change their visibility.

Alternatively use Show()/Hide() instead of Open()/Close()
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35058675
ive tried show hide and open close, they both have animation on them instead of just a static transfer

the problem is im using windows Ribbon for WinForms, and i need a different menu ribbon depending on which section, but im not keen on the crappy animation that happens when you open close, show/hide a form :S
0
 
LVL 14

Expert Comment

by:systan
ID: 35065073
>>that example is exactly what i needed, got it sorted now
Using the Panel? The one that I send to you? Whos example?
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35083781
thanks
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35464695
Hi, first you create one mdi form then when you click on the button of that form at that time write this code

childform objch = new childform();
objch.MdiParent = this.MdiParent;
objch.show();

it is working properly.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question