C
C#•9mo ago
elaine

How to Transfer Data from one Form to the other Form

Hey, is there anyone that can help me? Im pretty new to C# and I need help with something. My problem looks like this: I have 2 Forms. In form 1 there is a Textbox and in the Second box is a label. when i press a Button in Form 1, Form 1 dissapears and the second Form appears. I want to type in text in the first form 1 Textbox and i want it to appear in the Second Form Label. How does that work?
3 Replies
daniel2
daniel2•9mo ago
Can u show screenshot of both form
Hazel 🌊💃
I recommend using the constructor of Form2 to send in the data from Form1. Either that or a public initialization method. Something like this:
// Option 1
// -----------------------------------------------
// In Form 2
public Form2(string textToPutOnLabel) {
...
label1.Text = textToPutOnLabel;
}

// In Form 1
var form2 = new Form2(textBox1.Text);
form2.Show();
Close();
// Option 1
// -----------------------------------------------
// In Form 2
public Form2(string textToPutOnLabel) {
...
label1.Text = textToPutOnLabel;
}

// In Form 1
var form2 = new Form2(textBox1.Text);
form2.Show();
Close();
// Option 2
// -----------------------------------------------
// In Form 2
public void Initialize(string textToPutOnLabel) {
label1.Text = textToPutOnLabel;
}

// In Form 1
var form2 = new Form2();
form2.Initialize(textBox1.Text);
form2.Show();
Close();
// Option 2
// -----------------------------------------------
// In Form 2
public void Initialize(string textToPutOnLabel) {
label1.Text = textToPutOnLabel;
}

// In Form 1
var form2 = new Form2();
form2.Initialize(textBox1.Text);
form2.Show();
Close();
AboSkandar69
AboSkandar69•9mo ago
You could follow this if you haven't figured it out yet https://www.youtube.com/watch?v=hHHX6DxXSZk
Chamo Niwanthi
YouTube
How to pass data between one form to other form in visual studio wi...
#howtopassvaluesbetweenforms #Howtopassdatabetweenoneformtootherform #visualstudiowindowsformapplication
Want results from more Discord servers?
Add your server