⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 北大青鸟的教学程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
                result = MessageBox.Show("确实要删除该好友吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes) // 确认删除
                {
                    if (sbFriends.VisibleGroup == sbFriends.Groups[0])
                    {
                        string sql = string.Format(
                            "DELETE FROM Friends WHERE HostId={0} AND FriendId={1}",
                            UserHelper.loginId, Convert.ToInt32(sbFriends.SeletedItem.Tag));

                        try
                        {
                            SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                            DBHelper.connection.Open();
                            deleteResult = command.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        finally
                        {
                            DBHelper.connection.Close();
                        }
                        if (deleteResult == 1)
                        {
                            MessageBox.Show("好友已删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            sbFriends.SeletedItem.Parent.Items.Remove(sbFriends.SeletedItem);
                        }
                    }
                    else
                    {
                        MessageBox.Show("好友已删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        sbFriends.SeletedItem.Parent.Items.Remove(sbFriends.SeletedItem);
                    }                    
                }
            }
        }        

        // 将选中的人加为好友              
        private void tsmiAddFriend_Click(object sender, EventArgs e)
        {
            int result = 0;  // 操作结果
            string sql = string.Format(
                "INSERT INTO Friends (HostId, FriendId) VALUES({0},{1})",
                UserHelper.loginId, Convert.ToInt32(sbFriends.SeletedItem.Tag));

            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                result = command.ExecuteNonQuery();                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
            if (result == 1)
            {
                MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                sbFriends.SeletedItem.Parent.Items.Remove(sbFriends.SeletedItem);  
                ShowFriendList();   // 更新好友列表             
            }
            else
            {
                MessageBox.Show("添加失败,请稍候再试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // 退出
        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确实要退出吗?", "操作确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        // 可见组发生变化时,发出声音
        private void sbFriends_VisibleGroupChanged(SbGroupEventArgs e)
        {
            SoundPlayer player = new SoundPlayer("folder.wav");
            player.Play();
        }        

        /// <summary>
        /// 登录后显示个人的信息
        /// </summary>
        public void ShowSelfInfo()
        {
            string nickName = "";  // 昵称
            int faceId = 0;        // 头像索引
            bool error = false;    // 标识是否出现错误

            // 取得当前用户的昵称、头像
            string sql = string.Format(
                "SELECT NickName, FaceId FROM Users WHERE Id={0}",
                UserHelper.loginId);
            try
            {
                // 查询
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader dataReader = command.ExecuteReader();

                if (dataReader.Read())
                {
                    if (!(dataReader["NickName"] is DBNull))  // 判断数据库类型是否为空
                    {
                        nickName = Convert.ToString(dataReader["NickName"]);
                    }
                    faceId = Convert.ToInt32(dataReader["FaceId"]); 
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                error = true;                
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }

            // 根据操作数据库结果进行不同的操作
            if (error)
            {
                MessageBox.Show("服务器请求失败!请重新登录!", "意外错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            else
            {
                // 在窗体标题显示登录的昵称、号码
                this.Text = UserHelper.loginId.ToString();
                this.picFace.Image = ilFaces.Images[faceId];
                this.lblLoginId.Text = string.Format("{0}({1})", nickName, UserHelper.loginId.ToString());
            }            
        }

        /// <summary>
        /// 向我的好友组中添加我的好友列表
        /// </summary>
        private void ShowFriendList()
        {
            // 清空原来的列表
            sbFriends.Groups[0].Items.Clear();

            bool error = false;   // 标识数据库是否出错

            // 查找有哪些好友
            string sql = string.Format(
                "SELECT FriendId,NickName,FaceId FROM Users,Friends WHERE Friends.HostId={0} AND Users.Id=Friends.FriendId",
                UserHelper.loginId);
            try
            {
                // 执行查询
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader dataReader = command.ExecuteReader();

                // 循环添加好友列表
                while (dataReader.Read())
                {
                    // 创建一个SideBar项
                    SbItem item = new SbItem((string)dataReader["NickName"], (int)dataReader["FaceId"]);
                    item.Tag = (int)dataReader["FriendId"]; // 将号码放在Tag属性中

                    // SideBar中的组可以通过数组的方式访问,按照添加的顺序索引从0开始
                    // Groups[0]表示SideBar中的第一个组,也就是“我的好友”组
                    sbFriends.Groups[0].Items.Add(item); // 向SideBar的“我的好友”组中添加项
                }

                dataReader.Close();
            }
            catch (Exception ex)
            {
                error = true;
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
            // 出错了
            if (error)
            {
                MessageBox.Show("服务器发生意外错误!请尝试重新登录", "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }        
         
        /// <summary>
        /// 判断发消息的人是否在列表中
        /// </summary>        
        private bool HasShowUser(int loginId)
        {
            bool find = false;  // 表示是否在当前显示出的用户列表中找到了该用户

            // 循环 SideBar 中的2个组,寻找发消息的人是否在列表中
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < sbFriends.Groups[i].Items.Count; j++)
                {
                    if (Convert.ToInt32(sbFriends.Groups[i].Items[j].Tag) == loginId)
                    {
                        find = true;                        
                    }
                }
            }
            return find;
        }
       
        /// <summary>
        /// 更新陌生人列表
        /// </summary>        
        private void UpdateStranger(int loginId)
        {
            // 选出这个人的基本信息
            string sql = "SELECT NickName, FaceId FROM Users WHERE Id=" + loginId;
            bool error = false; // 用来标识是否出现错误
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();                
                SqlDataReader dataReader = command.ExecuteReader(); // 查询
                if (dataReader.Read())
                {
                    SbItem item = new SbItem((string)dataReader["NickName"], (int)dataReader["FaceId"]);
                    item.Tag = this.fromUserId;           // 将Id记录在Tag属性中
                    sbFriends.Groups[1].Items.Add(item);  // 向陌生人组中添加项
                }
                sbFriends.VisibleGroup = sbFriends.Groups[1];  // 设定陌生人组为可见组
            }
            catch (Exception ex)
            {
                error = true;
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }

            // 出错了
            if (error)
            {
                MessageBox.Show("服务器出现意外错误!", "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -