Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like SESSION in Windows application?

Tags:

c#

windows

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.

like image 643
Manish Avatar asked Mar 29 '10 11:03

Manish


4 Answers

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.

like image 99
codeulike Avatar answered Nov 09 '22 01:11

codeulike


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.

like image 43
Will Marcouiller Avatar answered Nov 09 '22 03:11

Will Marcouiller


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.

like image 2
0x49D1 Avatar answered Nov 09 '22 01:11

0x49D1


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.

like image 1
Aiden Bell Avatar answered Nov 09 '22 02:11

Aiden Bell