I'm new in C# programming. I have a beginner level question:
How do I change the text property of the textbox1 in my form 2 object using a button in my form1?
Here's my code in form1:
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
This is in form2:
namespace DoubleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = "Test";
}
}
}
When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such
namespace DoubleForms
{
public partial class Form1 : Form
{
// NEW CODE
public string TextBoxText
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.
Code is creating new Form1 every-time you click the button, which is not you want I believe.
What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.
namespace DoubleForms
{
public partial class Form2 : Form
{
public event EventHandler Updated; // define an event handler
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(Updated != null)
{
Updated(sender, new EventArgs()); //Raise a change.
}
}
}
}
Now in Form1 subscribe to Form2 event.
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
frm2.Show();
}
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
Related
So i have 2 forms: Form1 and Form2
In Form1 i have this code:
pubilc class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(); //Here i want to pass MyFunction
}
public void MyFunction()
{
MessageBox.Show("This is passed function from " + this.Text);
}
}
And in Form2 i have this:
public class Form2 : Form
{
public Form2() //Here i want to receive Form1.MyFunction
{
InitializeComponent();
this.Button1.Click += new System.EventHandler(passedFunction);
}
}
In code above you can get my point. Having form with controls to which i would assign Eventhandlers with passed function while creating that form.
I haven't tried anything since i do not have idea how to pass method to form.
Try this:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(MyFunction); //Here i want to pass MyFunction
}
public void MyFunction(object sender, EventArgs e)
{
MessageBox.Show("This is passed function from " + this.Text);
}
}
public class Form2 : Form
{
public Form2(EventHandler passedFunction) //Here i want to receive Form1.MyFunction
{
InitializeComponent();
this.Button1.Click += passedFunction;
}
}
This is the first form( it contains an OK button and a textbox)
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
MessageBox.Show(dimx.ToString());
}
}
}
This is the second form (it contains an OK button + a messageBox when OK is pressed)
namespace Testt
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1();
MessageBox.Show(f.dimx.ToString());
}
}
}
I want to write the value of 6 in the textbox press OK, then form2 pops up and when i press OK on the second form it should display 6 not 0..what am i doing wrong?
You could make it so that your form takes dimx as a variable, so it would look like this
public partial class Form2 : Form
{
private int dimX;
public Form2(int dimx)
{
InitializeComponent();
dimX = dimx;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dimX.ToString());
}
}
alternatively you could pass the form itself, changing
public Form2(int dimx)
into
public Form2(Form1 f1)
You would then also have to replace
private int dimX;
//and
dimX = dimx;
//and
MessageBox.Show(dimX.ToString());
with
private Form1 f;
//and
f = f1;
//and
MessageBox.Show(f.dimx.ToString());
You are creating a NEW form object on every onclick event and that is not the way it works...
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1(); // no that way!!!
MessageBox.Show(f.dimx.ToString());
}
use instead a callback, delegate
So im using this now
Form1:
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
//MessageBox.Show(dimx.ToString());
}
}
}
and Form2:
namespace Testt
{
public partial class Form2 : Form
{
private Form1 f;
public Form2(Form1 f1)
{
InitializeComponent();
f=f1;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(f.dimx.ToString());
}
}
}
I have an application with 2 Forms, for those forms I have create a Menu which I depose on the two forms.
There is only a menuStrip item on the menu, I just want when I click on "test1" to redirect to Form1 and when I click to "test2" I want to redirect to Form2.
But if test1 is already open/display I don't want to show him again and the same for test2.
My code in my Menu :
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f2.Hide();
f1.Hide();
f1.ShowDialog();
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.Hide();
f2.Hide();
f2.ShowDialog();
}
}
My Form1 :
My Form2 :
I just want the same result like my Buttons in Form1 and Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.ShowDialog();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
I thought that the property Visible for forms could help me but not...
The problem is when I click on my buttons it's open a new window but when my form is already open I don't want to open it again.
Thanks for your reply, I hope that I am clear sorry for my english in advance.
You are currently creating a new form each time the click handler code is executed.
Here is one way, but its nasty and I wouldn't really recommend it. I've assumed that form1 is the entry to your application and that its also the exit of the application. This solution uses a singleton to hold the f1/f2 instances.
public static class Global
{
static Global()
{
f2 = new Form2();
}
public static Form f1;
public static Form f2;
}
Your menu altered:
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Global.f2.Hide();
Global.f1.Hide();
Global.f1.Show();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
Global.f1.Hide();
Global.f2.Hide();
Global.f2.Show();
}
public void SetForm1(Form form)
{
Global.f1 = form;
}
public void SetForm2(Form form)
{
Global.f2 = form;
}
}
And the forms:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Global.f1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Global.f2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Global.f1.Show();
}
}
Hope this helps.
Your logic is wrong.
It seems to me that you want to display different content depending on the user selection on that 'menuStrip'. You need to look at dynamic controls loading not different forms loading.
You can have just a MainForm with that 'menuStrip' and a Panel. Define some User Controls and dynamically add them to that panel based on the user selection.
Snippet
public MainForm : Form
{
public MainForm()
{
// code
}
public void MenuStrip_OptionSelected(object sender, EventArgs e)
{
Panel1.Controls.Clear();
switch(MenuStrip.SelectedValue)
{
case "UserControl1" : Panel1.Controls.Add(new UserControl1()); break;
...
}
}
}
public UserControl1 : UserControl
{
// code
}
can sameone provide example code on how to call non-static method on form1 from form2.
form1
public Form1()
{
InitializeComponent();
}
public void prikazi()
{
MessageBox.Show("ok");
}
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
form2
public Form2()
{
InitializeComponent();
}
private void callMethod_Click(object sender, EventArgs e)
{
// this don't work. If I change to public static void on form1 then it work's but I need non-static example
Form1.prikazi();
}
thanks
It doesn't matter if it's a form class, if you want to access a non-static method, there is no other alternative then to create an instance of the class.
But - It doesn't make sense.. so don't do it
Find other alternatives, like creating the method you need static in a common place, or consider adding this method (or a variation of it) to the form
form1
public Form1()
{
InitializeComponent();
}
public void prikazi()
{
MessageBox.Show("ok");
}
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
form2
private Form1 parentForm;
public Form2(Form1 parentForm)
{
this.parentForm = parentForm;
InitializeComponent();
}
private void callMethod_Click(object sender, EventArgs e)
{
parentForm.prikazi();
}
But better learn to bundle reusable code in to a separate class, rather than passing around form instances.
You need to have an instance of the form to call the method.
There are a few ways that you can make this work
1) Pass an action through to the new form
public Form2()
{
InitializeComponent();
}
public Action yourAction {get; set;}
private void callMethod_Click(object sender, EventArgs e)
{
Action instance = yourAction;
if(instance != null)
instance();
}
then in Form 1 you can say
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.yourAction = prikazi;
frm.Show();
}
2) You can pass an instance of Form1 into Form 2
So in Form 2 you have:
public Form1 ParentForm {get; set;}
private void callMethod_Click(object sender, EventArgs e)
{
if (ParentForm != null)
ParentForm.prikazi();
}
And Form1 you assign a value to the ParentForm variable
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ParentForm= this;
frm.Show();
}
public partial class Form1 : Form
{
internal static Form1 ViewForm1; // make other form run Public void
public form1()
{
InitializeComponent();
ViewForm1 = this; //Add this
}
public void DoSomething()
{
//Code...
}
}
......................
public partial class Form1 : Form
{
public form2()
{
InitializeComponent();
Form1.ViewForm1.ShowData(); // call public void from form1
}
I have two buttons in Form 1, one is "ShowForm2" button and one is "button1" button.
Button 1 is disable by default. And when I click "ShowForm2" button, Form 2 will show.
So, what I want is, when I click the "button2" in form 2, it will enable the "button1" in form 1.
So, I try to code like this in my form2 class:
public partial class Form2 : Form
{
bool enable_form1_button1;
public bool Enable_form1_button1
{
get { return this.enable_form1_button1; }
set { this.enable_form1_button1 = value; }
}
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
enable_form1_button1 = true;
}
}
Then in my Form1 class, I am expecting to get the "enable_form1_button1 = true" to pass into form 1 and enable my form 1 button1. But how to do this?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btb_Showfrm2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
button1.Enabled = frm2.Enable_form1_button1; // I put it here, and it just does not seems right
}
}
Well what you can do is, expose the button as a property and send a reference of your current form to the form that needs to take control:
Form1
public partial class Form1 : Form
{
public Button BtnShowForm2
{
get { return btnShowForm2; }
set { btnShowForm2 = value; }
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
// pass the current form to form2
Form2 form = new Form2(this);
form.Show();
}
}
Then in Form2:
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
private void btnEnabler_Click(object sender, EventArgs e)
{
// access the exposed property here <-- in this case disable it
_form1.BtnShowForm2.Enabled = false;
}
}
I hope this is what you're looking for.
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
Form2 oFrm2 = new Form2();
oFrm2.evtFrm += new ShowFrm(oFrm2_evtFrm);
oFrm2.Show();
}
void oFrm2_evtFrm()
{
button1.Enabled = true;
}
}
Form2.cs
public delegate void ShowFrm();
public partial class Form2 : Form
{
public event ShowFrm evtFrm;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (evtFrm != null)
{
evtFrm();
}
}
}
you can send a refrence of your form1 to your form2 in the from2 constructor for example :
and then in button clicked event handler in form2 call a public method in form1 to enable the button :
this could be your form1 code :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public void enableButton()
{
button2.Enabled = true;
}
this could be your form2 code :
public partial class Form2 : Form
{
private Form1 frm;
public Form2(Form1 frm)
{
InitializeComponent();
this.frm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
frm.enableButton();
}
If you'll only have one instance of Form1 you can also get away with having a static Button member in Program which references the button you want to enable from Form2.
Form1.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button2.Enabled = false;
Program.button = button2;
}
private void button1_Click(object sender, EventArgs e)
{ new Form2().Show(); }
private void button2_Click(object sender, EventArgs e) { }
}
}
Form2.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form2 : Form
{
public Form2()
{ InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{ Program.button.Enabled = true; }
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
static class Program
{
public static Button button;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I would recommend you to use event/delegates to perform this task. This is the standard way of doing so.
Again passing reference in constructor or making static func/var/properties are just workaround and Called as bad programming.