开发者

Is there something like SESSION in Windows application?

Is there something like S开发者_开发知识库ESSION 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.


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.


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.


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.


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