Enable control of form based on form it was loaded? - c#

How to enable control of a form based in form it was loaded?
For example if Form3 was open from Form1 the button that will be enable in Form3 is button1 and if Form3 was open from Form2 the button that will be enable in Form3 is button2.

Yes you can, just set the Owner of the Form3 when you Show it, then in Form3's Load EventHandler check the Type of the Owner to determine which button to enable. Something like this should work.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show(this);
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2(); //Show Form2 for Testing
frm2.Show();
}
}
Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show(this);
}
}
Form3
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
if (Owner == null) return; //Check to make sure there is an Owner
if (Owner.GetType() == typeof(Form1))
button1.Enabled = true;
else if (Owner.GetType() == typeof(Form2))
button2.Enabled = true;
}
}

I think if (typeof(ParentForm) == typeof(Form1)) will do what you want. Keep in mind this check is based purely on the type so if you have multiple instances of Form1 you could run into some problems.

What he means is if you have a form that is of type Form than you wouldn't know which form is the parent if you only look at types.
Form1 opens Form3 and both forms are of type System.Windows.Forms
Form2 opens Form3 and both forms are of type System.Windows.Forms
If all three forms have different types than you can check if types are different.
Otherwise you have to check names.
form1.Name = "form1";
form2.Name = "form2";
form3.Name = "form3";
You will open child forms from within form1 with
form3.ShowDialog(this);
In form3 you can check for parent form and check for it's name. If it's form2 do something otherwise do something else.

Related

how to hide form1 by clicking button in form2

I designed a form for login that named as form1, and have 2 more form form2,form3
form2 items showing in panel from form1
and what I want to do
when I click the button in panel ( the item from form2 ) want to show form2 and hide form1 but the code isnt working
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
Form3 frm3 = new Form3();
frm1.Hide();
frm3.Show();
};
form3 is opening but form1 isnt hiding
Its not hiding because you created a new instance for form1 which is already instantiated.
You must call the Hide() method on the same instance used to call the Show() method.
If you added this code inside form1 class ,then change it like this
private Form1 frm1
public Form2()
{
frm1 = new Form1()
}
private void button_show_form1_Click(object sender, EventArgs e)
{
frm1.Show();
};
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm1.Hide();
};
You creating both forms in your codesnipped. Form1 is not the form you want to close, i think. frm1 is only another instance of Form1, but not the openend instance von Form1. You must have anywhere another instance of Form1. You must use the right referenz to the right instance.
It is important to know that WinForms create an instance of the startup form. In our case the startup form would be Form1. So, when you say
Form1 frm1 = new Form1();
You're actually creating a new (second) instance of Form1. This means that, in code, there are two different Form1's.
What we want to do is check with our application to get the instance of Form1 that already exists.
// This goes in Form2. It returns an instance of Form1, if it exists.
private Form getForm1()
{
// Application holds information about our application, such as which forms are currently open.
var formCollection = System.Windows.Forms.Application.OpenForms;
// Now we loop through the open forms in search of the form we want, Form1.
foreach (Form frm in formCollection)
{
if (frm.Name.Equals("Form1"))
{
return frm;
}
}
return null;
}
Now that we can get the existing instance of Form1 we can use it to make the form hidden.
private void button1_Click(object sender, EventArgs e)
{
var form1 = getForm1();
if (form1 != null) form1.Hide();
}
Something to know here is that when a form is hidden it is not closed. So, we need to make sure that Form1 becomes visible again. For example, we can set Form1 to be visible when Form2 closes.
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// The question mark (?) checks to see if the result of
// getForm1() is null. Same thing that is happening in
// button1_click
getForm1()?.Show();
}
Complete Form2 Code
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm1 = getForm1();
var frm3 = new Form3();
if (frm1 != null) frm1.Hide();
frm3.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// The question mark (?) checks to see if the result of getForm1() is null. Same thing that is happening in button1_click
getForm1()?.Show();
}
// This goes in Form2. It returns an instance of Form1, if it exists.
private Form getForm1()
{
// Application holds information about our application, such as which forms are currently open.
// Note that Open and Visible have different definitions.
var formCollection = System.Windows.Forms.Application.OpenForms;
// Now we loop through the open forms in search of the form we want, Form1.
foreach (Form frm in formCollection)
{
if (frm.Name.Equals("Form1"))
{
return frm;
}
}
return null;
}
}

Show MDIChild Form from another MDIChild Form

