Passing data from one Form to another - c#

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();

Related

Get Text from text box on Form1 when calling function on Form2

Background/Context
So I am building a program that take 2 Excel Files and compares them highlight the differences. This is working fine. Now i am developing a second form, which does a very similar thing but essentially "Applies" the changes. Now in the first form I have two text boxes which contain the file locations, however on the second form which appears after the changes are highlighted there is only an Apply button hence I need to pull down the text box path for the file from Form1 however this doesn't seem to work in mt code:
CODE
public partial class Form2 : Form
{
Form1 form1 = new Form1();
public Form2()
{
InitializeComponent();
btnApply1.Click += new EventHandler(this.btnApply_Click);
btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
}
private void btnApply_Click(object sender, EventArgs e)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
new CRCCompareWorksheets.CompareHelper().ApplyChanges(
form1.ExcelPath1.Text, form1.ExcelPath2.Text, "CRC");
}
private void btnCancel1_Click(object sender, EventArgs e)
{
new CRCCompareWorksheets.CompareHelper().CancelApplication();
}
}
The Problem
So at the line where i call the function for Applying the changes, the variables form1.ExcelPath1.Text and form1.ExcelPath1.Text are both blank hence the file locations are not being pulled through and nothing works :(
The problem here is, that you create a new Instance of Form1. I guess, that Form2 is opened by Form1. In that case I would provide a reference of the calling form to the newly generated Form2. This could look something like the following:
Form2
public partial class Form2 : Form
{
Form1 form1 = null;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
btnApply1.Click += new EventHandler(this.btnApply_Click);
btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
}
private void btnApply_Click(object sender, EventArgs e)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
new CRCCompareWorksheets.CompareHelper().ApplyChanges(form1.ExcelPath1.Text, form1.ExcelPath2.Text, "CRC");
}
private void btnCancel1_Click(object sender, EventArgs e)
{
new CRCCompareWorksheets.CompareHelper().CancelApplication();
}
}
In Form1 you would then need to change the calling of Form2 to something like this:
Form2 frm = new Form2(this);
frm.Show();
Additionally it would be good practice to create properties for the values you want to read from the TextBoxes instead of making the controls public:
// Properties in Form1
public string ExcelPath1Text
{
get
{
return this.ExcelPath1.Text;
}
set
{
this.ExcelPath1.Text = value;
}
}
public string ExcelPath2Text
{
get
{
return this.ExcelPath2.Text;
}
set
{
this.ExcelPath2.Text = value;
}
}
And then use the properties in Form2:
CRCCompareWorksheets.CompareHelper().ApplyChanges(form1.ExcelPath1Text, form1.ExcelPath2Text, "CRC");
At the moment you are creating a new form1 in form2, rather than using the existing form1 that has the data you want to pass to form2.
You should pass form1 to the form2 constructor:
public partial class Form2 : Form
{
Form1 form1 = { get; set; }
public Form2(Form form1)
{
this.form1 = form1;
InitializeComponent();
btnApply1.Click += new EventHandler(this.btnApply_Click);
btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
}
And when you create form2 (I assume this is in form1):
Form2 form2 = new Form2(this);

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();

Passing variables between forms using a button [duplicate]

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();
}
}

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;
}
}

How to use a field in another form file?

public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
int port; // I declared a variable and I wanna use this in another form like
}
// ------------------------------------------------------- //
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SagTikMenuOlustur();
}
void menu1_Click(object sender, EventArgs e)
{
Form2 frq = new Form2();
frq.Show();
MessageBox.Show("{0} server is online ",port); //How to I declare ????
}
}
Set the field as public
or
Create property for that field.
This is the way you can use
Refer this link: How to access a form control for another form?
Best thing would be to create a property for it.
Try this
public partial class Form3 : Form
{
int _port;
public int Port
{
get { return _port; }
set { _port = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void menu1_Click(object sender, EventArgs e)
{
Form2 frq = new Form2();
frq.Show();
Form3 frm3 = new Form3();
frm3.Port = 8080;
MessageBox.Show("{0} server is online ", frm3.Port);
}
}
You have to change port to public.
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public int port; <<== Change to public
or public int port {get;set;}

Categories

Resources