zoukankan      html  css  js  c++  java
  • winform实现Session功能(保存用户信息)

    问题描述:在winform中想实现像BS中类似Session的功能,放上需要的信息,在程序中都可以访问到。

    解决方案:由于自己很长时间没有做过winform的程序,一时间竟然手足无措起来。后来发现winform实现起来也十分简单。

    一、在窗体Form1中创建static 用户类

     1         /// <summary>
     2         /// 当前登录用户信息
     3         /// </summary>
     4         public static class CurrentUser
     5         {
     6             /// <summary>
     7             /// 用户名
     8             /// </summary>
     9             public static string userName { get; set; }
    10 
    11 
    12             /// <summary>
    13             /// 登录时间
    14             /// </summary>
    15             public static DateTime LoginTime { get; set; }
    16 
    17         }

           在page_load中为它赋值

    1         private void Form1_Load(object sender, EventArgs e)
    2         {
    3             CurrentUser.userName = "test";
    4             CurrentUser.LoginTime = DateTime.Now;
    5         }

      在点击事件中打开新窗体

    1         private void button1_Click(object sender, EventArgs e)
    2         {
    3             int x = this.Location.X;
    4             int y = this.Location.Y;
    5             this.Hide();
    6             Form2 secondForm = new Form2();
    7             secondForm.Location = new Point(x, y);
    8             secondForm.Show();
    9         }

      效果如图:

    二、在form2窗体中直接访问静态类的数值即可

    1         private void Form2_Load(object sender, EventArgs e)
    2         {
    3             this.txtUserName.Text = Form1.CurrentUser.userName;
    4             this.txtLoginTime.Text = Form1.CurrentUser.LoginTime.ToString("yyyy-MM-dd HH:mm:ss");
    5         }

      效果:

    写在后面的话:其实现在想想,winform并不需要session,因为我的理解中session是保存用户和服务器之间的会话信息,尤其是多用户访问网站时,尤显得重要。但是winform程序类似于单机软件,相当于一个用户对winform程序,也不知道这样理解对不对。对于winform的开发理解得太浅了,有时候会经常把web开发的思路带到winform中,发现其实两者还是有很大差别的。继续要努力学习呢~

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    《EffectiveJava中文第二版》 高清PDF下载
    《MoreEffectiveC++中文版》 pdf 下载
    《啊哈c语言》 高清 PDF 下载
  • 原文地址:https://www.cnblogs.com/jas0203/p/10009352.html
Copyright © 2011-2022 走看看