当前位置:编程学习 > C#/ASP.NET >>

新手问个问题,刚来没分,希望得一帮助


  delegate void ListBoxCallback(string str);
        public void add(string str)
        {
            if (rtbMsg.InvokeRequired == true)
            {
                ListBoxCallback d = new ListBoxCallback(add);
                rtbMsg.Invoke(d, str);

            }
            else
            {
                rtbMsg.AppendText(str);

              

            }
        }
private void ServerResponse()
{
//定义一个byte数组,用于接收从服务器端发送来的数据,
//每次所能接收的数据包的最大长度为1024个字节
byte[] buff=new byte[1024];
string msg;
int len;
try
{
if(!Stream.CanRead)
{
return;
}

stopFlag = false;
while(!stopFlag)
{
//从流中得到数据,并存入到buff字符数组中
len=Stream.Read(buff,0,buff.Length);

if (len < 1)
{
Thread.Sleep(200);
continue;
}

//将字符数组转化为字符串
msg=System.Text.Encoding.Default.GetString(buff,0,len);
msg.Trim();

string[] tokens=msg.Split(new Char[]{'|'});
//tokens[0]中保存了命令标志符(LIST或JOIN或QUIT)

if (tokens[0].ToUpper()== "OK")
{
//处理响应
add("命令执行成功");
}
else if (tokens[0].ToUpper()== "ERR")
{
//命令执行错误
add("命令执行错误:" + tokens[1]);
}
else if(tokens[0]== "LIST")
{
//此时从服务器返回的消息格式:
//命令标志符(LIST)|用户名1|用户名|2...(所有在线用户名)|
add("获得用户列表");
//更新在线用户列表
lstUsers.Items.Clear();
for(int i=1;i<tokens.Length-1;i++)
{
lstUsers.Items.Add(tokens[i].Trim());
}
}
else if(tokens[0]== "JOIN")
{
//此时从服务器返回的消息格式:
//命令标志符(JOIN)|刚刚登入的用户名|
add(tokens[1]+" "+"已经进入了聊天室");
SetListBox(tokens[1]);
if (this.tbUserName.Text ==tokens[1]) 
{
this.state = CONNECTED;
}
}
else if(tokens[0]== "QUIT")
{
if (this.lstUsers.Items.IndexOf(tokens[1])>-1)
{
this.lstUsers.Items.Remove(tokens[1]);
}
add("用户:" + tokens[1] + " 已经离开");
}
else
{
//如果从服务器返回的其他消息格式,
//则在ListBox控件中直接显示
add(msg);
                        myevent(msg);
}
}
//关闭连接
tcpClient.Close();
}
catch
{
add("网络发生错误");
}
}

聊天程序,有多个窗体,怎么把聊天信息准确发到需要信息的窗体的,
有人说用事件,在哪里用啊 --------------------编程问答-------------------- 用哈希,Hashtable 
Hashtable hashFriend= new Hashtable();
为每个好友都new一个聊天窗体,对象名为aa,key是主键,能区分每个好友就行,比如qq号
hashFriend.Add(key, aa);
这样做最大的优点就是好找。
--------------------编程问答-------------------- 窗口订阅发消息的消息管理器(姑且这么叫),消息管理器再分发消息的时候按照订阅者来发送到指定的窗口即可。 --------------------编程问答-------------------- 有没有小例子啊,
--------------------编程问答-------------------- 路过 --------------------编程问答--------------------

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      public delegate void EventNameEventHandler(string str);
        public static event EventNameEventHandler EventName;

        private void button1_Click(object sender, EventArgs e)
        {
            if (EventName!= null)
            {
                EventName("你好");
            }
        }
        //Form2.Show
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
        }
        //Form3.Show
        private void button3_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3();
            frm3.Show();
        }


    }
}



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 WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            Form1.EventName += new Form1.EventNameEventHandler(Form1_EventName);//订阅事件
        }

        void Form1_EventName(string str)
        { if(在这判断是否是要的信息)
            label1.Text = str;
        }
    }
}



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 WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Form1.EventName += new Form1.EventNameEventHandler(Form1_EventName);//订阅事件
        }

        void Form1_EventName(string str)
        {  if(同上)
            label1.Text = str;
        }
    }
}



  len=Stream.Read(buff,0,buff.Length);

                    if (len < 1)
                    {
                        Thread.Sleep(200);
                        continue;
                    }
                    
                    //将字符数组转化为字符串
                    msg=System.Text.Encoding.Default.GetString(buff,0,len);
                    msg.Trim();
         if (EventName!= null)
            {
                EventName("你好");
            }

cuike519大侠是不是像这样啊.没打开的窗口,就是没订阅这个事件,有打开就是订阅了。
--------------------编程问答-------------------- 错还是对???
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,