using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace 简单通讯 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 简单通讯 { class liaotian { public string fa { get; set; } public string shou { get; set; } public string neirong { get; set; } public DateTime shijian { get; set; } public bool zhuangtai { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace 简单通讯 { class liaotianData { SqlConnection conn = null; SqlCommand cmd = null; public liaotianData() { conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); cmd = conn.CreateCommand(); } public void Insert(liaotian lt) { cmd.CommandText = "insert into liaotian values(@fa,@shou,@neirong,@shijian,@zhuangtai)"; cmd.Parameters.Clear(); cmd.Parameters.Add("@fa", lt.fa); cmd.Parameters.Add("@shou", lt.shou); cmd.Parameters.Add("@neirong", lt.neirong); cmd.Parameters.Add("@shijian", lt.shijian); cmd.Parameters.Add("@zhuangtai", lt.zhuangtai); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } public List<liaotian> Select(string shouID) { List<liaotian> list = null; cmd.CommandText = "select *from liaotian where shou = @shou"; cmd.Parameters.Clear(); cmd.Parameters.Add("@shou",shouID); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { list = new List<liaotian>(); while (dr.Read()) { liaotian lt = new liaotian(); lt.fa = dr[0].ToString(); lt.shou = dr[1].ToString(); lt.neirong = dr[2].ToString(); lt.shijian = Convert.ToDateTime(dr[3]); lt.zhuangtai = Convert.ToBoolean(dr[4]); list.Add(lt); } } conn.Close(); return list; } public void Delete(string neirong) { cmd.CommandText = "delete from liaotian where neirong = @neirong"; cmd.Parameters.Clear(); cmd.Parameters.Add("@neirong",neirong); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } public liaotian Select2(string shouID) { liaotian list = null; cmd.CommandText = "select *from liaotian where shou = @shou"; cmd.Parameters.Clear(); cmd.Parameters.Add("@shou", shouID); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { list = new liaotian(); while (dr.Read()) { list.fa = dr[0].ToString(); list.shou = dr[1].ToString(); list.neirong = dr[2].ToString(); list.shijian = Convert.ToDateTime(dr[3]); list.zhuangtai = Convert.ToBoolean(dr[4]); } } conn.Close(); return list; } } }
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; namespace 简单通讯 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } private void button1_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(textBox1.Text)) //如果发送的内容不是空,就可以发送 { liaotian lll = new liaotian(); //新建一个聊天对象 lll.fa = label1.Text; //谁发的信息 lll.shou = "超人"; //发给谁 lll.neirong = textBox1.Text; //发送内容 lll.shijian = DateTime.Now; //发送时间 lll.zhuangtai = true; new liaotianData().Insert(lll); richTextBox1.Text += lll.shijian+" "+ lll.fa+ "对"+lll.shou+"说: "+lll.neirong; textBox1.Text = ""; } } } }
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; namespace 简单通讯 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } //List<liaotian> ltxx = new List<liaotian>(); liaotian zhong = null; private void timer1_Tick(object sender, EventArgs e) { zhong = new liaotianData().Select2(label1.Text); //查询出需要超人收的信息,查一条 if (zhong != null)//如果查出来了,那么就需要显示 { //将查出来的信息显示出来 richTextBox1.Text += zhong.shijian + " " + zhong.fa + "对" + zhong.shou + "说: " + zhong.neirong + " "; //显示完毕后,在数据库删掉这条信息,防止重复查询显示 new liaotianData().Delete(zhong.neirong); } } private void Form2_Load(object sender, EventArgs e) { } } }
一个重要属性Interval,这个值设定每个多长时间执行一次动作,单位是毫秒,还有一个重要事件是Timer,双击Timer控件即可自动产生这个事件的框架,在这个框架里面写入每隔Interval毫秒就要执行的代码动作。
例如:
1.放一个Timer控件在窗体上
2.设置Timer控件的Interval属性为60000(表示60000毫秒,即一分钟)
3.双击Timer控件,在光标处写代码:msgbox("hello")
这样,每隔一分钟就弹出一个消息框,显示hello