Solved

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

Posted on 2011-03-07
17
1,550 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
  • 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
 
LVL 14

Accepted Solution

by:
systan earned 167 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 62

Expert Comment

by:Fernando Soto
ID: 35057424
Hi awilderbeast;

Are you looking to do something like this.

Fernando
ScreenShot002.png
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 167 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 166 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 62

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 18
Open form in the top right hand corner of screen 5 19
Get String split 5 33
Close word object 13 18
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now