I would like to load multiple forms within a form using user controls and I've tried the following code but nothing seems to happen after clicking on button1. Anyone knows what's wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 control = new UserControl1();
control.Dock = DockStyle.Fill;
this.Controls.Add(control);
}
}
however the contents of UserControl1 seems to be overlapping and I can still see the content of Form1
The Z-order of the controls on the form matters. With Controls.Add(), the control ends up on the bottom of the order, existing controls overlap it. You fix it like this:
this.Controls.Add(control);
control.BringToFront();
Or use Controls.SetChildIndex() to insert it between controls.
Probably you need to change the value of Dock property. When it is DockStyle.Fill -- it will just take the whole area. Try to change it to other value, depending what layout you need.
Related
I keep getting the error in the title, and I can't figure out where the Form is set to read-only or how I can disable this.
private void studentToolStripMenuItem1_Click_1(object sender, EventArgs e)
{
addStudent newStudentForm = new addStudent(this);
newStudentForm.MdiChildren = this;
newStudentForm.Show();
}
addStudent is a form that takes care of creating students, and populating a list of students which is kept on the main form. So I want to be able to edit data on Form1 from Form2 which is why I am using Mdi
Can anyone give me any tips on where I should be looking at what I should be looking for to fix this error?
EDIT: To be clear
Form1 = Parent
Form2 = Child
MdiChildren is just an array, which represents the current forms, which are the Mdi Childs of the dialog. You need to load the MDI child into the parent.
Something like this:
addStudent newStudentForm = new addStudent();
newStudentForm.MdiParent = this;
newStudentForm.Show();
You don't need to set MdiChildren. Moreover, you wouldn't be able to set it this way even if it wasn't readonly - it is a collection of forms, not a form.
Logically, you try to tell that "newStudentForm has current form as MDI child", and is isn't true, you actually need "newStudentForm is a new MDI child of this".
The only thing you need to do is to set MdiParent in your child form:
private void studentToolStripMenuItem1_Click_1(object sender, EventArgs e)
{
AddStudent newStudentForm = new AddStudent(this);
newStudentForm.MdiParent = this;
newStudentForm.Show();
}
P.S. Please, use proper naming for your classes. It shuld be AddStudent, not addStudent. This one letter makes code absolutely unreadable.
So essentially I'm using the dotNetBrowser for a project that i'm loading into a panel on my main form, and I have a button in a usercontrol for user input so it can interact with the browser. Here's what I have:
public partial class Form1 : Form
{
public BrowserView browserView = new WinFormsBrowserView();
public Form1()
{
InitializeComponent();
this.panel1.Controls.Add((Control)browserView);
browserView.Browser.LoadURL("URL TO BE LOADED");
browserView.Browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
{
if (e.IsMainFrame)
{
// Do stuff when loaded
} else return;
}
};
}
}
That works fine, in my usercontrol.cs I have:
public void button1_Click(object sender, EventArgs e)
{
BrowserView br = (this.Parent as Form1).Controls["browserView"] as BrowserView;
br.Browser.LoadURL("NEW URL");
}
So that when the button is clicked it can load a new url. But this is throwing a null exception.
Basically I need these two components to be able to pass information on to eachother. The method I've used worked fine for other Form1 controls, but not the browser it seems.
Any advice?
In your case, browserView is a name of the public variable, so you can simply use(this.Parent as Form1).browserView to access it.
Your are adding browserView to Form1.panel1, but trying to get it from (this.Parent as Form1).
You don't need to search for BrowserView when you have explicit reference to it. I suggest giving this reference to the user control. User control having the knowledge of the innards of the hosting form means that information is flowing in the wrong direction.
Names of controls are given to them by IDE, and are empty when controls are created in code.
I have 2 Forms and i want them to be inserted into a form with a tabcontrol. I have read this question about adding forms to tabcontrols and Form1 is successfully inserted into the tabcontrol. Form2 is also inserted but the content of the form is not showing.
This is my code:
private FrmMainForm trackIT = new FrmMainForm();
private MainForm customer = new MainForm();
private void TrackITForm_Load(object sender, EventArgs e)
{
AddNewForm(trackIT, trackitTab);
AddNewForm(customer, customerTab);
}
public void AddNewForm(Form form, TabPage tab)
{
form.WindowState = FormWindowState.Maximized;
form.TopLevel = false;
form.Parent = tab;
form.Visible = true;
}
I also have set my parent Form's IsMDIContainer property to true.
What can be the issue here?
I also have set my parent Form's IsMDIContainer property to true : Don't do that. You're not doing MDI.
In AddNewForm(), set the WindowState property after all the others.
I think (not 100% sure) that Visble=true is not enough, call form.Show() instead. Do it after setting the WindowState and especially the Parent.
Check the forms for conflicting properties in the designer code and FormLoad.
Consider using UserControls instead of Forms. They are meant to be embedded.
How can I close an opened window when I call a new window? That means I want only 1 child window at the time. I don't allow multi-window.
public partial class Main_Usr : Form
{
public Main_Usr()
{
InitializeComponent();
this.IsMdiContainer = true;
if (Program.IsFA) barSubItem_Ordre.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
Ordre_Liste f = new Ordre_Liste();
f.MdiParent = this;
f.Show();
}
private void barButtonItem_CreateOrdre_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Program.AllerRetour = "Ordre Aller";
Ordre_Fiche f = new Ordre_Fiche();
f.MdiParent = this;
f.Show();
}
private void barButtonItem_OrdreListe_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Ordre_Liste f = new Ordre_Liste();
f.MdiParent = this;
f.Show();
}
private void barButtonItem_CreateOrdRet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Program.AllerRetour = "Ordre Retour";
Ordre_Fiche f = new Ordre_Fiche();
f.MdiParent = this;
f.Show();
}
}
There are different ways to implement pseudo masterpage:
You can create BaseForm form with desired layout. Then inherit other forms from this BaseForm and provide custom content.
You can create MainForm form with desired layout. Then create content controls as UserControls and show them in panel.
You can create MasterUserControl with desired layout. Then create content controls by inheriting from MasterUserControl (they will have same layout). Then use your main form as browser for displaying different content controls like pages.
EXAMPLE:
Create desired layout on Main_Usr form.
Do not set it as MdiContainer
If you want to access some controls (e.g. footer or header from child forms, set property Modifiers of those controls to protected)
Open Ordre_Liste form code and change it to inherit from Main_Usr form, instead of Form
Add custom content to Ordre_Liste form
voila! you have 'masterpage'
UPDATE (for 3rd option)
Create new user control with name MasterUserControl
Create desired layout on this control, keeping space for custom content (btw don't use TableLayoutPanels - they have issue with designer inheritance).
Create new user control with name HomeUserControl and change it to inherit from your MasterUserControl.
Open HomeUserControl designer and add custom content. Also you can modify parent controls, which has protected modifier.
On your main form place HomePageUserControl
There different ways to implement navigation between controls (aka pages). Simplest way - have menu on main form. Other way - define event 'Navigate' on master control, subscribe to that event on main form, and raise it from 'pages'.
Create Form instances on a class level.
Then you can access to them from events or methods.
Form1 f1;
Form2 f2;
void OpenForm1()
{
f1 = new Form1()
f1.Show();
}
void OpenForm2()
{
f1.Dispose(); //or Hide if you want to show it again later
f2 = new Form2();
f2.Show();
}
Like:
List<Form> openForms = new List<Form>();
foreach (Form f in Application.OpenForms)
openForms.Add(f);
foreach (Form f in openForms)
{
if (f.Name != "Menu")
f.Close();
}
Note, do NOT close them directly, becuase there will come to an error, if you would try to close (or dispose) them in the 1st foreach loop. Thats why you need to put them into a list and close them there.
This one has really got me stumped. I have certain forms that are being instantiated. When I instantiate a form i make it a child of the mdi form by
form1.MdiParent = this;
I have set the MDIWindowListITem property of my menustrip to a toolstripmenuitem
However this toolstripmenuitem does not show the mdi child form when it is instantiated
Does any one have any ideas on this?
Any inputs/ leads / hints would be most welcome. I am using .net framework 3.5
Regards
,
You have to write your code so that it is added manually I believe.
See the example here for pointers:
MSDN help on ToolStripPanel
Edit
You're right ignore my previous entry here's the code for a very simple MDI app that appears to do what your after.
It is just two blank forms. Form1 has IsMDIContainer=true. It also has menuStrip1, which contains two items "new" (newToolStripMenuItem) and "windows" (windowsToolStripMenuItem). Clicking new will open a new child window. I have set the MDIWindowListItem of menuStrip1 to windowsMenuStripItem. When a new child window is opened clicking on windowsMenuStripItem produces a drop down that shows all windows open.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int count;
public Form1()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
count++;
//Set a window title text as this is what is shown in the window list.
Form2 newForm = new Form2() { Text = string.Format("Window {0}", count) };
newForm.MdiParent = this;
newForm.Show();//<--- this needed to show window in list.
}
}
}
There is no code in Form2.
The child windows only show below windowMenuStripItem once Form.Show() has been called.
Without this they do not show in the list.