-2

I have a C# windows form application. I have a form already opened and I need to open another form as dialog. For example, I have a form "Sell Cars", inside that form I have the fields related to the car (license, model, year, etc.) and a button to open a dialog box with the list of cars to be sold. The user selects one from the list and after that I need to copy all values of the selected car into the main form and close the dialog form.

What is the best way to do it?

tbag
  • 4,913
  • 3
  • 13
  • 8

1 Answers1

-1

You can access and set variable of any form opened by your main form.

main.cs

string result = "";
using(SecondForm secondForm = new SecondForm()) 
{
    if(secondForm.ShowDialog() == DialogResult.OK) 
    {
        result = secondForm.carName;
    }
}

SecondForm.cs

public partial class SecondForm
{
    public string carName;
    public SecondForm(){
        carName = "honda";
    }
}
Jay
  • 161
  • 1
  • 18