C#WinForm页面传值解决方法(仿web中Session)

昨天同事问我有没有一个安全简单的办法实现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 窗体设计器生成的代码 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(136144);
            
this.listBox1.Name = "listBox1";
            
this.listBox1.Size = new System.Drawing.Size(12088);
            
this.listBox1.TabIndex = 0;
            
// 
            
// tbxAdd
            
// 
            this.tbxAdd.Location = new System.Drawing.Point(14432);
            
this.tbxAdd.Name = "tbxAdd";
            
this.tbxAdd.Size = new System.Drawing.Size(11221);
            
this.tbxAdd.TabIndex = 1;
            
this.tbxAdd.Text = "";
            
// 
            
// button1
            
// 
            this.button1.Location = new System.Drawing.Point(33624);
            
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(14472);
            
this.tbxDel.Name = "tbxDel";
            
this.tbxDel.Size = new System.Drawing.Size(11221);
            
this.tbxDel.TabIndex = 3;
            
this.tbxDel.Text = "";
            
// 
            
// button2
            
// 
            this.button2.Location = new System.Drawing.Point(33672);
            
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(336176);
            
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(614);
            
this.ClientSize = new System.Drawing.Size(528273);
            
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了,让它帮你处理页面传值。

 

posted on 2008-08-06 13:42  yatasoft  阅读(6242)  评论(4编辑  收藏  举报

导航