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
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
public FormB(string value)
{
InitializeComponent();
label10.Text = value;
}
Please help out
Prasad RaveendranPosted Dec 31, 2023, 6:53 PM
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
Sam HobbsPosted Dec 31, 2023, 4:21 PM
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.
Sam HobbsPosted Dec 31, 2023, 4:16 PM
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.My Form1 has:
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:
I hope that is enough for you to determine how to satsify your requirements.
Sam HobbsPosted Dec 31, 2023, 4:05 PM
This forum software messed up this post when I updated it to fix a problem it caused. I will post this post again.
Naimish MakwanaPosted Dec 31, 2023, 5:20 AM
You can add property in FormB like below.
then you can set the property value while show the formb.
Then you can use _uid value in FormB.
Thanks
Abdu AbdulPosted Dec 30, 2023, 5:00 AM
this is a chatgpt answer, i need more logical thinking
i have tried that already
Prasad RaveendranPosted Dec 30, 2023, 12:44 AM
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: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:This way, the data from
FormAwill be passed toFormB, and thenFormAwill be closed.