C#WinForm页面传值解决方法(仿web中Session)
2008-03-11 10:13
513 查看
昨天同事问我有没有一个安全简单的办法实现win应用程序页面之间传值,因为他先前是做web的所以老是想着传值,我想了想决定做个类似web中Session的组件,当然这不是会话的了,这个一个声明周期为整个应用程序级的。
具体代码如下:
using System;
using System.Collections;
/**//// <summary>
/// Session 的摘要说明。
/// </summary>
public class Session:DictionaryBase
...{
private static Session assion=null;
/**//// <summary>
/// 生成一个实例
/// </summary>
private Session()
...{
}
/**//// <summary>
/// 得到一个单身实例
/// </summary>
/// <returns>返回类型为Session</returns>
public static Session GetSession()
...{
if(Session.assion==null)
...{
Session.assion=new Session();
}
return Session.assion;
}
/**//// <summary>
/// 添加新成员
/// </summary>
/// <param name="newID">新字成员名字</param>
/// <param name="newmember">新成员</param>
public void Add(string newID,Object newmember)
...{
try
...{
Dictionary.Add(newID,newmember);
}
catch
...{
return;
}
}
/**//// <summary>
/// 删除成员
/// </summary>
/// <param name="memberID"></param>
public void Remove(string memberID)
...{
try
...{
Dictionary.Remove(memberID);
}
catch
...{
return;
}
}
/**//// <summary>
/// 本类的索引器
/// </summary>
/// <returns>返回Object成员</returns>
public Object this[string memberID]
...{
get
...{
try
...{
Object obj=(Object)Dictionary[memberID];
Dictionary.Remove(memberID);//销毁
return obj;
}
catch
...{
return null;//如果没有数据则返回null
}
}
set
...{
try
...{
this.Dictionary.Add(memberID,value);
}
catch
...{
return ;
}
}
}
}
先在来看如何使用的例子:
具体代码:
( 注意要添加引用上面程序编译成功的程序集:Session.dll)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
...{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox tbxAdd;
private System.Windows.Forms.TextBox tbxDel;
private Session a;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
...{
a=Session.GetSession();
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.listBox1 = new System.Windows.Forms.ListBox();
this.tbxAdd = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.tbxDel = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(136, 144);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 88);
this.listBox1.TabIndex = 0;
//
// tbxAdd
//
this.tbxAdd.Location = new System.Drawing.Point(144, 32);
this.tbxAdd.Name = "tbxAdd";
this.tbxAdd.Size = new System.Drawing.Size(112, 21);
this.tbxAdd.TabIndex = 1;
this.tbxAdd.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(336, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "add";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tbxDel
//
this.tbxDel.Location = new System.Drawing.Point(144, 72);
this.tbxDel.Name = "tbxDel";
this.tbxDel.Size = new System.Drawing.Size(112, 21);
this.tbxDel.TabIndex = 3;
this.tbxDel.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(336, 72);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "del";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(336, 176);
this.button3.Name = "button3";
this.button3.TabIndex = 5;
this.button3.Text = "look";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.tbxDel);
this.Controls.Add(this.button1);
this.Controls.Add(this.tbxAdd);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
...{
// a.Add(this.tbxAdd.Text.Trim(),this.tbxAdd);
a[this.tbxAdd.Text.Trim()]=this.tbxAdd;
}
private void button2_Click(object sender, System.EventArgs e)
...{
a.Remove(this.tbxDel.Text.Trim());
}
private void button3_Click(object sender, System.EventArgs e)
...{
this.listBox1.Items.Add(((TextBox)a[this.tbxAdd.Text.Trim()]).Text.Trim());
}
}
}
现在你可以到处使用Session.dll了,让它帮你处理页面传值。
具体代码如下:
using System;
using System.Collections;
/**//// <summary>
/// Session 的摘要说明。
/// </summary>
public class Session:DictionaryBase
...{
private static Session assion=null;
/**//// <summary>
/// 生成一个实例
/// </summary>
private Session()
...{
}
/**//// <summary>
/// 得到一个单身实例
/// </summary>
/// <returns>返回类型为Session</returns>
public static Session GetSession()
...{
if(Session.assion==null)
...{
Session.assion=new Session();
}
return Session.assion;
}
/**//// <summary>
/// 添加新成员
/// </summary>
/// <param name="newID">新字成员名字</param>
/// <param name="newmember">新成员</param>
public void Add(string newID,Object newmember)
...{
try
...{
Dictionary.Add(newID,newmember);
}
catch
...{
return;
}
}
/**//// <summary>
/// 删除成员
/// </summary>
/// <param name="memberID"></param>
public void Remove(string memberID)
...{
try
...{
Dictionary.Remove(memberID);
}
catch
...{
return;
}
}
/**//// <summary>
/// 本类的索引器
/// </summary>
/// <returns>返回Object成员</returns>
public Object this[string memberID]
...{
get
...{
try
...{
Object obj=(Object)Dictionary[memberID];
Dictionary.Remove(memberID);//销毁
return obj;
}
catch
...{
return null;//如果没有数据则返回null
}
}
set
...{
try
...{
this.Dictionary.Add(memberID,value);
}
catch
...{
return ;
}
}
}
}
先在来看如何使用的例子:
具体代码:
( 注意要添加引用上面程序编译成功的程序集:Session.dll)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
...{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox tbxAdd;
private System.Windows.Forms.TextBox tbxDel;
private Session a;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
...{
a=Session.GetSession();
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.listBox1 = new System.Windows.Forms.ListBox();
this.tbxAdd = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.tbxDel = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(136, 144);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 88);
this.listBox1.TabIndex = 0;
//
// tbxAdd
//
this.tbxAdd.Location = new System.Drawing.Point(144, 32);
this.tbxAdd.Name = "tbxAdd";
this.tbxAdd.Size = new System.Drawing.Size(112, 21);
this.tbxAdd.TabIndex = 1;
this.tbxAdd.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(336, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "add";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tbxDel
//
this.tbxDel.Location = new System.Drawing.Point(144, 72);
this.tbxDel.Name = "tbxDel";
this.tbxDel.Size = new System.Drawing.Size(112, 21);
this.tbxDel.TabIndex = 3;
this.tbxDel.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(336, 72);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "del";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(336, 176);
this.button3.Name = "button3";
this.button3.TabIndex = 5;
this.button3.Text = "look";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.tbxDel);
this.Controls.Add(this.button1);
this.Controls.Add(this.tbxAdd);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
...{
// a.Add(this.tbxAdd.Text.Trim(),this.tbxAdd);
a[this.tbxAdd.Text.Trim()]=this.tbxAdd;
}
private void button2_Click(object sender, System.EventArgs e)
...{
a.Remove(this.tbxDel.Text.Trim());
}
private void button3_Click(object sender, System.EventArgs e)
...{
this.listBox1.Items.Add(((TextBox)a[this.tbxAdd.Text.Trim()]).Text.Trim());
}
}
}
现在你可以到处使用Session.dll了,让它帮你处理页面传值。
相关文章推荐
- C#WinForm页面传值解决方法(仿web中Session)
- 记Outlook插件与Web页面交互的各种坑 (含c# HttpWebRequest 连接https 的完美解决方法)
- C# winform url 传值中文乱码解决方法
- C#web页面间传值的几种方法
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- C# WinForm页面切换导致闪烁的解决方法
- 页面上通过地址栏传值时出现乱码的两种解决方法
- vs2008与IIS 7.0使用在vista上时出现的问题及解决方法(Internet Explorer 无法显示该页面)(VS2008: IE Cannot Display Web Page)
- c# webapi POST 参数解决方法
- ASP.net的页面缓存导致Session更新不及时的解决方法
- 【分享】VS200X水晶报表边框效果在WEB页面不连续显示问题解决方法
- java 页面url传值中文乱码的解决方法
- web服务器集群(多台web服务器)session同步、共享的3种解决方法
- 转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题
- C# Webbrowser使用加载页面多少了内存泄露问题解决方法汇总
- .NET 4.0 WinForm无法引用System.Web.dll的解决方法
- IOS和安卓WEB页面,input输入框被软键盘遮挡解决方法
- 页面上通过地址栏传值时出现乱码的两种解决方法
- 关于在WinForm里用HttpWebRequest获得某个页面,并填写页面的textbox及点击button的方法
- Selenium webdriver 新跳转页面上找对象解决方法