I am trying to pass data from FormA to FormB, if i tried normal method it worked fine, but when i tried closing the FormA after when passing data to FormB the data dosent get passed
What i have tried:
FormA
C#
private void button1_Click(object sender, EventArgs e)
{
//This don't pass the data
this.Close();
th = new Thread(() => open_FormB(value_lbl.Text.Trim()));
th.SetApartmentState(ApartmentState.STA);
th.Start();
//This don't pass the data as well
this.Close();
th = new Thread(() =>
{
FormB Form_b = new FormB(value_lbl.Text.Trim());
Form_b.ShowDialog();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
//This Worked Fine
FormB F = new FormB(value_lbl.Text);
F.Show();
}
private void open_FormB(string email)
{
Application.Run(new FormB(email_lbl.Text));
}FormB
C#
public FormB(string value)
{
InitializeComponent();
label10.Text = value;
}Please help out

Prasad RaveendranRank #320 · 7.8K reputationPosted Dec 29, 2023, 7:44 PM EST
It seems like you're trying to pass data from
FormAtoFormBwhenbutton1is clicked. However, when you closeFormAbefore openingFormB, the data doesn't get passed.The issue lies in the fact that when you call
this.Close(), it closes the current form (FormA) immediately, and the subsequent code that tries to pass data toFormBdoesn't get executed.To pass data from
FormAtoFormBand then closeFormA, you can do it like this:private void button1_Click(object sender, EventArgs e) { string dataToPass = value_lbl.Text.Trim(); // Create an instance of FormB FormB formB = new FormB(dataToPass); formB.Show(); // Close FormA this.Close(); }This code first creates an instance of
FormBand passes the necessary data (dataToPass) to its constructor. Then it showsFormBwhile immediately closingFormA.Ensure that your
FormBconstructor correctly accepts and uses the value passed to it:public FormB(string value) { InitializeComponent(); label10.Text = value; // Set the label or control in FormB with the passed value }This way, the data from
FormAwill be passed toFormB, and thenFormAwill be closed.Naimish MakwanaRank #324 · 7.7K reputationPosted Dec 31, 2023, 12:20 AM EST
You can add property in FormB like below.
private string _uid; public string Uid { get { return _uid; } set { _uid= value; } }then you can set the property value while show the formb.
FormB Form_b = new FormB(); Form_b.Uid = "Your Value"; Form_b.ShowDialog();Then you can use _uid value in FormB.
Thanks
Abdu AbdulRank #5,389 · 301 reputationPosted Dec 30, 2023, 12:00 AM EST
this is a chatgpt answer, i need more logical thinking
i have tried that already
Sam HobbsRank #107 · 20.5K reputationPosted Dec 31, 2023, 11:05 AM EST
This forum software messed up this post when I updated it to fix a problem it caused. I will post this post again.
Sam HobbsRank #107 · 20.5K reputationPosted Dec 31, 2023, 11:16 AM EST
I can provide a better answer if you can explain the fundamental problem. Why do you need to open the form using a separate message loop, in other words, using the
Runmethod?You can use the
Runmethod in theProgramclass. The following shows how to do that. This is not exactly what you need but as I say, if you can explain the fundamental problem then I can be more specific about how to do that. In my code, I am using theProgramclass to pass the data. You can use the constructor instead, as you are doing. TheProgramclass however is an alternative.internal static class Program { public static string data; /// /// The main entry point for the application. /// [STAThread] static void Main() { ApplicationConfiguration.Initialize(); Form1 f1 = new(); Application.Run(f1); Form2 f2 = new(); Application.Run(f2); } }My Form1 has:
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Program.data = textBox1.Text; }That is not what you want exactly, you want to use a button click event, but I hope that shows what you can use in it.
My Form2 has:
public Form2() { InitializeComponent(); textBox1.Text = Program.data; }I hope that is enough for you to determine how to satsify your requirements.
Sam HobbsRank #107 · 20.5K reputationPosted Dec 31, 2023, 11:21 AM EST
You say i need more logical thinking but you need to provide a more logical explanation of what you need. Old-time programmers like me used to say GIGO; Garbage In, Garbage Out. The best answers are obtained from the best questions.
Prasad RaveendranRank #320 · 7.8K reputationPosted Dec 31, 2023, 1:53 PM EST
I appreciate your initiative in trying out the suggested solution beforehand. It's great to know you've already explored that avenue. To delve deeper into resolving the issue, perhaps we can consider alternative approaches or examine the problem from a different angle. Could you share more details about the specific challenges you encountered during your previous attempts? This way, we can tailor our approach more precisely and hopefully find a solution that aligns more accurately with your needs