Passing variables between forms using a button [duplicate] - c#

This question already has answers here:
Pass variable between forms when clicking button
(5 answers)
Closed 8 years ago.
I'd like to know how to pass, let's say an integer, from form1 to form2.
I tried to do that through a button that would open form2, but the event button click didn't recognize the integer... What should I do?
In form1 I have integer x, and I want that when I click on button1, form2 would open up with x value in a label.
If there's a way to pass the info without the button (then I could use the button just to open form2) that would be great as well.

you can use second form constructor.
private int input;
public Form2(int input)
{
this.input = input;
InitializeComponent();
}
when you create an object , you can pass your var(int in here):
int myvar=911;
Form2 tmp = new Form2(myvar);
tmp.Show();
now you can use that private variable in form2:
lbl.Text=input.toString();
in Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 tmp = new Form2(911);
tmp.Show();
}
and in Form2:
public Form2(int input)
{
InitializeComponent();
label1.Text = input.ToString();
}
send your code for solve this problem.i can't find out why it doesn't recognize your vars without your codes!

In your code have a variable accessible by both forms. For example create a new Namespace and add a public static class FormData with a public static int Value inside.
namespace GlobalVariables
{
public static class FormData
{
public static int Value { get; set; }
}
}
Then from both of your forms you can access the said variable (and modify it) with GlobalVariables.FormData.Value. Here I made it a property, but you can do pretty much whatever you want with it.

Alternatively to passing the value by Form2 constructor, you can create a property that sets the label value, e.g.
Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public int XValue{
set{
label1.Text = value.ToString();
}
}
}
Form1
public partial class Form1 : Form
{
private int x = 10;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.XValue = x;
form2.Show();
}
}

Related

How to get the current position of the dialog in win forms?

I have a dialog that I can drag it around the screen.When I close the dialog and open again. I want it to be shown in the last position it was before.
How can I save the last position of the dialog in win forms?
Sample & dirty solution.
In solution we have two forms. Form1, Form2, and Settings static class.
public static class Settings
{
public static int X = 0;
public static int Y = 0;
}
Inside Form1 we have button1 that will be responsible to show Form2.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(Settings.X, Settings.Y);
frm.ShowDialog();
}
}
Inside Form2 we have ClosingEvent Handler :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.X = this.Location.X;
Settings.Y = this.Location.Y;
}
}
When Form2 is closing we are storing location to static Settings class. Later we are reading from this class X and Y position.
I hope it was helpful.
You need to:
1) create your custom "dialog", which is basically Windows.Forms.Form.
FormStartPosition property set to Manual.
2) once created, in its OnClosing event save its position somewhere from Location property
3) next time you are going to show it, assign the value you've saved before.

Getting info from formA's comboBox and use it in FormB [duplicate]

I have two forms in one of my projects. In form1 I have a dataGridView and In form2 I have 4 TextBoxes.
in Form1, I want to get a value in a variable from a datagridview using CellMouseClick event and then pass it to a TextBox in Form2
I have tried this.
form1 # it give me an Error
public form(int id)
{
int x;
x = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
and what iam suppose to do in the form2
With a constructor you can construct a type with the given prerequisites for construction.
If that means an integer, then so be it:
public MyForm(int id) {
SomeIdProperty = id;
}
And instead of var form = new MyForm();, do:
var form = new MyForm(idOfTheRelevantThing);
Then show it.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm2 = new Form2(dataGridView1.Rows[0].Cells[0].Value.ToString());
frm2.Show();
}
}
Form2
public partial class Form2 : Form
{
public Form2(string s)
{
InitializeComponent();
textBox1.Text = s;
}
}
If you are showing the Form2 from Form1, you can pass the value using the constructor. Something like this:
class Form2 {
public string Value { get; set; }
public Form2(string value) {
Value = value;
}
public void Form2_Load() {
textBox1.Text = Value;
}
}
and do this (inside Form1.cs):
Form2 f = new Form2("the value here");
f.ShowDialog(); //or f.Show();

