Add userControl2 to Panel2 from userControl1 button in Panel1 - c#

How to get userControl2 from button click on userControl1 into panel2?
Pressing the button New shows userControl1 control on Panel1, but I want to show usercontrol2 by pressing userControl1 button to panel2 userControl2. I couldn't do it.
private void btnNewDay_Click(object sender, EventArgs e) {
frmMain main = new frmMain();
main.panel2.Controls.Clear();
userControls.ucNewDay newDay = new userControls.ucNewDay();
main.panel2.Controls.Add(newDay);
main.userControlcontrol = true;
}

You're creating a new form with new frmMain();. What you need is to get a handle of the current form. You can do that by simply using the this keyword, but this is even optional and you don't have to use anything. However, in a user control, this refers to the control. In this case use the Parent property.
Change the first line:
frmMain main = new frmMain();
to:
frmMain main = (frmMain)this.Parent;
Alternatively, you can use the FindForm() method:
frmMain main = (frmMain)this.FindForm();

Related

Opening a Form from Another Form C#

I have forms 'Form1','Form2' and a mdi. Mdi contains a splitcontainer. In split container there is two panels first one for menu and the other for displaying forms when we click on the menu. My issue is I want to call Form2 from Form1 when I click on an icon in the Form1. I wrote the bellow code in Form1 's icon click. But the Form2 is not showing. I wrote another alternative code like Form2.show() in this case Form2 is displaying but not fit in the panel2 of the Mdi. It is displaying like a popup.
This is the code that I wrote in the Form1 icon click.
private void icon_Click(object sender, EventArgs e)
{
this.Close();
Form2 obj2 = new Form2 ();
obj2 .Show();
obj2 .Location = new Point(0, 0);
obj2 .TopLevel = false;
Mdi Objmdi = new Mdi();
Objmdi.splitContainerControl1.Panel2.Controls.Add(obj2); Objmdi.splitContainerControl1.Panel2.Controls["Form2"].BringToFront();
}
Create an object reference to form 2 in the button_click event and call the 'show' function.
Form1 main = new Form1();
main.Hide();
Form2 second = new Form2();
second.Show();
second.Width = this.Width;
second.Height = this.Height;
second.StartPosition = FormStartPosition.Manual;
second.Location = new Point(this.Location.X, this.Location.Y);
this.Visible = false;
If you want your Form2 to be displayed in some panel instead of a new window then you should create a UserControl instead of a Form and then you should be able to call Objmdi.splitContainerControl1.Panel2.Controls.Add(myNewUserControl);
When a form is closed, all resources created within the object are closed and the form is disposed.
So when you call this.Close(); code after this is not executed.
Change it to this.Hide();.
OR
Change it to this.Hide(); and pass Form1's reference to Form2, which should close it.
Also,
You are creating a new MDI in the below line:
Mdi Objmdi = new Mdi();
Objmdi.splitContainerControl1.Panel2.Controls.Add(obj2); Objhome.splitContainerControl1.Panel2.Controls["Form2"].BringToFront();
Instead of creating a new MDI use existing MDI's reference to do this operation.
Thirdly, Controls["Form2"] try changing to Controls["obj2"]

C#, WinForm in another winform

