How to change ActiveForm from another Class - c#

I would like to ask for help. I would like to achieve that in Form1 in the panel I open Form2 in which there is a button, when the user presses in the panel on Form1 opens Form3
Here I am in the Form1 code where the user presses the button that is in the window, and thus Form2 opens in the panel
{
public partial class Form1 : Form
{
private Form activeForm;
public Form1()
{
InitializeComponent();
}
public void OpenSlaveForm(Form podrizenyForm, object btnSender)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = podrizenyForm;
podrizenyForm.TopLevel = false;
podrizenyForm.FormBorderStyle = FormBorderStyle.None;
podrizenyForm.Dock = DockStyle.Fill;
this.pnlMain.Controls.Add(podrizenyForm);
this.pnlMain.Tag = podrizenyForm;
podrizenyForm.BringToFront();
podrizenyForm.Show();
string nazev = podrizenyForm.Text.ToUpper();
}
private void btnForm1_Click(object sender, EventArgs e)
{
OpenSlaveForm(new Forms.Form2(), sender);
}
}
}
Here I am in Form2 and I try to open Form3 through the button in the Form1 panel
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnForm2_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.OpenSlaveForm(new Forms.Form3(), sender);
}
}
Thank you in advance for any help

You are creating a new Form1 instance when you press the button in Form2, that adds the new Form3 to that new instance instead in the one you are seeing on the screen.
You need to pass a reference to the current Form1 instance to Form2:
public partial class Form2 : Form
{
Form1 mainInstance;
//Add a new constructor which accepts a reference to Form1
public Form2(Form1 MainInstance)
{
mainInstance = MainInstance;
InitializeComponent();
}
public Form2()
{
InitializeComponent();
}
private void btnForm2_Click(object sender, EventArgs e)
{
//Now use that instance to create Form3
mainInstance.OpenSlaveForm(new Forms.Form3(), sender);
}
}
Now, change the button call in Form1:
private void btnForm1_Click(object sender, EventArgs e)
{
//Use the new constructor in order to pass the current instance
OpenSlaveForm(new Forms.Form2(this), sender);
}

Related

Changing text in one form, from another form

Im trying to change the text in Form1 when pushing the button on Form2
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textCh = "Text has been changed";
}
}
Form 1:
public partial class Form1 : Form
{
public string textCh {
get
{
return this.textCh;
}
set
{
this.label1.Text = value;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
When I'm pushing button nothing happens, the text remain the same.
Another method of passing Form1 to Form2 via the Show() method and the .Owner property:
// In Form1
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass Form1 via "this"
Then, in Form2, you CAST .Owner to type Form1:
// In Form2
Form1 f1 = this.Owner as Form1;
if (f1!=null && !f1.IsDisposed)
{
f1.textCh = "Text has been changed";
}
There are several way to do this here 2 examples
This example use references whitout any use of events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2(this);
frm.Show();
}
}
here the Form2 is created passing the Form1 as parameter
public partial class Form2 : Form
{
Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var lbl = (Label)_parent.Controls.Find("label1", false).First();
lbl.Text = "new text";
_parent.Update();
}
}
Than the Form2 use that for set the value wanted.
This example use events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2();
frm.UpdateLabelEvent += Frm_UpdateLabelEvent;
frm.Show();
}
private void Frm_UpdateLabelEvent(string str)
{
label1.Text = str;
}
}
and here the code of Form2
public partial class Form2 : Form
{
public event Action<string> UpdateLabelEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UpdateLabelEvent("new string value");
}
}
Nothing changes in your Form1 because you are creating a new Form1 first and change the text there. The new Form1 is never shown, so you see no changes.
Solution
private void button1_Click(object sender, EventArgs e)
{
//Form1 f1 = new Form1(); GET rid of this line
f1.textCh = "Text has been changed";
}
you need to make sure off course that f1 is known in form2, if you dont know how here is a simple way to do that
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.f1 = this;
f2.ShowDialog();
}
public Form2()
{
public Form1 f1 { get; set; }
...

how to keep a form activated

Now, I have two forms, called form1 and form2, in the form1 there's a button, when I click it, then open the form2
private void button1_Click(object sender, EventArgs e)
{
Ges.produit.Add(p);
Form3 f= new Form3();
f.dataGridView1.Rows.Clear();
foreach (var item in Ges.produit)
{
f.dataGridView1.Rows.Add(item.Id, item.Name, item.Qty, item.Prac, item.Prav, item.Disc);
}
this.MdiParent = f.MdiParent;
f.Show();
this.Hide();
}
Question: in the form2, I created a button when I click it, the form2 close and get back to form1 without opening another form1 . How to do?this
You can do this a few ways, one way is to pass a reference of the parent form to the child form in its constructor, and then when you close the child form call show against this reference.
Another way to accomplish this is when you create the child form inside the parent form, at the same time also wire up the FormClosed event of the child form to re-display the parent form.
The following is an example for both of these methods
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
var form2 = new Form2(this);
this.Hide();
form2.Show();
}
private void btnShowForm3_Click(object sender, EventArgs e)
{
var form3 = new Form3();
form3.FormClosed += (o, args) =>
{
this.Show();
};
this.Hide();
form3.Show();
}
}
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1)
:this()
{
_form1 = form1;
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
_form1.Show();
}
}

How can i change Form1.Text when its Opened with a button on Form2