C# Pass selection from listbox in form1 to textbox in form2

I've got two forms and a custom class. I have populated a listbox in form1 using my custom class which holds several data types. I want to pass each of those values in the class located in the listbox to individual text boxes in form2. I'm having trouble figuring out how to access the individual values in each listbox instance of my class and then split them among the text boxes in form2. I thought I was on the right track by creating a property on form2 for my first textbox. I only have the one property set up right now because I wasn't sure it would work and was only testing. In form1 I was trying to set it up so I could access my class values from the selected item.
Form 1
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProperties editProperties = new frmProperties();
DialogResult result = editProperties.ShowDialog();
object employeeSelect = lstBoxEmployees.SelectedValue;
editProperties.TextFirstName = Convert.ToString(employeeSelect);
}
form 2
public partial class frmProperties : Form
{
public string TextFirstName
{
get { return txtFirstName.Text; }
set { txtFirstName.Text = value; }
}
public frmProperties()
{
InitializeComponent();
}
}
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}
public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}
Form 2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}

Passing data from one Form to another

I have two forms in one of my projects. In form1 I have a dataGridView and In form2 I have 4 TextBoxes.
in Form1, I want to get a value in a variable from a datagridview using CellMouseClick event and then pass it to a TextBox in Form2
I have tried this.
form1 # it give me an Error
public form(int id)
{
int x;
x = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
and what iam suppose to do in the form2
With a constructor you can construct a type with the given prerequisites for construction.
If that means an integer, then so be it:
public MyForm(int id) {
SomeIdProperty = id;
}
And instead of var form = new MyForm();, do:
var form = new MyForm(idOfTheRelevantThing);
Then show it.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm2 = new Form2(dataGridView1.Rows[0].Cells[0].Value.ToString());
frm2.Show();
}
}
Form2
public partial class Form2 : Form
{
public Form2(string s)
{
InitializeComponent();
textBox1.Text = s;
}
}
If you are showing the Form2 from Form1, you can pass the value using the constructor. Something like this:
class Form2 {
public string Value { get; set; }
public Form2(string value) {
Value = value;
}
public void Form2_Load() {
textBox1.Text = Value;
}
}
and do this (inside Form1.cs):
Form2 f = new Form2("the value here");
f.ShowDialog(); //or f.Show();

sharing parent variable between forms

form A opens form B, and form A.visible = false;
form A has a public int variable and I need controls in form B to be able to access and modify this variable. could this be done as passing the value through the constructor is only one way!
and so if it could be done, if form A is not visible could the value still be accessed?
(form b is not supposed to be dialog!)
many thanks!
edited: I do not quite get the explanations actually. so far it is like that:
in form a:
//in global space
public int temp = 123;
//in form_load event
Form setup = new setup();
setup.Show();
this.Visible = false;
in form setup:
//in form_load event
textBox1.text = temp.toString();
//in button_press event
form a.temp = "456";
I hope I have explained my stance clearly!
First, have member field in form B of type form A:
private FormA parent;
Second, have such constructor in form B:
public FormB(FormA parent)
: this()
{
this.parent = parent;
}
Now when you create instance of form B, pass reference to the running form A instance:
FormB formB = new FormB(this);
formB.Show();
And you can access the public property through the parent field e.g.
//inside Form B code..
public Foo()
{
parent.PUblicProp = "Hello";
}
this is one way to pass values from one form to another form
public partial class Form2 : Form
{
public string MyProperty { get; set; }
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = MyProperty;
}
}
after that in the button click event handler in Form1
add the following code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MyProperty = "This is from Form1";
form2.Show();
}
}
Since your int variable is public then your controls in formB can access and modify it, formA visibility won't affect this it will be accessed disregarding the visibility of it:
FormA f = new FormA();
int newValue = f.yourintvariable;
And there are a lot of alternative ways like using constructor to send the variable in the FormA constructor and initialize it from FormB,
or you can define it as static but in this case will be one variable for all the instances of thid form

Categories

Resources