C
C#•6mo 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•6mo 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•6mo 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
More Posts
ASP.NET MVC How to open Database in SQL Server Management Studio (can't connect server)?Pretty much the title. I have tried to go to the connection string to get the the server name and in✅ Urgent | Cannot update property in EF because another instance with the same key is being trackedHi, so I have following code: Basically I get a batch to update the field ```cs var batch = await cSelenium QuestionDo I need tear down etc for finalized product? Im using nunit framework but cant seem to be able to dotnet tool not recognized after local installI am trying to locally install the tool `dotnet-mgcb-editor` from nuget into my repository. But it's✅ Getting error when installing CSharpier globally``` C:\Users\mango\AppData\Local\Temp\c9a0f2f7-6b74-4505-bfe2-8c9a0efe4d18\restore.csproj : error NUDependency injection in WPFI'm trying to do DI in my WPF app. I've read about it on different sites and followed youtubers but Critical Path Method - Issue with Latest Start TimesI have made my program to calculate the project duration, the earliest start time and the latest staHave all ControllerBase ObjectResults return StatusCodeResult with ProblemDetailsI'd like to unify the way errors are returned from my web api. With the introduction of AddProblemDeCancelKeyPress not being handled/interrupted correctlyThe issue is that on ctrl+c press the application does call CancelKeyPress event, however I'm assumiurgent ! it's related with json serializerhello, Document = new NodeDocument() { Content = JsonSerializer.Serialize(document, new JsonSe