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.

#0

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.

#1

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.

#2

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.

#3

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.

#4

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.

#5

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

推荐文章

在struts2中获得会话的最佳和推荐方法?

在struts2中获得会话的最佳和推荐方法?

推荐文章

可以用Excel文件中声明的数据填充Java对象的库

可以用Excel文件中声明的数据填充Java对象的库

推荐文章

在否定的look behind断言Regex Python中等价于(.*)

在否定的look behind断言Regex Python中等价于(.*)

推荐文章

jquery-为每个元素定义不同的选项

jquery-为每个元素定义不同的选项

推荐文章

像Photoshop一样用RaphaelJs旋转角上的元素

像Photoshop一样用RaphaelJs旋转角上的元素

推荐文章

如何读写这种位图格式

如何读写这种位图格式

推荐文章

利用Android中的WebView问题制作位图

利用Android中的WebView问题制作位图

推荐文章

RedirectMatch vs.Deny from All-哪个更安全?

RedirectMatch vs.Deny from All-哪个更安全?

推荐文章

如何为打印(或html)报表指定页面宽度和长度?

如何为打印(或html)报表指定页面宽度和长度?

推荐文章

Python等价于Ruby的“method_missing”

Python等价于Ruby的“method_missing”

推荐文章

svn:redmine中父存储库的外部历史

svn:redmine中父存储库的外部历史

推荐文章

为什么是TreePanel节点&IE错误?

为什么是TreePanel节点&IE错误?

推荐文章

没有在AS3文件中指定这个单词会降低性能吗?

没有在AS3文件中指定这个单词会降低性能吗?

推荐文章

如何有效地使用计时器

如何有效地使用计时器

推荐文章

Django表单子类化-如何修改继承字段的某些属性,同时保留其他属性?

Django表单子类化-如何修改继承字段的某些属性,同时保留其他属性?

推荐文章

何时使用@Transactional's propagation=Proagation.REQUIRES\u NEW?

何时使用@Transactional's propagation=Proagation.REQUIRES\u NEW?