c# – 在Windows应用程序中是否有像SESSION这样的东西?
发布时间:2020-12-15 05:40:48 所属栏目:百科 来源:网络整理
导读:在 Windows应用程序中有类似SESSION的东西吗?我希望存储一些值,以便在表单之间保持持久性. 例如:第一个表单有一些复选框,第三个表单相应地处理它们.所以我需要将复选的复选框存储在某处. 解决方法 您只能通过此表单的属性公开CheckBoxes Checked状态,您可
在
Windows应用程序中有类似SESSION的东西吗?我希望存储一些值,以便在表单之间保持持久性.
例如:第一个表单有一些复选框,第三个表单相应地处理它们.所以我需要将复选的复选框存储在某处. 解决方法
您只能通过此表单的属性公开CheckBoxes Checked状态,您可以在其中放置CheckBox,并从第三个或Process表单访问这些属性.
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. } } 请考虑我把这个代码放在首位,所以可能不会编译.无论如何,我希望这能让您了解如何在整个表单中传递您的价值观. Web和WinForm编程之间的最大区别是Web是无状态的. SESSION和VIEWSTATE是允许保留值的变通方法. WinForms是有状态的,因此您不需要经历SESSION和类似VIEWSTATE的变量.只要对象存在,就会保留一个值. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |