C#将文本添加到ListBox或TextBox

时间:2017-10-07 10:31:38

标签: c# textbox listbox

  1. 您好,我在编写简单字符串到TextBox和ListBox时遇到了问题。我不知道出了什么问题。 点击button3后,在类中运行侦听运行方法,打开通信和接收数据包。在此方法(Listen.StartListen)中引用PrintReceivedPackets。在任务部分是错误的吗? 使用Thread而不是Task更好吗?
  2. 2

    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private Listen lis, start;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button2_Click_1(object sender, EventArgs e)
            {
                string deviceNum = comboBox2.Text;
                char dN = deviceNum[0];
                start = new Listen();
                Task.Factory.StartNew(() => start.StartListen(dN));
    
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                lis = new Listen();
                var devices = lis.GetDevices();
                comboBox2.DataSource = devices;
            }
    
            public void PrintReceivedPackets(string packetInfo)
            {
                //  Do not work 
                Console.WriteLine(">>>" + packetInfo);
                listBox1.Items.Add(packetInfo);
                textBox1.AppendText(packetInfo);
            }
    
        }
    

    课堂听法中的方法

    public void StartListen(char deviceNum)
            { 
                int deviceNumber = (int)Char.GetNumericValue(deviceNum);
    
                // Take the selected adapter
                PacketDevice selectedDevice = allDevices[deviceNumber - 1];
    
                // Open the device
                using (PacketCommunicator communicator =
                    selectedDevice.Open(65536,                                  // portion of the packet to capture
                                                                                // 65536 guarantees that the whole packet will be captured on all the link layers
                                        PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                        1000))                                  // read timeout
                {
                    Console.WriteLine("Listening on " + selectedDevice.Description + "...");
    
                    // start the capture
                    communicator.ReceivePackets(0, PacketHandler);
                }
            }
    
            // Callback function invoked by Pcap.Net for every incoming packet
            public void PacketHandler(Packet packet)
            {
                Console.WriteLine(packet.Timestamp.ToString("dd-MM-yyyy ") + " length:" + packet.Length + " " + packet.DataLink);
                string packetInfo = packet.Timestamp.ToString("dd-MM-yyyy ") + " length:" + packet.Length + " " + packet.DataLink;
    
                Form1 f = new Form1();
                f.PrintReceivedPackets(packetInfo);
            }
    

    编辑:添加了有关如何使主窗体对监听器可见的评论

    最简单的方法是在Listen类中添加对主窗体的引用。甲' LA:

    public class Listen
    {
        Form1 mainForm;
    
        public Listen(Form1 mainForm)
        {
            this.mainForm = mainForm;
    
            ...
        }
    }
    

    然后,在button2_Click_1中,您可以像这样创建起始对象:

    start = new Listen(this); 
    

    然后,在PacketHandler中,你可以这样做:

    mainForm.Invoke((Action)(() => mainForm.PrintReceivedPackets(packetInfo)));
    

    从PacketHandler中删除Form1 f = new Form1(),因为您实际上并不想为每个数据包添加新表单。

1 个答案:

答案 0 :(得分:0)

在PacketHandler中,您将创建一个新的Form1实例,然后调用 形式的PrintReceivedPackets方法。从代码开始,这种形式从未实际打开过 - 因为你需要在某个时刻调用f.Show()或f.ShowSialog()。

如果您打算显示实际主表单的数据包通知,那么您需要做两件事:

  1. 通过将主表单对象分配给全局变量,或者将其作为参数传递给StartListen,使主表单对StartListen对象可见。

  2. 在PacketHandler中,调用主窗体的PrintReceivedPackets方法。在不同的线程中执行PacketHandler并不是很明显。如果你得到一个“跨线程”异常,那么你需要使用Invoke将更新传递给主线程,如下所示:

    mainForm.Invoke((Action)(()=> mainForm.PrintReceivedPackets(packetInfo)));