c#フォーム偽ボタンチャット————ログインからチャット(1)


ログインcsクラスファイル
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myqq
{
    class LonginU
    {
        public int qq { get; set; }
        public string LoginPwd { get; set; }
        public string NickName { get; set; }
        public int faceid { get; set; }
        public string sex { get; set; }
        public int age { get; set; } 
        public string name { get; set; }
    }
}

ログイン実装
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myqq
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string sql = String.Format("select * from Users where Id={0} and loginpwd='{1}'",textBox1.Text,textBox2.Text);
            DBHelper.conn.Open();
            SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
            SqlDataReader rd= cmd.ExecuteReader();
            
            Boolean flag = false;
            while (rd.Read())
            {                
                DBHelper.loginU.qq = Convert.ToInt32(rd["Id"]);
                DBHelper.loginU.NickName = rd["NickName"].ToString();
                DBHelper.loginU.faceid = Convert.ToInt32(rd["FaceId"]);
                flag = true;

            }
            DBHelper.conn.Close();
            if (flag)
            {
                
                this.Visible = false;
                Main m = new Main();
                m.Show();
            }
            else
            {
                MessageBox.Show("        !");
            }
        }
    }
}

友達リストmain
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Aptech.UI;

namespace myqq
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            SelectPhoto selectPhoto = new SelectPhoto();
            selectPhoto.Show();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            DBHelper.conn.Open();
            string sql = string.Format("update Users set Online=1 where Users.Id={0}", DBHelper.loginU.qq);
            SqlCommand com = new SqlCommand(sql, DBHelper.conn);
            com.ExecuteNonQuery();
            DBHelper.conn.Close();
            this.pictureBox1.Image = imageList1.Images[DBHelper.loginU.faceid];
            this.label1.Text = DBHelper.loginU.NickName;
            sideBar1.AddGroup("    ");
            sideBar1.AddGroup("   ");
            ShowMyFriends();
            timer1.Start();
            timer2.Start();
        }

        private void ShowMyFriends()
        {
            String Sql = String.Format("select * from Users,Friends where HostId={0} and Users.Id=FriendId and online=1",DBHelper.loginU.qq);
            DBHelper.conn.Open();
            SqlCommand cmd = new SqlCommand(Sql, DBHelper.conn);
            SqlDataReader rd = cmd.ExecuteReader();
            while(rd.Read())
            {
                int i = 0;
                string friendname = rd["NickName"].ToString();
                int faceid = Convert.ToInt32(rd["FaceId"]);
                SbItem sb = new SbItem(friendname, faceid);
                sb.Tag = Convert.ToInt32(rd["Id"]);
                sideBar1.Groups[i].Items.Add(sb);
                i++;
            }
            DBHelper.conn.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //      
            sideBar1.Groups[0].Items.Clear();
            ShowMyFriends();
        }
        public int massageId;
        public int sendfaceID;
        public string sendnickname;
        private void timer2_Tick(object sender, EventArgs e)
        {
            //          
            DBHelper.conn.Close();
            string sql = string.Format("select * from Messages,Users  where ToUserId={0} and MessageTypeId=1 and Messages.FromUserId=Users.Id",DBHelper.loginU.qq);
            DBHelper.conn.Open();
            SqlCommand cm = new SqlCommand(sql,DBHelper.conn);
            SqlDataReader rd= cm.ExecuteReader();
            while (rd.Read())
            {
                massageId = Convert.ToInt32(rd["FromUserId"]);
                sendfaceID = Convert.ToInt32(rd["faceId"]);
                sendnickname = Convert.ToString(rd["NickName"]);
                if (Convert.ToInt32(rd["MessageState"]) == 0)
                {
                    SoundPlayer sp = new SoundPlayer("F:\\     \\myQQ\\qq  \\qq  \\msg.wav");
                    sp.Play();
                    shantu();
                }
                   
            }
            DBHelper.conn.Close();            
        }
       //        
        private void shantu()
        {
            for(int i = 0; i < sideBar1.Groups[0].Items.Count; i++)
            {
                if(Convert.ToInt32(sideBar1.Groups[0].Items[i].Tag)== massageId)
                {
                    if (sideBar1.Groups[0].Items[i].ImageIndex < 100)
                    {
                        sideBar1.Groups[0].Items[i].ImageIndex = 100;
                    }
                    else
                    {
                        sideBar1.Groups[0].Items[i].ImageIndex = sendfaceID;
                    }
                    sideBar1.Invalidate();
                }
            }
        }
        

        
        private void sideBar1_ItemDoubleClick(SbItemEventArgs e)
        {
            chat c = new chat();
            c.friendqq = massageId;
            c.nickname = sendnickname;
            c.faceId = sendfaceID;
            c.Show();
        }
        //      
        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            DBHelper.conn.Open();
            string sql = string.Format("update Users set Online=0 where Users.Id={0}", DBHelper.loginU.qq);
            SqlCommand com = new SqlCommand(sql, DBHelper.conn);
            com.ExecuteNonQuery();
            DBHelper.conn.Close();
        }
    }
}

チャットチャット
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myqq
{
    public partial class chat : Form
    {
        public chat()
        {
            InitializeComponent();
        }
        public int friendqq;
        public int faceId;
        public string nickname;

        private void chat_Load(object sender, EventArgs e)
        {
            label1.Text = nickname;
            pictureBox1.Image = imageList1.Images[faceId];
            getmessage();
            timer1.Start();
        }

        private void getmessage()
        {
            DBHelper.conn.Open();
            string sql = string.Format("select * from Messages where FromUserId = {0} and ToUserId = {1} AND MessageTypeId = {2} AND MessageState = {3} ",friendqq,DBHelper.loginU.qq,1,0);
            SqlCommand cm = new SqlCommand(sql, DBHelper.conn);
            SqlDataReader rd=cm.ExecuteReader();
            while(rd.Read())
            {
                richTextBox1.Text += string.Format("{0} :{1}\r
", nickname, rd["message"].ToString()); } DBHelper.conn.Close(); DBHelper.conn.Open(); string Updatasql = string.Format("update Messages set MessageState=1 where FromUserId={0} and ToUserId={1} and MessageTypeId={2}",friendqq,DBHelper.loginU.qq,1); cm = new SqlCommand(Updatasql, DBHelper.conn); cm.ExecuteNonQuery(); DBHelper.conn.Close(); } private void button3_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } DateTime d = DateTime.Now; DBHelper.conn.Open(); string sql = string.Format("insert into Messages values({0},{1},'{2}',{3},{4},'{5}')",DBHelper.loginU.qq,friendqq,textBox1.Text,1,0,d.ToString("yyyy-MM-dd hh:mm:ss")); SqlCommand com = new SqlCommand(sql,DBHelper.conn); com.ExecuteNonQuery(); DBHelper.conn.Close(); richTextBox1.Text += string.Format("{0} :{1}\r
", DBHelper.loginU.NickName, textBox1.Text); textBox1.Text = ""; } private void timer1_Tick(object sender, EventArgs e) { getmessage(); } } }

以上は登録チャット機能のみを実現しており、その後、アイコンの修正、個人情報の検索、友人の検索に加えて友人として機能する.
 
 
 
こんなにきれいにしてくれてありがとう
こんなに勉強好きなのに
そして私のブログが好きです
 
 
何かわからないことがあったらコメントして質問してもいいですよ!!