I have 2 form and I want change Form1.Text when its run with a button on
Form2 !!!! I dont whant make a instance Form1.
Thanks friends**
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ChangeForm1Text_Click(object sender, EventArgs e)
{
Form1
}
}
In this particular case (you need a reference to the Owner form) there is a simple shorcut that doesn't require to keep a local instance of the first form
Just pass the instance of Form1 as the Owner in the ShowDialog call
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog(this);
}
Now you could reference the property Owner inside the Form2 code
private void ChangeForm1Text_Click(object sender, EventArgs e)
{
this.Owner.Text = "your new caption for form1";
}
One possible way for you to do this is to pass the instance of Form1 into a constructor on Form2`. So add this constructor and field to Form2:
private readonly Form1 _parentForm1;
public Form2(Form1 parentForm1) : this()
{
_parentForm1 = parentForm1;
}
Now when you create your Form2 in the button click, create it like this:
Form2 F2 = new Form2(this);
F2.ShowDialog();
Then you can use _parentForm1 anywhere in your instance of Form2 to refer back to the other form.

if called window is already running then close it and run newly called window

In my app in several time i have to call a window(class). the work of this window is to show the meaning of a word.when i again call that window a new window shows but the previous one also shows.
I have two form named form1,form2.
Form1 is like that:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
Form2 s = new Form2(a);// it will be called as many time as i click
s.Show();
}
}
Form2 is like that:
public partial class Form2 : Form
{
public Form2(string s)
{
InitializeComponent();
label1.Text = s;
}
}
what i want is that inside form1 if i call form2 it shows but if i call form2 again the previous form2 window will be closed automatically and new form2 window will be shown instead of previous one.
How can i do that????
Here's an example of storing the Form2 reference at class level, as mentioned by the others already:
public partial class Form1 : Form
{
private Form2 f2 = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (f2 != null && !f2.IsDisposed)
{
f2.Dispose();
}
string a = textBox1.Text;
f2 = new Form2(a);
f2.Show();
}
}
I think you should consider using singleton pattern.
You can implement it like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
Form2.ShowMeaning(a);// it will be called as many time as you click
}
}
and Form2
public partial class Form2 : Form
{
private static readonly Form2 _formInstance = new Form2();
private Form2()
{
InitializeComponent();
}
private void LoadMeaning(string s)
{
label1.Text = s;
}
//Override method to prevent disposing the form when closing.
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
public static void ShowMeaning(string s)
{
_formInstance.LoadMeaning(s);
_formInstance.Show();
}
}
Hope it helps.

Add listbox items in form 1 from form 2 button pushed in c#

I have a Form (form1) which has a ListBox and a Button. On clicking the button, it opens another Form (form2). In this Form I have a TexBox and a Button. On clicking this button, whatever I entered in this Textbox, should have to go into the ListBox in form1. Please help me to find out the solution.
step 1 : Set the Modifiers property of Listbox as Public
step 2 : in button click of Form1, put
Form2 fm2 = new Form2(this);
fm2.ShowDialog();
step 3: in Form2, put the following declaration at top level
private Form1 _fm1;
Also add a constructor :
public Form2(Form1 fm1)
{
_fm1 = fm1;
InitializeComponent();
}
step 4: in button click of Form2, put the following lines:
_fm1.ListBox1.Items.Add(Textbox1.Text);
this.Close(); //close the Form2
Hope this helps.
Make a static instance of form1 public static Form1 _Form1;,then you have access it from other forms with out make an instance:
I write an example:
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Form1 = this;
}
public static Form1 _Form1;
public void AddItem(object value)
{
listBox1.Items.Add(value);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 _Form2 = new Form2();
_Form2.Show();
}
}
Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1._Form1.AddItem("your item");
}
}
Make Form1 the Owner of Form 2 and then you can access Form1 from Form2:
Form1:
private void Form1Button_Clicked(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Owner = this;
f2.Show();
}
public void AddListItem(object text)
{
YourListBox.Items.Add(text);
}
Form2:
private void Form2Button_Clicked(object sender, EventArgs e)
{
Form1 f1 = (Form1)Owner;
f1.AddListItem(YourTextBox.Text;);
Close();
}
When you open the second form pass a reference to the current form:
var form2 = new Form2(this);
form2.Show();
In the constructor of form2 save the reference and use the reference in the click handler
class Form2
{
private Form1 _form1;
public Form2()
{
// ...
}
public Form2(Form1 form1):this()
{
_form1 = form1;
}
void Button_Click(object sender, EventArgs e)
{
if(_form1 != null)
{
_form1.AddValue(textBox1.Text);
}
}
}
It would be way better to define an interface that contains the method 'AddValue' and instead of using the type Form1 use the interface:
interface IAddValues
{
void AddValue(string value);
}
class Form1: IAddValues
{
// ...
public void AddValue(string value)
{
// ...
}
}
// somewhere in form1:
var form2 = new Form2(this as IAddValues);
form2.Show();
In the constructor of form2 save the reference and use the reference in the click handler
class Form2
{
private IAddValues _valueAdder;
public Form2()
{
// ...
}
public Form2(IAddValues valueAdder):this()
{
_valueAdder = valueAdder;
}
void Button_Click(object sender, EventArgs e)
{
if(_valueAdder != null)
{
_valueAdder.AddValue(textBox1.Text);
}
}
}

Categories