Let’s I have MDIParentForm (Home), Child Form (Form1, Form2), Home has one Menu (Add) and Form1 has button (btnOk)
'''''Here is C# Code''''''
public partial class Home : DevExpress.XtraBars.Ribbon.RibbonForm
{
public Home()
{
InitializeComponent();
}
public void CreateForm(Form frm)
{
frm.MdiParent = this;
frm.WindowState = FormWindowState.Maximized;
frm.Show();
}
private void btnAddForm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Form1 frm = new Form1();
CreateForm(frm);
}
}
//Up to this 1st level my code works fine.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDone_Click(object sender, EventArgs e)
{
//From Here I am not able show the Form2.
Home MDIParentForm = new Home();
Form2 frm = new Form2();
MDIParentForm.CreateForm(childForm);
}
}
Action: On the click of Add Menu I used to open Form1 (It works fine) and on the click of btnOk I want to open Form2 (which is another MDIChild form).
Let me know how we can accomplish this task.
What you do is you create yet another instance of your MDI parent and you set the Form2 as a child of this newly created MDI parent. This is clearly wrong, you want both to be children of the very same parent.
Either do this directly:
private void btnDone_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
// both have the same MDI parent
frm.MDIParent = this.MDIParent;
frm.Show();
}
or, if you insist on reusing your CreateForm
private void btnDone_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
((Home)this.MDIParent).CreateForm( frm );
}
Both approaches depend on this.MDIParent set in form1 and make sure the same instance is used for newly created form2.

How to show existing form from another form?

i have Form1 and Form2, in Form1 i have some textboxes like username,password and more... and a textbox "region". when user hits "region"(Form1.hide()), then Form2 opens witch has 5 labels with names of regions on it.
so how can i make that when user clicks on a name of region in Form2, Form1 will have the region on it? and keep all the data that the user entered before the region click.
something like this(in form 2):
private void center_Click(object sender, EventArgs e)
{
this.Hide();
Form1.region = "center";
Form1.show();
}
Try creating an instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
When creating Form2, just pass Form1 as a parameter and edit the textbox value in your click event.
On form1:
private void click_on_region(object sender, EventArgs e)
{
this.Hide();
Form2 frm2 = new Form2(this);
Form2.Show();
}
on form2:
Form1 _frm1;
public Form_Main(Form1 frm)
{
InitializeComponent();
_frm1 = frm;
}
private void center_Click(object sender, EventArgs e)
{
this.Hide();
_frm1.textBox_region.Text = whateverobject.Text;
_frm1.Show();
}
This might not be the prettiest but It'll do for starters.
Form 1 Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
textBox_Region.Text = objForm2.RegionName;
}
}
And Form 2 Code
public partial class Form2 : Form
{
public string RegionName
{
get
{
return textBox_Form2_Region.Text.ToString();
}
set { }
}
public Form2()
{
InitializeComponent();
}
}
On Form 2
private void center_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textbox_region = whateverobject.text;
this.hide();
frm1.show();
}
this will bring up a form1 with the region text on it.

Hide and restore the GUI Mutiple forms in c#

I have multiple form application. Form 1 is Login form for user validation. Form1 goes to Form2(Menu form). Form 2 leads to Form3 which is only popupform and hides the form2 when it is open.Form 3 goes to Form 4. Now from Form4 ,with button click, I need to restore the Form2 without creating a new instance. I tried using singelton approach, getting error.Code as follows as described above.
Form1:
private void click_Click(object sender, EventArgs e)
{
if ((user.Text == username) && (pswd.Text == password))
{
Form2 menu = new Form2();
menu.Username = user.Text;
//hides the form1
this.Hide();
menu.ShowDialog();
}
}
Form2:
private static Form2 instance;
public static Form2 Instance
{
get
{
if (instance == null)
{
instance = new Form2();
}
return instance;
}
}
private void button_Click(object sender, EventArgs e)
{
//Hide the form2
Hide();
//Bring up your PopUp form
using (Form3 form3 = new Form3())
form3.ShowDialog();
}
Form3:
private void button_Click(object sender, EventArgs e)
{
Hide();
Form4 form4 = new Form4();
form4.Show();
}
Form4:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.Instance;//error occured as Mainmenu does not contain a reference for Instance and no extension method accepting a first argument of type 'Mainmenu'
}
Basically, I want to restore the Form2 that was hided.Any help would be appreciated!Thanks
How about keeping track of the Forms parents so you have direct access.
For example in your Form1:
menu.Parent = this;
Hide();
menu.ShowDialog();
Then in your Form2 when Form3 is called:
using (Form3 f3 = new Form3())
{
Hide();
f3.Parent = this.Parent;
f3.ShowDialog();
}
Then when you want to dispose of Form3:
this.Parent.Show();
this.Parent.Focus();
this.Dispose();
Something along this lines for as many open forms as you need. If you need to drop back to the previous form, then make the Parent 'this' instead of 'this.Parent'

Using Another Form to Show Data with Checkboxes C#

I'm using WinForms. I have 2 forms, Form 1 and Form 2. In Form 2 i have multiple checkboxes. How do I check if anyone of those checkboxes in form2 is checked?
Each checkbox in form 2 displys a differnet message in form 1. Example, If i open form 2 and check the first checkbox, and close form2. In form1 a message should display in a label.
Form1
private Form2 frm2;
private void Open_frm_btn_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
private void show_btn_Click(object sender, EventArgs e)
{
if (frm2.checkBox1.Checked)
{
lbl_checkbx1.Text = "Hello";
}
}
Error
When i run the program, click on open Form2 button and check Message 1 checkbox and close from 2 then click show button. I get this error.
In Form1 you would have this:
var form2 = new Form2();
form2.ShowDialog();
if (form2.Check1Checked)
{
label1.Text = "Check1 is checked on form2";
}
if (form2.Check2Checked)
{
label2.Text = "Check2 is checked on form2";
}
form2.Dispose();
On Form2 set up some properties to expose the checkbox's checked value. This way you don't have to expose the entire control.
// C# v5.0 or earlier
public bool Check1Checked
{
get { return checkBox1.Checked; }
}
// C# v6.0
public bool Check2Checked => checkBox2.Checked;
The simplest way is to pass form1 as a parameter of form2 and then just linking the form2 checkboxes with Form1 labels.
public class Form2 {
private readonly Form1 _form1;
public Form2(Form1 form1) {
_form1 = form1;
}
}
and from Form1,
private void Open_frm_btn_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.ShowDialog();
}

Categories

Resources