尝试将数据值从另一个表单添加到列表框时如何修复

How to fix when trying to add data values to a listbox from another form

我在尝试从我的主表单向列表框添加数据时遇到问题,我正在尝试从我的项目中的新 class 向该列表添加数据,我需要那个新的 class 为了能够将数据无误地添加到我的列表框中,我正在尝试使用调用,但出现以下错误 (System.InvalidOperationException: 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.') 我在其他问题中看到过该错误在堆栈溢出但类似于我的问题,我将在此处添加我的代码的两个 classes,一个是主要的 class,另一个是我创建的第二个 class将数据添加到列表框。数据来自 telnet tcp/ip 和连接工作正常的端口 23,问题是将该数据添加到我的列表框。

Main class 从我的另一个 class

调用函数

namespace BarcodeReceivingApp {

//TelnetConnection stopConnection = new TelnetConnection();
public partial class BarcodeReceivingForm : Form
{
    //GLOBAL VARIABLES
    const string Hostname = "myip";
    private const int Port = 23;


    public BarcodeReceivingForm()
    {
        InitializeComponent();
    }

    private void btn_ConnectT_Click(object sender, EventArgs e)
    {

        var readData = new TelnetConnection(Hostname, Port);
        readData.ServerSocket(Hostname, Port);

    }

    private void btn_StopConnection_Click(object sender, EventArgs e)
    {
        //var connection = new TelnetConnection(Hostname, Port);
       // connection.CloseConnection();
    }
}

}

class 这将更改主列表框的数据 class。

 namespace BarcodeReceivingApp

{ public class TelnetConnection { public BarcodeReceivingForm BcForm = new BarcodeReceivingForm();

    private Thread _readWriteThread;
    private TcpClient _client;
    private NetworkStream _networkStream;
    private string _hostname;
    private int _port;

    public TelnetConnection(string hostname, int port)
    {
        this._hostname = hostname;
        this._port = port;
    }

    public void ServerSocket(string ip, int port)
    {

        try
        {
            _client = new TcpClient(ip, port);         
        }
        catch (SocketException)
        {
            MessageBox.Show(@"Failed to connect to server");
            return;
        }

        //Assign networkstream
        _networkStream = _client.GetStream();

        //start socket read/write thread
        _readWriteThread = new Thread(ReadWrite);
        _readWriteThread.Start();
    }

    public void ReadWrite()
    {

        //Set up connection loop
        while (true)
        {
            var command = "test";
            if (command == "STOP1")
                break;

            //write(command);
             var received = Read();


            BcForm.lst_BarcodeScan.Invoke(new Action (() => BcForm.lst_BarcodeScan.Items.Add(received)));


        }

    }

    public string Read()
    {
        byte[] data = new byte[1024];
        var received = "";

        var size = _networkStream.Read(data, 0, data.Length);
        received = Encoding.ASCII.GetString(data, 0, size);

        return received;
    }

    public void CloseConnection()
    {
        _networkStream.Close();
        _client.Close();
    }
}
}

我所说的最终结果是我的 ReadWrite 方法,当 运行 循环将从我的主窗体 class

将数据添加到我的列表框中

这里是我得到错误的图像 Image of Error

像这样写你的第二个class

using System;
using System.Threading;
using System.Windows.Forms; 
namespace BarcodeReceivingApp { 
    public class TelnetConnection { 

        private Thread _readWriteThread;
        private TcpClient _client;
        private NetworkStream _networkStream;
        private string _hostname;
        private int _port;
        private Form foo;

        public TelnetConnection(string hostname, int port)
        {
            this._hostname = hostname;
            this._port = port;
        }

        public void ServerSocket(string ip, int port,Form f)
        {
            this.foo = f;
            try
            {
                _client = new TcpClient(ip, port);         
            }
            catch (SocketException)
            {
                MessageBox.Show(@"Failed to connect to server");
                return;
            }

            _networkStream = _client.GetStream();

            _readWriteThread = new Thread(ReadWrite());
            _readWriteThread.Start();
        }  

        public void ReadWrite()
        {
            while (true)
            {
                var command = "test";
                if (command == "STOP1")
                    break;

                //write(command);
                 var received = Read();
                 if (foo.lst_BarcodeScan.InvokeRequired)
                 {
                         foo.lst_BarcodeScan.Invoke(new MethodInvoker(delegate {foo.lst_BarcodeScan.Items.Add(received);}));
                 }
            }
        }

        public string Read()
        {
            byte[] data = new byte[1024];
            var received = "";

            var size = _networkStream.Read(data, 0, data.Length);
            received = Encoding.ASCII.GetString(data, 0, size);

            return received;
        }

        public void CloseConnection()
        {
            _networkStream.Close();
            _client.Close();
        }
    }
}

然后从你的主 class:

中像这样使用它
private void btn_ConnectT_Click(object sender, EventArgs e)
{

    var readData = new TelnetConnection(Hostname, Port);
    readData.ServerSocket(Hostname, Port, this);

}