c# winform 创建日志登录界面

一.创建一个用于登录的Login的复合控件

1.右击项目文件,点击添加用户控件,设置为控件名为Login。

2.拉动两个lable控件,两个textBox控件,一个button,一个CheckBox控件。

3.将控件的权限(Modifiers)设置为Public。

二.将控件CheckBox与textBox2(显示密码的textBox)的控件绑定点击事件。

cs 复制代码
 private void checkBox1_CheckedChanged(object sender, EventArgs e)
 {
     if (checkBox1.Checked) 
     {
      textBox2.PasswordChar = '\0';
     }
     else { textBox2.PasswordChar = '*'; }
 }

三.重新生成项目,点击工具箱打开,打开自定义控件Login拖入到Form1中,添加listBox控件。

四.为Login的button控件添加自定义的点击事件。(注意设置Login中的button控件权限为公开)

五.给Form1窗口添加ContextMenuStrip菜单栏,里面设置一个删除的选项

六.为listBox添加点击事件

cs 复制代码
   private void listBox1_MouseDown(object sender, MouseEventArgs e)
   {
      
       if (e.Button == MouseButtons.Right)
       {
           if (listBox1.SelectedItems.Count != 0)
           {
               删除ToolStripMenuItem.Enabled = true;
           }
           else
           {
               删除ToolStripMenuItem.Enabled = false;
           }
           this.contextMenuStrip1.Show(listBox1, e.Location);

       }

   }

七.为删除按钮添加点击事件

cs 复制代码
  private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  {
      listBox1.Items.Remove(listBox1.SelectedItem);
      string str = JsonConvert.SerializeObject(listBox1.Items, Formatting.Indented);
      File.WriteAllText("登陆日志.txt", str);
      listBox1.ClearSelected();
  }

八.总代码

cs 复制代码
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace winform对json数据的存储和读取
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            login1.button1.Click += new EventHandler (this.A);
        }

      
        private void A(object sender, EventArgs e) 
        {
            if (login1.textBox1.Text != "" && login1.textBox2.Text != "") 
            {
               listBox1.Items.Add(DateTime.Now.ToString("yyyy-MM-dd  HH:mm:ss  ") + login1.textBox1.Text+"登陆了此页面");
                string str = JsonConvert.SerializeObject(listBox1.Items, Formatting.Indented);
                File.WriteAllText("登陆日志.txt",str);
            }
            else { MessageBox.Show("账号密码不能为空"); }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
            string str = JsonConvert.SerializeObject(listBox1.Items, Formatting.Indented);
            File.WriteAllText("登陆日志.txt", str);
            listBox1.ClearSelected();
        }

      

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
           
            if (e.Button == MouseButtons.Right)
            {
                if (listBox1.SelectedItems.Count != 0)
                {
                    删除ToolStripMenuItem.Enabled = true;
                }
                else
                {
                    删除ToolStripMenuItem.Enabled = false;
                }
                this.contextMenuStrip1.Show(listBox1, e.Location);

            }

        }

       
    }
}
相关推荐
java—大象1 分钟前
基于JavaWeb开发的java+Springboot操作系统教学交流平台详细设计实现
java·开发语言·spring boot
DreamByte26 分钟前
Python Tkinter小程序
开发语言·python·小程序
覆水难收呀35 分钟前
三、(JS)JS中常见的表单事件
开发语言·前端·javascript
阿华的代码王国38 分钟前
【JavaEE】多线程编程引入——认识Thread类
java·开发语言·数据结构·mysql·java-ee
__water41 分钟前
『功能项目』QFrameWork框架重构OnGUI【63】
c#·unity引擎·重构背包框架
繁依Fanyi1 小时前
828 华为云征文|华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
运维·服务器·开发语言·人工智能·pytorch·华为·华为云
Crazy Struggle1 小时前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
weixin_486681141 小时前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言1 小时前
C++入门
开发语言·c++
怀九日1 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·