温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#怎么实现窗体通讯录系统功能

发布时间:2022-04-18 13:45:13 来源:亿速云 阅读:181 作者:iii 栏目:开发技术

本文小编为大家详细介绍“C#怎么实现窗体通讯录系统功能”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#怎么实现窗体通讯录系统功能”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

具体如下:

C#怎么实现窗体通讯录系统功能

1.首先创建DBhelp类用来连接数据库

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
 
namespace TongxunLu
{
  public static   class DBHelp
    {
       static  string sqlcon = "Data Source=.;Initial Catalog=TXL;Integrated Security=True";
        public static  SqlConnection con = new System.Data.SqlClient.SqlConnection(sqlcon);
       public static  SqlCommand cmd = new SqlCommand();
      
    }
}

登录页面:

C#怎么实现窗体通讯录系统功能

 双加登录里面代码:

 private void btnOK_Click(object sender, EventArgs e)
        {
            //验证用户名与密码非空
            if (txtUserName.Text == "")
            {
                MessageBox.Show("用户名不能为空,请输入!");
                txtUserName.Focus();
                return;
            }
            if (txtUserPwd.Text == "")
            {
                MessageBox.Show("密码不能为空,请输入!");
                txtUserPwd.Focus();
                return;
            }
            //定义链接字符串和链接对象        
            string sqlcon = "Data Source=.;Initial Catalog=TXL;Integrated Security=True";
            SqlConnection con = new System.Data.SqlClient.SqlConnection(sqlcon);
            //操作数据库,实现登录功能
            try
            {
                con.Open();
                string sqlcomm = "select  distinct COUNT(*) from Users where UserName='" + txtUserName.Text + "'  and Password='" + txtUserPwd.Text + "'";
 
                SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlcomm, con);
                //cmd.Connection = con;
                //cmd.CommandText = sqlcomm;
 
                if ((int)cmd.ExecuteScalar() == 1)
                {
                   // MessageBox.Show("登录成功");
                    Users.UserName = txtUserName.Text;
                    FrmMain fmain = new FrmMain();
                    fmain.Show();
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
 
                // MessageBox.Show("数据库打开成功", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            finally
            {
                con.Close();
                // MessageBox.Show("数据库成功关闭", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }              
        }

双击取消,里面代码:

private void btnCacel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

C#怎么实现窗体通讯录系统功能

输入用户表里面的信息,进行登录:

C#怎么实现窗体通讯录系统功能

2.创建主页面:

C#怎么实现窗体通讯录系统功能

添加菜单栏、工具栏、状态栏

菜单栏控件:MenuStrip  以mus开头命名

项目名称以tsm开头 项目里的项目以tsmi开头命名

工具栏控件:ToolStrip  项显示图片和文本更改属性DisplayStyle,工具栏:以tst开头

状态栏控件:StatusStrip,状态栏:以tss命名开头

C#怎么实现窗体通讯录系统功能

C#怎么实现窗体通讯录系统功能

当页面加载那个用户登录,状态用Label控件就显示谁的名字,代码:

 private void FrmMain_Load(object sender, EventArgs e)
        {
            //接受登录名
            toolStripStatusLabel2.Text += Users.UserName;
            toolStripStatusLabel3.Text += GetNum(Users.UserName).ToString();
           LoadGroup();
        }

主页面里面的详细功能代码,如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace TongxunLu
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        //显示添加窗体
        public void add()
        {
            frmAdd fadd = new frmAdd();
 
            fadd.Show();
        }
        //显示修改窗体
        public void edit()
        {
            frmEdit fedit = new frmEdit();
            fedit.Show();
        }
        //显示查询窗体
        public void seacher()
        {
            frmSeach fseacher=new frmSeach ();
            fseacher.Show();
        }
        //统计登录联系人的个数
        public int GetNum(string str1)
        {
            int num = 0;
            try
            {
                DBHelp.con.Open();
                string sqlcomm = string.Format("select count(*) from BUsicInfo where UserName='{0}'",str1);
                DBHelp.cmd.Connection = DBHelp.con;
                DBHelp.cmd.CommandText = sqlcomm;
                num = (int)DBHelp.cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelp.con.Close();
            }
            return num;
 
        }
        //统计登录联系人分组的个数
        public int GetGroupsNum()
        {
            int num = 0;
            try
            {
                DBHelp.con.Open();
                string sqlcomm = string.Format("select count(*) from BUsicInfo where UserName='{0}' and  groups='{1}'",Users.UserName,treeView1.SelectedNode.Text );
                DBHelp.cmd.Connection = DBHelp.con;
                DBHelp.cmd.CommandText = sqlcomm;
                num = (int)DBHelp.cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelp.con.Close();
            }
            return num;
 
        }
 
        //加载treeview控件
 
        public void LoadGroup()
        {
            try
            {
                DBHelp.con.Open();
                string sqlcomm = string.Format("select groups from busicInfo where userName='{0}'",Users.UserName);
                DBHelp.cmd.Connection = DBHelp.con;
                DBHelp.cmd.CommandText = sqlcomm;
                SqlDataReader dr = DBHelp.cmd.ExecuteReader();
                while(dr.Read())
                {
                    treeView1.Nodes.Add(dr[0].ToString());
                }
                dr.Close();
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelp.con.Close();
            }
        }
 
        //加载ListView控件
        public void loadList()
        {
             if (treeView1.Nodes.Count == 0)
            {
                MessageBox.Show("请重新加载");
                listView1.Items.Clear();
                return;
            }
            if (treeView1.SelectedNode.Index >= 0)
            {
                listView1.Clear();
                listView1.Columns.Add("姓名", 100);
                listView1.Columns.Add("工作单位", 100);
                listView1.Columns.Add("联系电话", 100);
                listView1.Columns.Add("电子邮箱", 100);
                listView1.Columns.Add("QQ", 100);
 
                try
                {
                    DBHelp.con.Open();
                    string sqlcomm = string.Format("select * from busicInfo where userName='{0}' and Groups='{1}'",Users.UserName,treeView1.SelectedNode.Text);
                    DBHelp.cmd.CommandText = sqlcomm;
                    DBHelp.cmd.Connection = DBHelp.con;
                    SqlDataReader dr = DBHelp.cmd.ExecuteReader();
                    while(dr.Read())
                    {
                        ListViewItem lvi = new ListViewItem();
                        lvi.Tag = dr[0];
                        lvi.Text = dr[3].ToString();
                        lvi.SubItems.Add(dr[4].ToString());
                        lvi.SubItems.Add(dr[5].ToString());
                        lvi.SubItems.Add(dr[6].ToString());
                        lvi.SubItems.Add(dr[7].ToString());
                        listView1.Items.Add(lvi); 
                    }
                    dr.Close();
 
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    DBHelp.con.Close();
                }
            }
            
        }
        
        private void 增加联系人ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            add();
        }
 
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            add();
        }
 
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            seacher();
        }
 
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //接受登录名
            toolStripStatusLabel2.Text += Users.UserName;
            toolStripStatusLabel3.Text += GetNum(Users.UserName).ToString();
           LoadGroup();
        }
 
        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
 
        }
 
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
           loadList();
           toolStripStatusLabel3.Text = GetGroupsNum().ToString();
            
        }
        
    }
}

