Sarathy
Sarathy

Reputation: 452

Windows application

How the variables can be transferred between the Winforms? Example customer id

Thanks

Upvotes: 0

Views: 279

Answers (4)

Jim Anderson
Jim Anderson

Reputation: 3622

If CustomerID is public:

frmTwo.CustomerId = frmOne.CustomerId

Upvotes: 2

Robert Venables
Robert Venables

Reputation: 5981

You must declare a public property in the form you wish to pass to. Then, after instantiating your new form, it's a simple assignment:

C#: MyOtherInstantiatedForm.CustomerID = CurrentCustomerID;

Do you need to pass around CustomerID to several forms? How about other customer information? If you provide more information we can probably give you a better solution.

Upvotes: 2

senfo
senfo

Reputation: 29056

The most important thing to note here is that a Form is nothing more than a C# class. If you think about a Form in these terms, the answer will probably jump out, by itself.

Essentially, there are two options that you have. The first is to expose a property on the Form, for which you wish to pass data to. This is a decent method if your form does not rely on the data being passed, in order to function.

CoolForm myForm = new CoolForm();

myForm.MyProp = "Hello World";
myForm.ShowDialog();

The second option is to pass data through the constructor. I prefer this approach when the form relies on the data, in order to function. I also tend to mark the parameter-less constructor as private to ensure that the form is properly instantiated.

CoolForm myForm = new CoolForm("Hello World");

myForm.ShowDialog();

Hope that helps...

Upvotes: 5

Jab
Jab

Reputation: 13775

To add to Jim's answer, you can pass variables through public properties or via the forms constructor. So

Form2 frm = new Form2(customerId);

or like Jim provided. There are many ways to getting the value, I prefer constructor if the second form is dependent on it.

Upvotes: 2

Related Questions