private void Edit_bt_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                My_Database data = new My_Database();

                SqlCommand command = new SqlCommand("UPDATE Add_Student SET firstname = @Fname, lastname = @Lname, birthday = @Bdate, gender = @Gender, address = @Address, phone = @Phone, picture = @Picture WHERE id = @ID", data.GetConnection);
                command.Parameters.Add("@Fname", SqlDbType.NVarChar).Value   = Firstname.Text;
                command.Parameters.Add("@Lname", SqlDbType.NVarChar).Value   = Lastname.Text;
                command.Parameters.Add("@Bdate", SqlDbType.DateTime).Value   = Birthday_picker.DateTime;
                command.Parameters.Add("@Address", SqlDbType.NVarChar).Value = Address_student.Text;
                command.Parameters.Add("@Phone", SqlDbType.NVarChar).Value   = Phone_student.Text;
                ImageConverter converter = new ImageConverter();
                byte[]         image     = (byte[])converter.ConvertTo(Picture_Student.Image, typeof(byte[]));
                command.Parameters.Add("@Picture", SqlDbType.Image).Value = image;
                command.Parameters.Add("@ID", SqlDbType.Int).Value        = ID_student.Text;
                data.Openconnection();
                if (Check_male.Checked)
                {
                    command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = "Male";
                }
                else if (Check_female.Checked)
                {
                    command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = "Female";
                }

                if (command.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Complete", "Edit completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("ERROR", "Edit failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                data.Closeconnection();
            }

            catch
            {
                MessageBox.Show("Error, Please try this again!", "Edit Student", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void GetImageIDUser()
        {
            Contacts       contact  = new Contacts();
            My_Database    data     = new My_Database();
            SqlDataAdapter SDA      = new SqlDataAdapter();
            DataTable      table    = new DataTable();
            SqlCommand     commnand = new SqlCommand("SELECT * FROM New_User WHERE Id = @id", data.GetConnection);

            commnand.Parameters.Add("@id", SqlDbType.Int).Value = Globals.GlobalsUserID;
            SDA.SelectCommand = commnand;
            SDA.Fill(table);
            if (table.Rows.Count > 0)
            {
                byte[]       pic     = (byte[])table.Rows[0]["picture"];
                MemoryStream picture = new MemoryStream(pic);
                contact.Picture_User.Image         = Image.FromStream(picture);
                contact.Wellcome_LabelControl.Text = "Wellcome back " + table.Rows[0]["firstname"].ToString() +
                                                     " with username: "******"username"].ToString();
            }
        }
Exemple #3
0
        public DataTable GetContactsByID(int ID)
        {
            DataTable table = new DataTable();

            try
            {
                My_Database data    = new My_Database();
                SqlCommand  command = new SqlCommand("SELECT ID, Firstname, Fastname, Group_id, Gender, Birthday, Phone, Email, Facebook, Address, City, Picture, User_id " +
                                                     "FROM Contact WHERE ID = @id", data.GetConnection);
                command.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                SqlDataAdapter SDA = new SqlDataAdapter(command);
                SDA.Fill(table);
                return(table);
            }
            catch
            {
                XtraMessageBox.Show("The format is incorrect, please check again!");
                return(table);
            }
        }
Exemple #4
0
        public Main_Window(string username, Socket s)
        {
            InitializeComponent();
            //数据库连接
            connection          = My_Database.Connect_Database();
            label_username.Text = username;
            user_name           = username;
            client_socket       = s;
            int listen_port;

            listen_port = Convert.ToInt32(username.Substring(6)) + 50000;
            //开始监听
            P2P_Communication.Begin_Listening(listen_port, user_name, this);
            string search_text = "Select friend_list from friend_table where username=" + user_name;
            object friend_all  = My_Database.SQLite_Select(search_text, connection).ToString();

            //string[] friend_array;
            //friend_list = friend_all.ToString().Split('.');
            if (friend_all.ToString().Length > 10)
            {
                friend_list = friend_all.ToString().Substring(11);
            }
            get_friend_list(true);
            get_group_list();
            //创建进程刷新好友在线状态
            threadstart_query         = new ThreadStart(query_friend_state);
            thread_query              = new Thread(threadstart_query);
            thread_query.IsBackground = true;
            thread_query.Start();
            chat_select = 0;
            unread_num  = 0;
            listView_unread.Hide();
            listView_group.Hide();
            button_approve.Hide();
            button_refuse.Hide();
            label1.Hide();
            label2.Hide();
            button_refuse.Enabled  = false;
            button_approve.Enabled = false;
            //unread_msg = null;
        }
Exemple #5
0
        //找到用户列表对应的群聊名称
        private string find_group(string recv_str)
        {
            string            search_text    = "Select * from group_table";
            List <chat_group> all_chat_group = My_Database.SQLite_Select_all(search_text, connection);
            List <string>     all_name       = recv_str.Split('.').ToList();

            all_name.RemoveAt(0);
            all_name.RemoveAt(all_name.Count() - 1);
            foreach (chat_group group in all_chat_group)
            {
                List <string> all_user = group.user.Split('.').ToList();
                all_user.Sort();
                List <string> temp_user = new List <string>(all_name);
                temp_user.Sort();
                if (all_user.All(temp_user.Contains) && all_user.Count == temp_user.Count)
                {
                    return(group.group_name);
                }
            }
            return("");
        }
        public object TotalFemaleStudent()
        {
            My_Database data    = new My_Database();
            SqlCommand  command = new SqlCommand(@"select count(*) from Add_Student where gender = 'female'", data.Getcon);

            data.Openconnection();
            command.CommandText = @"select count(*) from Add_Student where gender = 'female'";
            try
            {
                return(command.ExecuteScalar());
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message);
            }
            finally
            {
                data.Closeconnection();
            }
            return(command.ExecuteScalar());
        }
        public void EditContact()
        {
            My_Database data    = new My_Database();
            SqlCommand  command = new SqlCommand("UPDATE Contact SET Firstname = @fn, Lastname = @ln, Group_id = @group, Gender = @gen, Birthday = @day, " +
                                                 "Phone = @phone, Email = @email, Facebook = @fb, Address = @add, City = @city, Picture = @pic WHERE ID = @id", data.GetConnection);

            command.Parameters.Add("@fn", SqlDbType.NVarChar).Value    = TextEdit_Fname.Text;
            command.Parameters.Add("@ln", SqlDbType.NVarChar).Value    = TextEdit_Lastname.Text;
            command.Parameters.Add("@group", SqlDbType.Int).Value      = Convert.ToInt32(table.Rows[ComboBox_GroupId.SelectedIndex][0].ToString());
            command.Parameters.Add("@gen", SqlDbType.NVarChar).Value   = ComboBoxEdit_Gender.Text;
            command.Parameters.Add("@day", SqlDbType.DateTime).Value   = DateEdit_Birthday.DateTime;
            command.Parameters.Add("@phone", SqlDbType.Int).Value      = Convert.ToInt32(TextEdit_Phone.Text);
            command.Parameters.Add("@email", SqlDbType.NVarChar).Value = TextEdit_Mail.Text;
            command.Parameters.Add("@fb", SqlDbType.NVarChar).Value    = TextEdit_Fb.Text;
            command.Parameters.Add("@add", SqlDbType.NVarChar).Value   = TextEdit_Address.Text;
            command.Parameters.Add("@city", SqlDbType.NVarChar).Value  = TextEdit_City.Text;
            ImageConverter converter = new ImageConverter();

            byte[] image = (byte[])converter.ConvertTo(PictureEdit_User.Image, typeof(byte[]));
            command.Parameters.Add("@pic", SqlDbType.Image).Value = image;
            command.Parameters.Add("@id", SqlDbType.Int).Value    = Convert.ToInt32(TextEdit_ID.Text);
            data.Openconnection();
            if (command.ExecuteNonQuery() == 1)
            {
                XtraMessageBox.Show("Edit Successful!", "Edit Contacts", MessageBoxButtons.OK, MessageBoxIcon.Information);
                data.Closeconnection();
            }
            else
            {
                XtraMessageBox.Show("Edit Failed!", "Edit Contacts", MessageBoxButtons.OK, MessageBoxIcon.Error);
                data.Closeconnection();
            }

            //}

            //catch
            //{
            //    XtraMessageBox.Show("Edit Failed!", "Edit Contacts", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }
Exemple #8
0
        public int GetFailNumber()
        {
            My_Database db = new My_Database();

            try
            {
                db.Openconnection();
                SqlCommand command = new SqlCommand()
                {
                    Connection  = db.GetConnection,
                    CommandText = "select count(A.Student_id) " +
                                  "from (SELECT Score.Student_id, AVG(Score.student_score) as AvgScore " +
                                  "FROM Add_Student inner join Score on Add_Student.ID = Score.Student_id " +
                                  "Group by Score.Student_id " +
                                  "HAVING AVG(Score.Student_score) < 5 and AVG(Score.Student_score) >= 0) as A"
                };

                int result = 0;
                try
                {
                    result = (int)command.ExecuteScalar();
                }
                catch (Exception)
                {
                    return(0);
                }

                db.Closeconnection();

                return(result);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                db.Closeconnection();
            }
        }
Exemple #9
0
        public bool CheckUserID(int user_id)
        {
            string      query   = "SELECT * FROM Contact WHERE user_id = @user_id";
            My_Database data    = new My_Database();
            SqlCommand  command = new SqlCommand(query, data.GetConnection);

            command.Parameters.Add("@user_id", SqlDbType.Int).Value = user_id;

            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable      table   = new DataTable();

            adapter.Fill(table);

            if (table.Rows.Count > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #10
0
        public void Remove()
        {
            try
            {
                DialogResult delete = MessageBox.Show("Are you sure delete student with id: " + ID_student.Text, "",
                                                      MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (delete == DialogResult.Yes)
                {
                    My_Database dataBase = new My_Database();

                    SqlCommand command = new SqlCommand(
                        "DELETE FROM Add_Student" +
                        " WHERE ID = @id", dataBase.GetConnection);
                    command.Parameters.Add("@ID", SqlDbType.Int).Value = ID_student.Text;

                    dataBase.Openconnection();
                    if (command.ExecuteNonQuery() == 1)
                    {
                        MessageBox.Show("Complete", "Delete complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ID_student.Text       = "";
                        Firstname.Text        = "";
                        Lastname.Text         = "";
                        Phone_lb.Text         = "";
                        Address_student.Text  = "";
                        Picture_Student.Image = null;
                    }
                    else
                    {
                        MessageBox.Show("ERROR", "Delete fails", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    dataBase.Closeconnection();
                }
            }
            catch (Exception E)
            {
                Console.WriteLine(E.Message);
                throw;
            }
        }
Exemple #11
0
 public bool UpdateContact(string fname, string lname, int group, string gender, DateTime bday, int phone,
                           string email, string fbook, string address, string city, MemoryStream picture, int userid)
 {
     try
     {
         My_Database data    = new My_Database();
         SqlCommand  command = new SqlCommand("UPDATE Contact SET Firstname = @fn, Lastname = @ln, Group_id = @group, Gender = @gen, Birthday = @day, " +
                                              "Phone = @phone, Email = @email, Facebook = @fb, Address = @add, City = @city, Picture = @pic, User_id = @id", data.GetConnection);
         command.Parameters.Add("@fn", SqlDbType.NVarChar).Value    = fname;
         command.Parameters.Add("@ln", SqlDbType.NVarChar).Value    = lname;
         command.Parameters.Add("@group", SqlDbType.Int).Value      = group;
         command.Parameters.Add("@gen", SqlDbType.NVarChar).Value   = gender;
         command.Parameters.Add("@day", SqlDbType.DateTime).Value   = bday;
         command.Parameters.Add("@phone", SqlDbType.NVarChar).Value = phone;
         command.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;
         command.Parameters.Add("@fb", SqlDbType.NVarChar).Value    = fbook;
         command.Parameters.Add("@add", SqlDbType.NVarChar).Value   = address;
         command.Parameters.Add("@city", SqlDbType.NVarChar).Value  = city;
         command.Parameters.Add("@pic", SqlDbType.Image).Value      = picture.ToArray();
         command.Parameters.Add("@id", SqlDbType.Int).Value         = userid;
         data.Openconnection();
         if (command.ExecuteNonQuery() == 1)
         {
             data.Closeconnection();
             return(true);
         }
         else
         {
             data.Closeconnection();
             return(false);
         }
     }
     catch
     {
         XtraMessageBox.Show("The format is incorrect, please check again!");
         return(false);
     }
 }
Exemple #12
0
 public bool InsertContact(string fname, string lname, int group, string gender, DateTime bday, int phone,
                           string email, string fbook, string address, string city, MemoryStream picture, int id)
 {
     try
     {
         My_Database data    = new My_Database();
         SqlCommand  command = new SqlCommand("INSERT INTO Contact (ID, Firstname, Lastname, Group_id, Gender, Birthday, Phone, Email, Facebook, Address, City, Picture)" +
                                              "VALUES (@id, @fn, @lname, @group, @gen, @day, @phone, @email, @fb, @add, @city, @pic)", data.GetConnection);
         command.Parameters.Add("@lname", SqlDbType.NVarChar).Value = lname;
         command.Parameters.Add("@group", SqlDbType.Int).Value      = group;
         command.Parameters.Add("@gen", SqlDbType.NVarChar).Value   = gender;
         command.Parameters.Add("@day", SqlDbType.DateTime).Value   = bday;
         command.Parameters.Add("@fn", SqlDbType.NVarChar).Value    = fname;
         command.Parameters.Add("@phone", SqlDbType.Int).Value      = phone;
         command.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;
         command.Parameters.Add("@fb", SqlDbType.NVarChar).Value    = fbook;
         command.Parameters.Add("@add", SqlDbType.NVarChar).Value   = address;
         command.Parameters.Add("@city", SqlDbType.NVarChar).Value  = city;
         command.Parameters.Add("@pic", SqlDbType.Image).Value      = picture.ToArray();
         command.Parameters.Add("@id", SqlDbType.Int).Value         = id;
         data.Openconnection();
         if (command.ExecuteNonQuery() == 1)
         {
             data.Closeconnection();
             return(true);
         }
         else
         {
             data.Closeconnection();
             return(false);
         }
     }
     catch
     {
         XtraMessageBox.Show("The format is incorrect, please check again!");
         return(false);
     }
 }
        public bool Insert_User(int id, string fname, string lname, string username, string pass, MemoryStream picture, string role)
        {
            My_Database data    = new My_Database();
            SqlCommand  command = new SqlCommand("INSERT INTO New_User (Id, firstname, lastname, username, password, picture, role)" +
                                                 "VALUES (@ID, @fname, @lname, @uname, @pass, @picture, @ro)", data.GetConnection);

            command.Parameters.Add("@ID", SqlDbType.Int).Value        = id;
            command.Parameters.Add("@fname", SqlDbType.NChar).Value   = fname;
            command.Parameters.Add("@lname", SqlDbType.NChar).Value   = lname;
            command.Parameters.Add("@uname", SqlDbType.NChar).Value   = username;
            command.Parameters.Add("@pass", SqlDbType.NChar).Value    = pass;
            command.Parameters.Add("@picture", SqlDbType.Image).Value = picture.ToArray();
            command.Parameters.Add("@ro", SqlDbType.NChar).Value      = role;
            data.Openconnection();
            if (command.ExecuteNonQuery() == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #14
0
        private void button_login_Click(object sender, EventArgs e)
        {
            string username = textBox_username.Text;
            //Server_Connection get_server = new Server_Connection();
            Socket client_socket = Server_Connection.Connect_Server();
            string search_text   = "Select username from user_table where username="******"Select password from user_table where username="******"该用户还未注册", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (result2.ToString() != textBox_password.Text)
            {
                MessageBox.Show(this, "密码错误", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                string info;
                info = Server_Connection.Loginto_Server(username, client_socket);
                if (info == "lol")
                {
                    MessageBox.Show(this, "登录成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //登陆成功,打开主窗口界面
                    Main_Window main_window = new Main_Window(username, client_socket);
                    main_window.Show();
                    connection.Close();
                    this.Hide();
                }
                else if (info == "Incorrect login No.")
                {
                    MessageBox.Show(this, "该用户名不规范", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
        public bool InsertCourse(int id, string label, int period, string description)
        {
            My_Database db = new My_Database();

            try
            {
                SqlCommand command = new SqlCommand("INSERT INTO Course (id, label, period, description) "
                                                    + "VALUES (@id, @label, @period, @description)", db.GetConnection);

                command.Parameters.Add("@id", SqlDbType.Int).Value           = id;
                command.Parameters.Add("@label", SqlDbType.VarChar).Value    = label;
                command.Parameters.Add("@period", SqlDbType.Int).Value       = period;
                command.Parameters.Add("@description", SqlDbType.Text).Value = description;

                db.Openconnection();
                if (command.ExecuteNonQuery() == 1)
                {
                    db.Closeconnection();
                    return(true);
                }
                else
                {
                    db.Closeconnection();
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);

                throw;
            }
            finally
            {
                db.Closeconnection();
            }
        }
        private void remove_bt_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult delete = MessageBox.Show("Are you sure delete the course with id: " + Id_tb.Text, "",
                                                      MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (delete == DialogResult.Yes)
                {
                    My_Database dataBase = new My_Database();

                    SqlCommand command = new SqlCommand(
                        "DELETE FROM Course" +
                        " WHERE Id = @ID", dataBase.Getcon);
                    command.Parameters.Add("@ID", SqlDbType.Int).Value = Id_tb.Text;
                    dataBase.Openconnection();
                    if (command.ExecuteNonQuery() == 1)
                    {
                        MessageBox.Show("Complete", "Delete complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("ERROR", "Delete fails", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    dataBase.Closeconnection();
                }
            }
            catch (Exception E)
            {
                Console.WriteLine(E.Message);
                throw;
            }
            Id_tb.Clear();
            Label_tb.Clear();
            Period_tb.Clear();
            dec_richtext.Clear();
        }
        public bool AddThisCourse()
        {
            My_Database mydb = new My_Database();

            try
            {
                SqlCommand command = new SqlCommand("INSERT INTO Course (Id, label, period, description, semester)" +
                                                    "VALUES (@Id, @lable, @period, @desciption, @se)", mydb.GetConnection);

                command.Parameters.Add("@Id", SqlDbType.Int).Value          = Id;
                command.Parameters.Add("@lable", SqlDbType.NVarChar).Value  = Label;
                command.Parameters.Add("@period", SqlDbType.Int).Value      = Period;
                command.Parameters.Add("@desciption", SqlDbType.Text).Value = Description;
                command.Parameters.Add("@se", SqlDbType.Int).Value          = Semester;
                mydb.Openconnection();

                if (command.ExecuteNonQuery() == 1)
                {
                    mydb.Closeconnection();
                    return(true);
                }
                else
                {
                    mydb.Closeconnection();
                    return(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                mydb.Closeconnection();
            }
        }
Exemple #18
0
        //发起聊天
        private void button_chat_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0 && listView_group.SelectedItems.Count == 0)
            {
                MessageBox.Show(this, " 请选中好友进行聊天", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (listView_group.SelectedItems.Count == 0 && listView1.SelectedItems[0].SubItems[1].Text == "offline")
            {
                MessageBox.Show(this, " 好友不在线,无法聊天", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                List <string> all_user    = new List <string>();
                List <Socket> chat_socket = new List <Socket>();
                int           chat_num    = 0; //聊天人数
                if (chat_select == 0)
                {
                    string friend_name = user_name;
                    foreach (ListViewItem item in listView1.SelectedItems)
                    {
                        friend_name = friend_name + "." + item.Text;
                        chat_num++;
                    }
                    //开始发起聊天请求
                    string send_str = "chat." + friend_name;
                    all_user.Add(send_str);
                    if (chat_num > 1)
                    {
                        string select_text = "Select count(*) from group_table";
                        string group_name  = "'chatgroup" + My_Database.SQLite_Select(select_text, connection) + "'";
                        //string group_name = "ccc";
                        string insert_text = "insert into group_table(groupname,username) values(" + group_name + "," + user_name + ")";
                        My_Database.SQLite_Insert(insert_text, connection);
                        string update_text = "update group_table set username = '******' where groupname=" + group_name;
                        My_Database.SQLite_Update(update_text, connection);
                        ListViewItem new_item = new ListViewItem(group_name);
                    }
                }
                else
                {
                    int      ind       = listView_group.Items.IndexOf(listView_group.SelectedItems[0]);
                    string[] user_list = all_chat_group[ind].user.Split('.');
                    string   send_str  = "chat.";
                    string   all       = null;
                    for (int i = 0; i < user_list.Length; i++)
                    {
                        if (user_list[i] == user_name)
                        {
                            user_list[i] = user_list[0];
                            user_list[0] = user_name;
                            all          = string.Join(".", user_list);
                            break;
                        }
                    }
                    send_str = send_str + all;

                    chat_num = all_chat_group[ind].user.Split('.').Length - 1;
                    all_user.Add(send_str);
                }
                //chat_socket = P2P_Communication.Commun_Friend(send_str, client_socket);
                //Socket chat_socket = P2P_Communication.Commun_Friend(friend_name, client_socket);
                //创建窗口
                Thread thread_chat = new Thread(() => Application.Run(new Chat_Window(all_user, chat_socket, chat_num, 0)));
                thread_chat.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制
                thread_chat.Start();
            }
        }
Exemple #19
0
 //处理未读消息
 public void add_unread_record(string recv_str, Socket socket)
 {
     if (button_msg.InvokeRequired || label2.InvokeRequired || label2.InvokeRequired ||
         label1.InvokeRequired || button_refuse.InvokeRequired || button_approve.InvokeRequired ||
         listView1.InvokeRequired)
     {
         add_Unread_msg d = new add_Unread_msg(add_unread_record);
         this.Invoke(d, new object[] { recv_str, socket });
     }
     else
     {
         if (recv_str.Split('.')[0] == "a")//好友申请
         {
             string friend_name = recv_str.Split('.')[1];
             //Socket add_friend_socket = P2P_Communication.Commun_Friend(friend_name, client_socket);
             label1.Show();
             label2.Show();
             label2.Text = friend_name + " 请求添加好友";
             button_approve.Show();
             button_refuse.Show();
             button_approve.Enabled = true;
             button_refuse.Enabled  = true;
         }
         else if (recv_str.Split('.')[0] == "aa")//好友申请
         {
             string friend_name = recv_str.Split('.')[1];
             friend_list = friend_list + "." + friend_name;
             string insert_text = "update friend_table set friend_list = '" + user_name + "." + friend_list + "' where username="******"update friend_table set friend_list = '" + friend_list + "' where username="******"online");
             listView1.Items.Add(new_item);
             MessageBox.Show(this, friend_name + " 通过了你的好友申请", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else if (recv_str.Split('.')[0] == "ar")
         {
             string friend_name = recv_str.Split('.')[1];
             MessageBox.Show(this, friend_name + " 拒绝了你的好友申请", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else
         {
             //收到一条新的未读消息
             unread_Object new_obj = new unread_Object();
             //string user = recv_str.Replace(user_name, "");
             string        user      = recv_str.Substring(5);
             string[]      user_list = user.Split('.');
             List <string> all_user  = user_list.ToList();
             if (recv_str.Split('.')[0] == "info")
             {
                 new_obj.msg_count++;
             }
             all_user.Sort();//排序
             new_obj.user_list = all_user;
             all_user.RemoveAt(all_user.Count() - 1);
             //new_obj.unread_msg.Add(recv_str);
             new_obj.unread_socket = socket;
             new_obj.main_Window   = this;
             //new_obj.socket_count++;
             unread_list.Add(new_obj);
             P2P_Communication.Unread_Chat_Receive(unread_list[unread_num], unread_num);
             //Chat_Receive(unread_list[unread_num], unread_num);
             unread_num++;
             //string show_mess = info[1] + " " + info[info.Length - 1];
             if (recv_str.Split('.')[0] == "info")
             {
                 string sender = recv_str.Split('.')[1];
                 listView_unread.Items.Add(sender);
                 button_msglist.Text = unread_num.ToString();
                 button_msg.Enabled  = true;
             }
         }
     }
     //listView1.Items.Add(new_item);
 }
Exemple #20
0
        public Chat_Window(List <string> recv_msg, List <Socket> s, int num, int con_num)
        {
            InitializeComponent();
            connection       = My_Database.Connect_Database();
            socket_to_server = Server_Connection.Connect_Server();
            user_name        = recv_msg[0].Substring(5).Split('.').ToList();
            List <string> unconnect_name = new List <string>(user_name);

            unconnect_name.RemoveAt(0);
            chat_num = num;
            datapath = ".\\data\\" + user_name[0] + "\\";
            if (!Directory.Exists(datapath))
            {
                Directory.CreateDirectory(datapath);
            }
            //读入聊天记录
            recordpath = ".\\data\\" + user_name[0] + "\\";
            for (int i = 0; i < chat_num; i++)
            {
                recordpath = recordpath + user_name[i + 1];
            }
            recordpath = recordpath + ".txt";

            StreamReader sr = new StreamReader(new FileStream(recordpath, FileMode.OpenOrCreate));
            string       exist_record;

            while ((exist_record = sr.ReadLine()) != null)
            {
                string[] record_split = exist_record.Split('.');
                if (record_split.Length >= 3)
                {
                    if (record_split[0] == user_name[0])
                    {
                        richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
                    }
                    else
                    {
                        richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    }
                    string show_msg = record_split[0] + " " + record_split[1] + "\n" + record_split[2] + "\n";
                    richTextBox1.AppendText(show_msg);
                }
                else
                {
                    richTextBox1.AppendText(exist_record);
                }
            }
            sr.Close();
            //如果为接收消息方 显示消息
            for (int i = 0; i < recv_msg.Count(); i++)
            {
                string[] info = recv_msg[i].Split('.');
                if (info[0] == "info")
                {
                    //string recv_msg = fri_name + recv_str.Split('.')[2];
                    string show_msg = info[2] + " " + info[info.Length - 1];
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    richTextBox1.AppendText(show_msg);
                    new_msg.Add(show_msg);
                    if (i == 0)
                    {
                        unconnect_name.Remove(info[2]);
                        user_name.RemoveAt(user_name.Count() - 1);
                    }
                }
            }

            if (s.Count() != 0)
            {
                socket_to_friend = s;
                //已连接用户
                for (int j = 0; j < con_num; j++)
                {
                    string send_message = "onchat." + user_name[0];
                    P2P_Communication.Connect_Send(socket_to_friend[j], send_message);
                }
                for (int i = con_num; i < chat_num; i++)
                {
                    socket_to_friend.Add(P2P_Communication.Commun_Friend(unconnect_name[i - con_num], socket_to_server));

                    string send_message = "chat.";
                    for (int j = 0; j <= chat_num; j++)
                    {
                        send_message = send_message + user_name[j] + ".";
                    }
                    P2P_Communication.Connect_Send(socket_to_friend[i], send_message);
                }
            }
            else
            {
                for (int i = con_num; i < chat_num; i++)
                {
                    socket_to_friend.Add(P2P_Communication.Commun_Friend(unconnect_name[i - con_num], socket_to_server));
                    string send_message = "chat.";
                    for (int j = 0; j <= chat_num; j++)
                    {
                        send_message = send_message + user_name[j] + ".";
                    }
                    P2P_Communication.Connect_Send(socket_to_friend[i], send_message);
                }
            }
            //开始接受消息
            for (int i = 0; i < chat_num; i++)
            {
                P2P_Communication.Chat_Receive(socket_to_friend[i], this, true);
            }
            //控件初始化
            //如果为群聊
            if (chat_num > 1)
            {
                textBox_name.Text = findGroup(user_name);
            }
            string others = null;

            for (int i = 0; i < chat_num; i++)
            {
                others = others + user_name[i + 1] + "\n";
            }
            label3.Text = user_name[0];
            label4.Text = others;
            label5.Hide();
            button_fileaccept.Hide();
            button_filerefuse.Hide();
            button_voicerefuse.Hide();
            if (chat_num > 1)
            {
                button_voicechat.Enabled = false;
            }
            waveProvider   = new BufferedWaveProvider(new WaveFormat(8000, 16, WaveIn.GetCapabilities(0).Channels));
            recievedStream = new WaveOut();
            recievedStream.Init(waveProvider);
        }
Exemple #21
0
        private void button_register_Click(object sender, EventArgs e)
        {
            string username  = textBox_username.Text;
            string password  = textBox_password.Text;
            string password2 = textBox_password2.Text;

            //密码输入不一致
            if (password != password2)
            {
                MessageBox.Show(this, "密码输入不一致", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                string info = Server_Connection.Loginto_Server(username, client_socket);
                if (info == "Incorrect login No.")
                {
                    MessageBox.Show(this, "用户名不符合规范", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    string search_text = "Select username from user_table where username="******"该用户已被注册", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    //注册成功
                    else
                    {
                        string insert_text  = "insert into user_table(username,password) values(" + username + "," + password + ")";
                        string insert_text2 = "insert into friend_table(username,friend_list) values(" + username + "," + username + ")";

                        /*
                         * using (SqlCommand cmd = connection.CreateCommand())
                         * {
                         *  cmd.CommandText = insert_text;
                         *  cmd.ExecuteNonQuery();
                         *  cmd.CommandText = insert_text2;
                         *  cmd.ExecuteNonQuery();
                         * }
                         */
                        My_Database.SQLite_Insert(insert_text, connection);
                        My_Database.SQLite_Insert(insert_text2, connection);
                        MessageBox.Show(this, "注册成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        connection.Close();
                        this.Close();
                    }
                }
            }
            textBox_username.Clear();
            textBox_password.Clear();
            textBox_password2.Clear();
        }
Exemple #22
0
 public Login()
 {
     InitializeComponent();
     connection = My_Database.Connect_Database();
 }