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

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

LVL 1
awilderbeastAsked:
Who is Participating?
 
systanCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
awilderbeastAuthor Commented:

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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Carl TawnSystems and Integration DeveloperCommented:
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
 
Fernando SotoRetiredCommented:
Hi awilderbeast;

Are you looking to do something like this.

Fernando
ScreenShot002.png
0
 
Fernando SotoRetiredCommented:
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
 
awilderbeastAuthor Commented:
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
 
awilderbeastAuthor Commented:
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
 
awilderbeastAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
Fernando SotoRetiredCommented:
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
 
awilderbeastAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
awilderbeastAuthor Commented:
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
 
systanCommented:
>>that example is exactly what i needed, got it sorted now
Using the Panel? The one that I send to you? Whos example?
0
 
awilderbeastAuthor Commented:
thanks
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.