将消息发送到现有表单类

时间:2014-05-07 07:47:32

标签: c#

希望有人可以帮我解决这个问题。我一直尝试不同的东西,我尝试了一些我在这里找到的解决方案和其他网站无济于事,但由于我是新手,我想我可能错过了一些东西。到此为止。

我正在编写一个应用程序,我想添加用户相互发送消息并让它们弹出屏幕的能力。这很好,消息被传递并且表单弹出,但如果用户发送另一条消息,它将创建该类的新实例。它似乎没有找到开放形式。我尝试过一些东西来搜索开放表格:

    //Receive message is started by the OpenListeningSocket function 
    //ProcessingThread = new Thread(() => ReceiveMessage());

    private void ReceiveMessage()   
    {
        Socket newSocket = tempServerSocket;
        NetworkStream netStream = new NetworkStream(newSocket);
        BinaryReader br = new BinaryReader(netStream);
        BinaryWriter bw = new BinaryWriter(netStream);
        String message;
        String sender; 
        try
        {
            do
            {
                message = br.ReadString();
                sender = br.ReadString();

            } while (br.PeekChar() > 0);
            /*
            //FormCollection fc = Application.OpenForms;
            for(int i = 0; i <= Application.OpenForms.Count; i++)
            //foreach (Form frm in Application.OpenForms)
            {
                if(Application.OpenForms[i].Text == sender)
                //if (frm is Message)
                {
                    Message liveMessage = (Message)Application.OpenForms[i];
                    if (liveMessage.Text == sender)
                    {
                        MessageBox.Show("liveMessage Message detected");
                        liveMessage.lbMain.Items.Add(sender + ": " + message);
                        liveMessage.cbUsers.Text = sender;
                        liveMessage.cbUsers.Enabled = false;
                        liveMessage.TopMost = true;
                        liveMessage.TopMost = false;
                        liveMessage.ShowDialog();
                        //liveMessage.Visible = true;
                    }
                }
                else if(Application.OpenForms[i] is Message)
                {
                    newMsg = new Message();
                    newMsg.Text = sender;
                    newMsg.userName = userName;
                    newMsg.cbUsers.Text = sender;
                    newMsg.cbUsers.Enabled = false;
                    newMsg.lbMain.Items.Add(sender + ": " + message);
                    //msgList.Add(newMsg);
                    newMsg.ShowDialog();
                }

            }

            */

            if (msgList.Count < 1)
            {
                Message newMsg = new Message();
                newMsg.Text = sender;
                newMsg.userName = userName;
                newMsg.cbUsers.Text = sender;
                newMsg.cbUsers.Enabled = false;
                newMsg.lbMain.Items.Add(sender + ": " + message);
                msgList.Add(newMsg);

                newMsg.Visible = true;

            }
            else
            {
                foreach (Message msg in msgList)
                {
                if (msg.Text == sender)
                    {
                        //Message msg = (Message)frm;
                        msg.lbMain.Items.Add(sender + ": " + message);
                        msg.cbUsers.Text = sender;
                        msg.cbUsers.Enabled = false;
                        newMsg.Visible = true;
                        //msg.Visible = true;
                    }
                }
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
     }

我已经尝试查看Form Collection中的所有打开表单,如果表单是Message类,则将其强制转换为Message。我无法让这个工作。我尝试将所有新的Message实例添加到Message类型的通用列表中,然后迭代它们但这不起作用,可能由ProcessingThread处理?

我已尝试查看一些即时消息应用程序示例,但我没有看到他们如何将新消息发送到现有表单。

如果有人有任何有用的提示或可以指出我正确的方向,我将不胜感激。谢谢,卡梅隆。

0 个答案:

没有答案