新手请教,怎么实时保存聊天记录并调用文本框显示?
我想要像腾讯QQ在聊天的时候实时把聊天记录(信息不重复)保存起来,在需要查看的时候,单击聊天记录按钮就可以在右边文本框中显示保存的聊天记录(希望能查看到每次聊天的记录)!
请问大家,要怎么实现?
程序代码:
private void button_Send_Click(object sender, EventArgs e) { IPAddress ip = IPAddress.Parse(Publec_Class.ServerIP);//服务器端的IP地址 string port = Publec_Class.ServerPort;//端口号 string revid = ((this.Tag as TreeNode).Tag as ClassUserInfo).UserID;//接收ID号 string sid = Publec_Class.UserID;//发送ID string msgid = Guid.NewGuid().ToString();//设置全局惟一标识 byte[] data = Encoding.Unicode.GetBytes(rich_Input.Rtf);//将当前要发送的信息转换成二进制流 ClassMsg msg = new ClassMsg(); msg.sendKind = SendKind.SendMsg;//发送的消息 msg.msgCommand = MsgCommand.SendToOne;//发送的是单用户信息 msg.SID = sid;//发送ID msg.RID = revid;//接收ID msg.Data = data;//发送的信息 msg.msgID = msgid; if (data.Length <= 1024)//如果发送信息的长度小于等于1024 { msg.sendState = SendState.Single; //将信息直接发送给远程客户端 udpsocket.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(msg).ToArray()); } else { ClassMsg start = new ClassMsg(); start.sendKind = SendKind.SendMsg; start.sendState = SendState.Start;//文件发送开始命令 start.msgCommand = MsgCommand.SendToOne;//发送单用户命令 start.SID = sid; start.RID = revid; start.Data = Encoding.Unicode.GetBytes(""); start.msgID = msgid; udpsocket.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(start).ToArray()); MemoryStream stream = new MemoryStream(data);//将二进制流存储到内存流中 int sendlen = 1024; //设置文件每块发送的长度 long sunlen = (stream.Length);//整个文件的大小 int offset = 0;//设置文件发送的起始位置 while (sunlen > 0) //分流发送 { sendlen = 1024; if (sunlen <= sendlen) sendlen = Convert.ToInt32(sunlen); byte[] msgdata = new byte[sendlen]; stream.Read(msgdata, offset, sendlen);//读取要发送的字节块 msg.sendState = SendState.Sending;//发送状态为文件发送中 msg.Data = msgdata; udpsocket.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(msg).ToArray());//发送当前块的信息 sunlen = sunlen - sendlen;//记录下一块的起始位置 } ClassMsg end = new ClassMsg(); end.sendKind = SendKind.SendMsg; end.sendState = SendState.End;//文件发送结束命令 end.msgCommand = MsgCommand.SendToOne; end.SID = sid; end.RID = revid; end.Data = Encoding.Unicode.GetBytes(""); end.msgID = msgid; udpsocket.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(end).ToArray());//发送信息,通知文件发送结束 string dir = System.Environment.CurrentDirectory; dir = dir.Substring(0, dir.Length - 9); pictureBox1.Load(dir + @"Image\QQ12.jpg"); } Rich_Out.SelectionStart = 0;//将文本的起始点设为0 Rich_Out.AppendText(Publec_Class.UserName);//将当前用户名添加到文本框中 Rich_Out.AppendText(" "+ DateTime.Now.ToString());//将当前发送的时间添加到文本框中 Rich_Out.AppendText("\r\n");//换行回车 Rich_Out.SelectedRtf = rich_Input.Rtf;//将发送信息添加到接收文本框中 rich_Input.Clear();//清空发送文本框 }这是我发送消息模块。