Mohsen Alizadeh
Mohsen Alizadeh

Reputation: 21

Passing values between Forms

As you know we don't have session in WinForm. I have several forms (Customer, Service, Operator, ...) in my project, and I take from Operator some values ( operator Name, Operator LastName and Profile Picture address). I want to store this values somewhere in run-time and whenever I need it, I can fetch it from where I stored it.

Is there an easy way to do this ?

Upvotes: 0

Views: 288

Answers (3)

Ben
Ben

Reputation: 188

also you can use project properties like this:

//write

Properties.Setting.Default["key"] = value;

Properties.Setting.Default.Save();

//read

value = Properties.Setting.Default["key"];

Upvotes: 0

Picrofo Software
Picrofo Software

Reputation: 5571

I believe that the simplest way to do this is to create a class that can be accessed from your forms which contains the data you want

Example

// Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myNameSpace
{
    class Class1
    {
        public static string Name = "";
        public static string LastName = "";
        public static string ProfilePictureaddress = "";
    }
}

Using this, whenever you would like to store or retrieve data, you may use myNameSpace.Class1. For example, if you would like to store into the Name, you may use myNameSpace.Class1.Name = "value";. This will set Name to value.

Thanks,
Happy Holidays! :)

Upvotes: 1

e-mre
e-mre

Reputation: 3383

I think these are the words of a long time web developer. I hear you my friend. I've been there...

Session object of web programming is just a workaround to work around the problem of saving state in a stateless environment. On the other hand winforms programming does not have this problem. Your application has objects that all keep their state information.

As for passing information from one form to another:

  1. You can define parameters on the receiving forms constructor to pass parameters. Then you define local variables in the receiving form to hold references to those objects.
  2. You can set a separate method on the receiving form to send parameters and call it from sender form.
  3. You can make the information public on the sender form (or some other place) and get it from receiving form when you need it.

Which one is best? Depends entirely on your business case and application logic.

Upvotes: 1

Related Questions