I'm opening second form from my main form like this.
On a combo box selected index changed event.
Code in main form.
if (cmbVtMgmnt.SelectedItem.ToString()=="Basic Voter Management")
{
this.Visible = false;
frmVoterOP votefrm = new frmVoterOP();
votefrm.Show();
}
How can I view the main form or open main form from the second form's label click event.
private void lblBacktoMain_Click(object sender, EventArgs e)
{
//What should come here?
}
You can pass main form object to your frmVoterOP form and use that object to show or hide the main form
In main form
frmVoterOP votefrm = new frmVoterOP(this);
In frmVoterOP
MainForm frmMainForm;
public frmVoterOP(MainForm mainForm)
{
frmMainForm = mainForm;
}
To show the main form from frmVoterOP
private void lblBacktoMain_Click(object sender, EventArgs e)
{
frmMainForm.Show();
}
Another alternative could be using the Form.Owner property of the second form.
In main form
if (cmbVtMgmnt.SelectedItem.ToString()=="Basic Voter Management")
{
this.Visible = false;
frmVoterOP votefrm = new frmVoterOP() {Owner = this};
votefrm.Show();
}
In frmVoterOP
private void lblBacktoMain_Click(object sender, EventArgs e)
{
MainForm mainForm = (MainForm)this.Owner;
mainForm.Show();
}
If possible, you can also use ShowDialog():
if (cmbVtMgmnt.SelectedItem.ToString()=="Basic Voter Management")
{
this.Visible = false;
frmVoterOP votefrm = new frmVoterOP();
votefrm.ShowDialog();
this.Visible = true;
}
So now when the votefrm is closed, your main form should pop back up.
Related
I'm trying to open a Panel that is in the main form (form1) from a button that is inside a UserControl , but the code runs but does not enable the panel of the main form
Can you help me?
//UserControl code
private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
{
Button seta = (Button)sender;
var form = new Form1();
form.EnabledPanel1(seta.Tag.ToString());
}
//main form code
public void EnabledPanel(string order)
{
panel1.Visible = true;
}
Assuming the UserControl is also contained by Form1, then you can use TopLevelControl to get a reference to the Form:
private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
{
Button seta = (Button)sender;
Form1 f1 = (Form1)this.TopLevelControl;
f1.EnabledPanel1(seta.Tag.ToString());
}
*Why does EnabledPanel() receive a string?
I have 2 forms (Form1 and Form2). Form1 has a splitter with two panels. I have added Form2 in the panel2 of the splitter control. I want to pop in and pop out Form2 without creating a new instance of Form2. Please find the code snippet below:
public partial class Form1 : Form
{
private Form2 form2 = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
form2 = new Form2();
form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form2.Dock = DockStyle.Fill;
form2.TopLevel = false;
splitContainer1.Panel2.Controls.Add(form2);
form2.Pop += new EventHandler(PopForm);
form2.Show();
}
//button click event handler from Form2
private void PopForm(object sender, EventArgs e)
{
Button b = sender as Button;
if(b.Text.ToUpper() == "POPOUT")
{
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel2.Controls.Remove(form2);
//need to show the form without creating a new instance to maintain state
form2 = new Form2();
form2.SelectedMailId = 1;
form2.Pop += new EventHandler(PopForm);
form2.SetButtonText = "PopIn";
form2.Show();
}
else
{
//this works fine
splitContainer1.Panel2Collapsed = false;
form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form2.Dock = DockStyle.Fill;
form2.TopLevel = false;
splitContainer1.Panel2.Controls.Add(form2);
}
}
}
How can I show the Form2 without creating a new instance when popping out?
While popout setup the form border styled and toplevel
if(b.Text.ToUpper() == "POPOUT")
{
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel2.Controls.Remove(form2);
//need to show the form without creating a new instance to maintain state
form2.TopLevel = true;
form2.FormBorderStyle = FormBorderStyle.Sizable;
// setup your settings
form2.Show();
}
On button click, I open a new form (lets say Form2), but I don't want that Form2 to open more than 1time. And I don't want to use .ShowDialog(), because it wont allow me to go to the Previous Form. How can I do that ?
You can use Application.OpenForms property to check if form already opened:
if (!Application.OpenForms.OfType<Form2>().Any())
{
Form2 form2 = new Form2();
form2.Show();
}
You can show existing form instead of creating new:
Form2 _form2 = null;
void Button1_Click(object sender, EventArgs e)
{
if (_form2 == null)
{
_form2 = new Form2();
_form2.Closed += Form2_Closed;
}
_form2.Show();
_form2.BringToFront();
}
private void Form2_Closed(object sender, System.EventArgs e)
{
_form2 = null;
}
You can issue the Show method, which will show the form and allow the users to get back to the form, but then you can also override the OnClosing event of the form:
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
}
and that will keep the users from being able to literally close the form. Finally, if you wanted, you could Hide the form when the user closes it:
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
and then you'll need to hold on to that instance in the first form so that you can re-show it when the button is clicked. So in the first form you might have a class field like this:
private Form2 _form2 = new Form2();
and then in the button click it would look like this:
_form2.Show();
and since they can't actually close the form this will always work.
You can try something like
bool windowIsNotOpen;
Mutex mutext = new Mutex(true, "Form2", out windowIsNotOpen);
if (!windowIsNotOpen)
{
// Form2 is already open
return;
}
You can do a static property that tells you whether it's open or not. You'd set it when the form is opened, and turn it off when the form is closed.
class Form2 {
public static bool IsOpen { get;set; }
public Form2() {
Load += (sender, e) => { Form2.IsOpen = true; };
Closed += (sender, e) => { Form2.IsOpen = false; };
}
}
Whenever you want to open it, check the flag first.
Form2 f;
bool isOpen = false;
private void button1_Click(object sender, EventArgs e)
{
if (f == null)
{
f = new Form2(); ;
f.FormClosed += new FormClosedEventHandler(f_FormClosed);
}
if (!isOpen)
{
isOpen = true;
f.Show();
}
}
void f_FormClosed(object sender, FormClosedEventArgs e)
{
isOpen = false;
}
Try this or use Application.OpenForms and check which one opened
I use MDI in MainForm.cs
public Le_MainForm()
{
InitializeComponent();
this.IsMdiContainer = true;
this.Name = "MainUSER";
}
private void barButtonItem_ListeOrdres_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_CreatOrdreAller_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Program.AllerRetour = "Ordre Aller";
Fiche_Ordre f_Fiche = new Fiche_Ordre();
f_Fiche.MdiParent = this;
f_Fiche.Show();
}
the question now, when I am in other form Ex.:Liste_Ordres.cs or Fiche_Ordre.cs how can i redirect from Fiche_Ordre.cs into Liste_Ordres.cs and vice versa without loosing MDI ?
When I'm in Fiche_Ordres.cs to go to Liste_Ordres.cs I use:
private void simpleButton_Annluer_Click_1(object sender, EventArgs e)
{
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.Show();
this.Close();
}
But as you can see I lose the MDI, that mean when I click the menu on MainForm, the Liste_Ordres form will disappear.
as you can see in this Video that when i redirect From Liste to fiche, and then maximize the window i lose the menu that mean i lose Mdi.
You just need to set the MdiParent again:
f_Liste.MdiParent = this.MdiParent;
Here is a small code that will illustrate my problem:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Interval = 3000;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
using (Form form = new Form())
{
form.ShowInTaskbar = false;
form.ShowDialog();
}
}
}
If I press button1 (which calls button1_Click) and then click on another application in the taskbar so that it comes to the top, and then after 5 secs I go back to my application, the created form won't be visible and I won't have a way to bring it back to the top, while my Form1 will be unresponsive because of having an invisible dialog on top.
What's a workaround for this?
Make your main form an owner of your modal box.
Form form = new Form();
form.Owner = this;
form.ShowInTaskbar = false;
form.ShowDialog();