3.添加联系人页面

C#怎么实现窗体通讯录系统功能

Tablcontrol控件:分页选项:用来分页:基本信息和其他信息 

双击确定,里面代码:

 private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DBHelp.con.Open();
 
          DBHelp.cmd.CommandText =string.Format("insert into BusicInfo(userName,Groups,Name,workUnit,Phone,Email,QQ)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",Users.UserName,cmbGroup.SelectedValue,txtName.Text,txtWorkUnit.Text,txtPhone.Text,txtEmail.Text,txtQQ.Text);
          DBHelp.cmd.Connection = DBHelp.con;
          if ((int)DBHelp.cmd.ExecuteNonQuery() == 1)
          {
              MessageBox.Show("添加成功");
 
          }
          else
          {
              MessageBox.Show("失败");
          }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelp.con.Close();
            }
 
        }

加载页面,双击压面,里面代码

 public void LoadGroup()
        {
            try
            {
                DBHelp.con.Open();
                string sqlcomm = string.Format("select groups from busicInfo where userName='{0}'", Users.UserName);
                DBHelp.cmd.Connection = DBHelp.con;
                DBHelp.cmd.CommandText = sqlcomm;
                SqlDataReader dr = DBHelp.cmd.ExecuteReader();
                while (dr.Read())
                {
                    cmbGroup.Items.Add(dr[0].ToString());
                }
                dr.Close();
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelp.con.Close();
            }
        }
 
        private void frmAdd_Load(object sender, EventArgs e)
        {
            LoadGroup();
        }

C#怎么实现窗体通讯录系统功能

C#怎么实现窗体通讯录系统功能

4.修改页面

C#怎么实现窗体通讯录系统功能

5.查询页面

C#怎么实现窗体通讯录系统功能

双击查询按钮,里面代码:

private void btnSeach_Click(object sender, EventArgs e)
        {
 
            string sqlcomm = "";
            if(txtUserName.Text==""&&txtUserPhone.Text=="")
            {
                sqlcomm = string.Format("select * from  BusicInfo ");
            
            }
            else if (txtUserName.Text == "")
            {
                sqlcomm = string.Format("select * from  BusicInfo  where Phone='%{0}%' ", txtUserPhone.Text);
 
            }
            else if (txtUserPhone.Text == "")
            {
                sqlcomm = string.Format("select * from  BusicInfo  where UserName='{0}%' ", txtUserName.Text);
            }
            else
            {
                sqlcomm = string.Format("select * from  BusicInfo  where UserName='{0}%' and Phone='{1}%' ", txtUserName.Text, txtUserPhone.Text);
            }
           
            try
            {
                DBHelp.con.Open();
 
                SqlDataAdapter da = new SqlDataAdapter(sqlcomm,DBHelp.con);
                DataSet ds = new DataSet();
               
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelp.con.Close();
            }
 
        }

读到这里,这篇“C#怎么实现窗体通讯录系统功能”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI