Passing a Value from One Form to Another Form

Passing Values from one Form to another Form in a Button click

Actually I have an object to pass. So I did this

in form1-->

private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}

in form2-->

public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();

}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;

a.set(g);
this.Close();
}
}

Passing a value from one form to another form

You don't access your form1, from which you created form2. In form2 button1_Click you create new instance of Form1, which is not the same as initial. You may pass your form1 instance to form2 constructor like that:

   // Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2(this);
objForm2.Show();
}

}

// Code From Form 2

public partial class Form2 : Form
{
Form1 ownerForm = null;

public Form2(Form1 ownerForm)
{
InitializeComponent();
this.ownerForm = ownerForm;
}

private void button1_Click(object sender, EventArgs e)
{
this.ownerForm.PassValue(textBox1.Text);
this.Close();
}
}

Passing Textbox Values one form to another form in Windows Forms

One way is to just have a method on your second form that returns any needed details

using (DrawForm drawForm = new DrawForm ())
{
drawForm.ShowDialog(); //will wait for the form to close before continuing
var userResults = drawForm.GetResults(); //returns whatever you need the form to return
return userResults;
}

The above logic only works if the second form is a popup form of sorts. If you want both forms to be persistent, then it's a bit different. You might want to ensure both forms have access to each other. When you create the drawform be sure to save a reference to it. and in the constructor for your drawform add an argument to include the main form.

  DrawForm drawForm = new DrawForm(this);//now the forms can talk to each other

in your drawing form when the user hits 'go'

 mainForm.TriggerFinishedDrawing(drawingObject);

There are several ways to achieve this sort of behaviour, you just need to decide what sort of flow makes the most sense in your use case

pass a value from one form to another

You create a new form, so old values will be lost. Default values are null.

Form1 F1 = new Form1(); //I'm a new Form, I don't know anything about an old form, even if we are the same type

You can use static vars, which would be the easiest solution to archive your goal, but there are other ways like constructors, containers, events etc.

public static string En1
{
get { return En; }
set { En = value; }
}

public static string Ed1
{
get { return Ed; }
set { Ed = value; }
}

And in the other form

private void button1_Click(object sender, EventArgs e)
{
Form1 F1 = new Form1();
Form1.Ed1 = textBox1.Text;
Form1.En1 = textBox2.Text;
}

Please be advised that a static variable exists only once for a class. So if you have multiple instances and you change the static variable in one, the change also affects all other instances.

How to pass string value from one form to another form's load event in C #

Just create a property on the Form2 class and set it before you show Form2.

public class Form2
{
...
public string MyProperty { get; set; }

private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(this.MyProperty);
}
}

From Form1:

public void button1_Click(object sender, EventArgs e)
{
string departmentName = "IT";
Form2 frm2 = new Form2();
frm2.MyProperty = departmentName;
frm2.Show();
this.Hide();
}

Passing data from another form to another form

Create a class for your values:

public class Person
{
public string No {get; set;}
public string Name {get; set;}
public string Address {get; set;}
}

In your second form add readonly public property, from where you get selected data.

public class SecondForm
{

private Person _SelectedPerson;
public Person SelectedPerson
{
get { return _SelectedPerson; }
}

//Set data to the SelectedPerson in your click eventhandler
private void metroButtonSelect_Click(object sender, EventArgs e)
{
_SelectedPerson = new Person();
_SelectedPerson.No = metroGrid1.SelectedRows[0].Cells[0].Value.ToString();
_SelectedPerson.Name = metroGrid1.SelectedRows[0].Cells[1].Value.ToString();
_SelectedPerson.Address = metroGrid1.SelectedRows[0].Cells[2].Value.ToString();
}

}

Then in your first method create method which open Second form and return selected data

public class FirstForm
{

private Person GetSelectedPerson();
{
Person selected = null;
using(var secondForm = new SecondForm())
{
secondForm.ShowDialog();
selected = secondForm.SelectedPerson;
}
return selected;
}

//And use above method in button click eventhandler
private void Button_Click(object sender, EventArgs e)
{
Person selected = this.GetSelectedPerson();
if(selected != null)
{
//Show selected data in the textboxes
this.TextBoxNo.Text = selected.No;
this.TextBoxName.Text = selected.Name;
this.TextBoxAddress.Text = selected.Address;
}
}
}

secondForm.ShowDialog() method will show form as modal, which will make first form not accessible.

How to pass data from one form to another?

You can try the following code to pass data from one form to another form.

By the way, if you want to select data from database, you can use SqlDataAdapter to fill the dataset. Then you could set the dataset as the datasource.

Form1:

 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.tb.Text = textBox1.Text;
form.ShowDialog();

}
}

Form2:

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
string connectionstring = @"";
MySqlConnection connection = new MySqlConnection(connectionstring);
connection.Open();
string sql = string.Format("select * from Teacher where Name='{0}'",textBox1.Text);
MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection);
DataSet set = new DataSet();
adapter.Fill(set);
dataGridView1.DataSource = set.Tables[0];
}

public TextBox tb
{
get { return textBox1; }
set { textBox1 = value; }

}
}

You can see the following picture to know what I am doing.

Result:

Sample Image



Related Topics



Leave a reply



Submit