I need help. I have winform, like one which is shown on picture, so I want to improve next things, when I click on first button, on the right side will be showed content from another winform, is this possible? I don't want to use panels.
Why don't you just embed a single UserControl dynamically to represent the current page or if you want to do everything at design-time - a custom TabControl?
showed content from another winform
You generally don't embed a popup window into another, rather controls. Otherwise you have to deal with hiding Minimise, Maximise, Close etc.
MDI Form is the best solution for you.
Use: this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;`
to make your child Forms more beautiful in your project!
If you want the MDI parent to auto-size the child form you can code like this.
Code Example:
private void Form1_Load(object sender, EventArgs e)
{
IsMdiContainer = true;
}
private void btnHotel_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.MdiParent = this;
}
private void btnCaffe_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm3.MdiParent = this;
}
You can use TabControl control -> for each specific content you want to display you just have to add a TabPage to TabControl and when a specific menu option is selected, just switch to required TabControl page.
myTabControl.SelectedIndex = 1; // for selecting and displaying page with index 1
To hide navigation header of myTabControl, set those props (in constructor or in Form_Load event:
myTabControl.ItemSize = new Size(0, 1);
myTabControl.SizeMode = TabSizeMode.Fixed;
Then, you have only content of the TabControl page displayed, without navigation header.
First make it's this.IsMdiContainer = true; either from design time or run time.
then at the button's click event place the following code.
childForm frm = new childForm();
frm.MdiParent = this; //the current mdi parent form
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
frm.Show();
output
You can use mdi (multiple-document interface):
when you want a form start inside mainform, use this following code:
Dim hotelForm As New HotelForm()
HotelForm.MdiParent = Me // Me is your parent form want to open hotelForm inside
HotelForm.Show()
All forms have a property TopLevel, by setting this property to false, then you can deal with that form as a control, and add it to a panel control.
See below psuedo code:
Form2 newForm = new Form2();
newForm.TopLevel = false;
myPanel.Controls.Add(newForm);
newForm.Show();
After that, your main form design should look like as a navigation control and a panel beside it docked "Fill", then on clicking any navigation button, just create the desired form and set TopLevel to false, then show it on the panel
You have to put a Container control like a panel "pnlHost" in your form and use this for showing any form you want in it
private Form _currentForme;
private void ShowForm(Form frm)
{
_currentForme?.Close();
_currentForme = null;
_currentForme = frm;
_currentForme.TopLevel = false;
_currentForme.TopMost = false;
pnlHost.Controls.Clear();
_currentForme.FormBorderStyle = FormBorderStyle.None;
pnlHost.Controls.Add(_currentForme);
_currentForme.Dock = DockStyle.Fill;
_currentForme.Show();
}
I would use MDI parent and child for this.
But I guess you could also finish the child application, build the exe and initiate it as a new process from your Parent application.
Check the solution given here.
I tried a similar approach long ago and it worked perfectly.
Set current form as a MdiParent then use Datagridview to display data from other forms

C# usercontrol show

I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.
i have tested with this
UC lauchUC = new UC(person);
lauchUC.Show();
but that don't show nothing, and also tested with this:
UC lauchUC = new UC(person);
this.Controls.Add(lauchUC);
but it appears in the form
can someone help me or telling me if it's possible show it separated from the form?
You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.
The (container) Form:
public partial class Form1 : Form
{
public Form1(UserControl control)
{
InitializeComponent();
this.Controls.Add(control);
}
}
How to open it.
public void ButtonClick(object sender, EventArgs e)
{
var myControl = new MyUserControl();
var form = new Form1(myControl);
form.Show();
}
You can Place it in a Window and call Window.ShowDialog.
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UC(person)
};
window.ShowDialog();
}

how to show form in mdiparent from button in another form by C#

I have two form ( form1 and form2 ) and mdiparent .
button1 in form1
when click this button I want show form2 in mdiparent
The key points when you want to show a form in an MDI Parent, are:
You should have an form with property IsMdiContainer set to true
You should show your mdi parent form
When you want to show a form as mdi child, Set the propetry MdiParent of your child form to and instance of your mdi parent form
So if your Form1 is showing as mdi Child, in button click handler of your form 1, you can simply do this:
var f = new Form2();
f.MdiParent = this.MdiParent;
f.Show();
else, if your mdi parent form is open but form 1 is not mdi child:
var f = new Form2();
//I supposed that [mdiparent] is class name of your mdi parent form
f.MdiParent = Application.OpenForms.OfType<mdiparent>().FirstOrDefault();
f.Show();
esle you should show your mdi parent form first and use above code to show form 2 as mdi child.
The only thing you need is to create a Form2, set it's MdiParent property and show it. The only problem is dynamically setting MdiParent property - you will need to hold the instance of MdiParent. There are several ways to do this "properly".
Simple way
In Form1 button click should have the following event handler:
private void Button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.MdiParent = this.MdiParent; // "this" is Form1
form.Show();
}
This solution is less architectural - however, you can choose this one if it is suitable.
Singleton solution
If I did this, I would use singleton pattern. That's how I would do this:
MdiParent:
public class MdiParent : Form
{
private static MdiParent _instance;
public static MdiParent Instance
{
get { return _instance ?? (_instance = new MdiParent()); }
}
}
In the place where you instantiate your MdiParent:
MdiParent.Instance.Show();
// instead of
new MdiParent().Show();
If it is a main form - Main in Program.cs:
Application.Run(MdiParent.Instance);
// instead of
Application.Run(new MdiParent());
Form1 button click event:
private void Button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.MdiParent = MdiParent.Instance;
form2.Show();
}

MDI Child Forms Opening each other with the same parent form

So I have 3 Forms, lets call them Form1, Form2, and Form3.
I have sent the IsMDIParent Property to true for Form1.
When I launch the app, It loads Form2 as an MDI Child using
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
And that works fine. What I then want to do is click a button withing the 2nd form that will close Form2 and open up Form3 as a child form of Form1.
I tried
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = Form1;
SecondFormMDI.Show();
on the button click event in Form2, but it would not work.
Do I have to always launch a Child form from the parent form? and if so, how would i go about doing that when it is on the button click event on a child form?
Just use this.MdiParent, instead of Form1, like
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = this.MdiParent;
SecondFormMDI.Show();
You can set the MDIParent of any form in design time, why do it in run-time?
Simply set the value of MDIParent property of Form2 and Form3 to Form1, and that's it.
You could create a method in your MDIForm to open a childform:
public void OpenForm(Form form)
{
form.MdiParent = this;
form.Show();
}
When you want to open a new form in another form you do something like this (example in ChildFormOne with button):
private void btnOpenChildFormTwo_Click(object sender, EventArgs e)
{
((MDIForm)this.MdiParent).OpenForm(new ChildFormTwo());
this.Close();
}
Hope this helps.
ChildForm frmChild = new ChildForm();
frmChild.MdiParent = this.MdiParent;
frmChild.Dock = DockStyle.Fill();
frmChild.Show();

Categories

Resources