Winforms,invoke()和有问题的按钮 [英] Winforms, invoke() and a problematic button

查看:78
本文介绍了Winforms,invoke()和有问题的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立聊天,基本上我使用了invoke函数是什么线程.我能够读取服务器发送给我的内容,但是我只能写入一次.我正在尝试完成此操作,但不确定每次服务器时如何写入服务器:
(考虑到我以前在控制台应用程序形式中编写了此文件,并且服务器工作正常..ie问题出在服务器上).

i am trying to build a chat, basically i used the invoke function what a thread. i am able to read what the server sends me, but i am able to write only once. i am trying to finish this but not sure how to write to server each time the server:
(take into account that i wrote this before in console application form and the server works fine..ie. the problem isnt with the server).

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        StreamReader sr;
        StreamWriter sw;
        TcpClient connection;
 
        public Form1()
        {
            InitializeComponent();
            Button btn1 = new Button();
            btn1.Click += button1_Click;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            connection = new TcpClient("127.0.0.1", 5000);
            sr = new StreamReader(connection.GetStream());
            sw = new StreamWriter(connection.GetStream());
        
        }
      
        private void button2_Click(object sender, EventArgs e)
        {
    
            Thread t2 = new Thread(Reader);
            t2.Start(connection);
        }
 
        string msg;
 
        public void Reader(object o)
        {
            TcpClient con = o as TcpClient;
            if (con == null)
            {
                return;
            }
            while (true)
            {
                msg = sr.ReadLine();
                Invoke(new Action(Output));
            }
        }
     
        public void Output()
        {
            ChatScreen.Text = msg;
        }
 
        string textinput;
 
        private void button1_Click(object sender, EventArgs e)
        {
            textinput = InputLine.Text;
            sw.WriteLine(textinput);
            sw.Flush();
        }
    }
}



我想做的是连接按钮,这样它就可以多次单击..例如btn.Click() ..或在WriteLine上运行带有invoke的线程,但是我的直觉说,使按钮多次单击会使程序正常工作



what i thought to do is to connect the button so it will be able to do multiple clicks ..e.g btn.Click()..or run a thread with invoke on the WriteLine but my intuition says that making the button click several times would make the program work

推荐答案

也许如果将按钮添加到表单中,可能会有所帮助...
在构造函数中添加:
Perhaps if you added the button to the form, it would help things...
In your constructor add:
Controls.Add(btn1);

点击处理程序之后

如果用户看不到该按钮,则无法单击它!

after the click handler

If the user can''t see the button, he can''t click it!


这篇关于Winforms,invoke()和有问题的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