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.
Related
I want to make a management application with a Windows Forms form and I want to display one login form and when the login button is clicked, the second form should appear.
I tried this:
MainProgramm.BringToFrot();
How can I do that?
If you have a FormMain and you want to call a different form from it, you should create an instance of the second form and show it on the screen (as per #Tvde1 comment):
SecondForm secondForm = new SecondForm();
secondForm.Show();
If you want the new form to act as a dialog box, you can use:
secondForm.ShowDialog(this);
What you pretty much to do is add the following code in the Click action of the button that you want:
MainProgram mainProgram=new MainProgram();
mainProgram.ShowDialog();
Or if you prefer
MainProgram mainProgram=new MainProgram();
mainProgram.Show();
You can use Show() method to display form.
MainProgram mainProgram = new MainProgram();
mainProgram.Show();
There is a typo in your example code. Maybe you should try:
MainProgramm.BringToFront(); // add the missing 'n' letter
then add a new line like this:
MainProgramm.Show();
Hook into the OnLoad() method of the main form, and transfer control to the login form modally with the .ShowDialog() method.
For example:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var dlg = new LoginForm();
dlg.UserID = Environment.UserName;
if (dlg.ShowDialog() != DialogResult.OK)
{
this.Close();
}
var id = dlg.UserID;
toolStripStatusLabel1.Text = $"Login: {id}";
}
}
I am creating a Windows app that will have multiple forms in it. Initially, I have set WindowState Maximized for all.
My problem here is that when I minimise one form, size of the other forms stay same. So, if I minimize the main screen go to the next screen, the size of the next screen remains unchanged. All I need to do is change size of all the windows at once.
I tried this:
Mainscreen mainscreen = new Mainscreen();
this.WindowsState = mainscreen.WindowsState;
But I am finding a way to do it for all screens.
A very ugly demo for your reference
private List<Form> Windows { get; set; }
public Form1()
{
InitializeComponent();
this.Text = "Main Window";
this.Windows = new List<Form>();
var defaultSize = new Size(200, 100);
for (var i = 0; i < 3; i++)
{
var form = new Form() { Size = defaultSize, Text = "Resize Me" };
var suppressEvent = false;
form.SizeChanged += (sender, e) =>
{
if (suppressEvent)
return;
suppressEvent = true;
defaultSize = (sender as Form).Size;
foreach (var otherForm in this.Windows)
{
if (otherForm != sender as Form)
otherForm.Size = defaultSize;
}
suppressEvent = false;
};
this.Windows.Add(form);
form.Show();
}
}
If the minimization is the concern and all sub forms launch from a previous form the quickest way would be to have the main form be the owner of the sub form.
If you make a simple project with two forms Form1 and Form2 and add a button onto Form1 that launches Form2 it would look like the following.
private void button1_Click(object sender, EventArgs e)
{
Form2 lForm = new Form2();
lForm.Show(this);
}
Passing "this" (which is reference to Form1) into the constructor of show will make sure that when it minimizes the subform (Form2) will also minimize. You can also change the setting of Form2 and set
lForm.ShowInTaskbar = false;
if you wanted to help make it clear that all of the forms are tied together.
If you are instead talking about having all forms maintain the same size regardless of who started the change and what the size ends up being then it gets a bit trickier and the simplest way would be to make a form manager of some sort that listens to the OnSizeChanged event and updates all forms whenever any form updates. Keep in mind with this that you'll also need to have a test in the form to know whether or not it is the one that started the update otherwise you would get in a sort of infinite loop where Form1 updates causing Form2 to update which then sends out another message which tries to make Form1 update etc...
I have a button on Form1 that starts disabled by default. I have a ConfigureForm, where I have a menu strip, with an option to enable the button in Form1.
So my code is:
private void Portal2HammerButtonEnable_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Portal2HammerButton.Enabled = true;
}
But when I close ConfigureForm and look at the button, it's still disabled.
That's because you create a new Form1 and enable on that form the button. Instead, you have to pass the instance of the form you actually have open.
For design purposes you may want to use a controller class between these two forms. This will help you to simplify the complexity of passing data or actions between the two forms and will give you the power to escalate better the app..
When you open the ConfigureForm you have to do the following (in the simplest form however not recommended.)
...
{
ConfigureForm frmConfigure = new ConfigureForm(this);
}
Then inside the ConfigureForm:
public partial class ConfigureForm : Form
{
private From1 mainForm = null;
public ConfigureForm()
{
InitializeComponent();
}
public ConfigureForm(Form callingForm):this()
{
mainForm = callingForm as Form1;
}
private void Portal2HammerButtonEnable_Click(object sender, EventArgs e)
{
mainForm.Portal2HammerButton.Enabled = true;
}
}
You're creating a new form on the click of this button. What you want instead is a valid reference to the actual instance of Form1.
You have some options available:
If one of these forms is the "main" form of the application then you can ensure that it is created first and thus creates the other "sub" forms. you can override the constructors of any sub forms to include a reference to your "main" form.
You can keep references to all your important forms in a public static class such that all your forms can get to those references
You add your own public method to assign the "parent form" as a member or property of the child forms.
You can use reflection to find the instance of the "main" or "parent" form during creation or display of any child forms. If you do this, only do it once rather than upon every request. Try to cache that information.
You can read through the System.Windows.Forms namespace to find out if there's already a collection of objects through which you could iterate to find your main form.
I'd recommend option 2 or 5.
I write program. It's simple editor linke Notepad, created new form in main form, and I don't know how I can get and set value in the richTextField in sub form. When You click New file program use trah function.
private void NewWindow()
{
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Text = "Document " + WindowNumber.ToString();
WindowNumber++;
f2.Show();
}
When I have many open windows i don't can get to richTextBox in each of window.
How to do that?
Normally, controls are added to forms with the protected acces modifier. Then, to get their values from outside you need to create a public property on each form to expose the text.
public string RichText{
get{ return myTextBox.Text;}
}
i couldn't quite get if you want the parent form to edit the children or the other way around.
if you want the parent to be able to edit the children then the children should expose a method like Oscar example to edit the RichTextBox and the parent should save the children somewhere:
List<Form2> frm = new List<Form2>();
private void NewWindow()
{
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Text = "Document " + WindowNumber.ToString();
WindowNumber++;
f2.Show();
frm.Add(f2);
}
if the child should edit the parent you have several ways to do it. probably the best one designedly is using Events:
public delegate void EditHandler();
public event EditHandler edit;
Methods opening forms :
form1 --> form2 --> form3
ChecklistBox on the form1 there. How to know the form3 That is active or not?
If the forms you are referencing are MDI child forms, you could use
Form activeChild = this.ActiveMdiChild;
else you could use the following code if not using MDI child forms.
Form currentForm = Form.ActiveForm;
I understand that you are asking if form 3 is opened. If that is incorrect, please enlighten me.
There are probably dozens of ways to do so, it all depends on what you want to do.
One simple way would be to leave a flag somewhere, say in your Program.cs file:
public static bool Form3IsOpen = false;
Then:
private void Form3_Load(sender object, EventArgs e)
{
Program.Form3IsOpen = true;
}
And:
private void Form3_Close(sender object, EventArgs e)
{
Program.Form3IsOpen = false;
}
Supplemental:
You can also keep a reference to your subform:
In form1.cs:
private Form2 FormChild;
//In the function that opens the Form2:
FormChild = new Form2();
FormChild.Show();
Form2 will have something similar to retain Form3. If one form can open several, just use an array or collection.
When i usually have many different forms and only one instance to be created, i put them in dictonary and check it if there is a form.
Something like this:
public static Dictonary<string, Form> act_forms_in_app = new Dictonary<string, Form>();
now in every forms creation i do it like this
Form1 frm = new Form1();
frm.Name = "Myformname"
//set its properties etc.
frm.Load => (s,ev) { act_forms_in_app.Add(frm.Name, frm);};
frm.Load += new EventHandler(frm_Load);
frm.Disposed => (s, ev) { act_forms_in_app.Remove(frm.Name)};
//your usual form load event handler
public void frm_Load(object sender, EventArguments e)
{
...
}
somewhere where you want to check
Form frm = //Your form object
if(act_forms_in_app.ContainsKey(frm.Name))
{
//Perform as required
}