views:

335

answers:

7

Is there something like SESSION in Windows application? I want to store a few values to be persistent between forms.

For example: First form has some check boxes and third form process them accordingly. So I need to store the checked checkboxes somewhere.

+5  A: 

If you're talking about different Forms within the same Application, then just create some static members on a class, it will be persisted for the lifetime of the executable.

codeulike
I think he'd like values to share between different instances. This solution involves only one executable.
Maurizio Reginelli
+1  A: 

If you want to persist data between independent execution of the same app (as in concurrent request serving in a HTTP farm) then just write out some XML or use a mashalling/serializing system with your runtime/plaform (dunno what it would be for C#).

Then import it again. Just watch your concurrency control.

Aiden Bell
+2  A: 

You can use app.config (or Settings section in Project's Properties) if you use Visual Studio, or just serialize your values and store them in some file.

nihi_l_ist
A: 

If you are looking to store data on a per user basis between execution sessions, you should consider Isolated Storage.

  • Doesn't clutter install directory
  • Doesn't cause issues with AnitVirus software
  • Part of the OS including .Net objects, don't need to install anything else
  • Already works with the Windows security model
  • Exists on a per user basis, so saved settings are separated for each user
  • Can serialize/deserialize obects directly into it
cdkMoose
+1  A: 

If this is just a regular single-user windows application, create a class to model the state you want to pass around and require it in your form constructors:

internal class ApplicationState 
{
    // Store the selected checkbox values here, for example
    public List<int> SelectedProductIds { get; }
    // ... additional state ...
}

internal class EditOrderForm: Form
{
    private ApplicationState applicationState;
    public EditCustomerForm(ApplicationState applicationState) {
        this.applicationState = applicationState;
    }
    // Rest of the code
}

You could use static variables instead of instances - but those are just global variables that make your code harder to read and maintain.

Jeff Sternal
+1 your solution was the second I thought about. Thanks for the upvote. =)
Will Marcouiller
+2  A: 

You could only expose your CheckBoxes Checked state through properties of this form where you put your CheckBoxes on, and access these properties from your third or Process form.

public partial class MainForm : Form {
    // We assume we have let's say three CheckBoxes named chkFirst, chkSecond and chkThird
    public bool IsFirstChecked { get { return chkFirst.Checked; } }
    public bool IsSecondChecked { get { return chkSecond.Checked; } }
    public bool IsThirdChecked { get { return chkThird.Checked; } }

    // Calling this form from where these checked states will be processed...
    // Let's suppose we have to click a button to launch the process, for instance...
    private void btnLaunchProcess(object sender, EventArgs e) {
        ProcessForm f = new ProcessForm();
        f.Parent = this;
        if (DialogResult.OK == f.ShowDialog()) {
            // Process accordingly if desired, otherwise let it blank...
        }
    }       
}

public partial class ProcessForm : Form {
    // Accessing the checked state of CheckBoxes
    private void Process() {
        if ((this.Parent as MainForm).FirstChecked)
            // Process according to first CheckBox.Checked state.
        else if ((this.Parent as MainForm).SecondChecked)
            // Process according to second CheckBox.Checked state.
        else if ((this.Parent as MainForm).ThirdChecked)
            // Process according to third CheckBox.Checked state.
    }
}

Please consider that I picked this code up the top of my head, so it might happen not to compile. Anyway, I hope that this gives you an idea of how to pass your values throughout your forms.

The biggest difference between Web and WinForm programming is that Web is stateless. SESSION and VIEWSTATE are workarounds to allow one to preserve values.

WinForms are stateful, so you don't need to go through SESSION and VIEWSTATE-like variables. A value is preserved as long as the object exists.

Will Marcouiller
+1 for the all-important last two paragraphs.
Jeff Sternal
+1 for the same...
Manish
A: 

hello. i have a login form from where users login by providing credentials that username should be displayed on a label to some other form..how can i do it...i cannot use static variables as users are many...how can i implement..suggest pls

Aejaaz
Whenever you want to ask a question click on the button "Ask Question" (http://stackoverflow.com/questions/ask) at the top right corner..
Manish