如何使用c#在Windows窗体中显示控制台应用程序数据 [英] how to display the console application data in the windows form using c#

查看:484
本文介绍了如何使用c#在Windows窗体中显示控制台应用程序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有控制台应用程序,它使用套接字从客户端获取数据,现在我希望在文本框中显示数据或在窗体应用程序中显示标签。

所以这个代码

i喜欢显示新的窗体应用程序,它将在表单控件中显示数据,如文本框或标签..等等。


I have console application which gets the data from the client using sockets, now i want display that data in the text box or label in the windows form application.
So this the code
i like to display new windows form applications that will display the data in the form control like text box or label..etc.,

 int Port = 8088;
 TcpListener server = new TcpListener(IPAddress.Any, Port);
 server.Start(); 
 byte[] bytes = new byte[1024];
 string data;
 Console.Write("Waiting for Client connection ");
  TcpClient client = server.AcceptTcpClient();
  Console.WriteLine("\n Client Connected");
   NetworkStream stream = client.GetStream();
   int i;
i = stream.Read(bytes, 0, bytes.Length);
data = Encoding.UTF8.GetString(bytes, 0, i);
 data=data.Substring(2, data.Length-2);
 Console.WriteLine(string.Format("Data Received :{0}", data));
  client.Close();

推荐答案

1)使用1个Button,ListBox和a创建你的WinForm应用程序Backgroundworker

2)添加Button_click-Event,BackgroundWorker1DoWork-Event,BackgroundWorker1RunWorkerCompleted-Event和BackgroundWorker1ProgressChanged-Event



3)添加此代码:

1) create your your WinForm - application with 1 Button, a ListBox and a Backgroundworker
2) Add the Button_click-Event, the BackgroundWorker1DoWork-Event, the BackgroundWorker1RunWorkerCompleted-Event and the BackgroundWorker1ProgressChanged-Event

3) add this code:
void Button1Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}
void BackgroundWorker1DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    int Port = 8088;
    TcpListener server = new TcpListener(IPAddress.Any, Port);
    server.Start();
    byte[] bytes = new byte[1024];
    string data;
    //Console.Write("Waiting for Client connection ");
    backgroundWorker1.ReportProgress(25);

    TcpClient client = server.AcceptTcpClient();
    //Console.WriteLine("\n Client Connected");
    backgroundWorker1.ReportProgress(50);
    NetworkStream stream = client.GetStream();
    int i;
    i = stream.Read(bytes, 0, bytes.Length);
    data = Encoding.UTF8.GetString(bytes, 0, i);
    data = data.Substring(2, data.Length - 2);

    client.Close();server.Close();
    e.Result=data;
}
void BackgroundWorker1RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
    listBox1.Items.Add(String.Format("Data Received: {0}", (string)e.Result));
}
void BackgroundWorker1ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    switch (e.ProgressPercentage) {
        case 25:
            listBox1.Items.Add("Waiting for Client connection ");
            break;
        case 50:
            listBox1.Items.Add("Client Connected");
            break;
    }
}


这篇关于如何使用c#在Windows窗体中显示控制台应用程序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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