使用ReceivedEventHandler从另一个类更新类窗体中的文本框

时间:2014-01-28 19:54:54

标签: c# textbox

我是c#的新手,我知道这个话题以前已经解决了,我现在尝试了一些解决方案大约3天但是他们没有工作,你能帮帮我吗?

我的问题是:我有一个班级Form1和一个班级connectionconnection类处理与串行端口的连接和通信。每次类connection收到一些信息时,都会触发OnSerialDataReceived - Eventhandler,并且信息字符串会保存在received_data中。

现在我要更新Form1中名为tb_received的文本框的文本,但我不知道如何从我的连接类中获取与tb_received的连接。< / p>

请帮帮我,谢谢。

Form1中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


    namespace mp6_Control_Rev1._0
    {
        public partial  class Form1 : Form 
        {
            Connection con;




            public  Form1()
            {
                InitializeComponent();

                con= new Connection();

                Disable();           
                bt_start_stop.Text = "Start";
                rb_pump.Checked = true;

                foreach (string port in con.Port())                                         
                {
                    dd_ports.Items.Add(port);
                }

            }

    ...............
    }


    Connection:#

    using System;
    using System.IO.Ports;
    using System.Threading;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Windows.Forms;

    namespace mp6_Control_Rev1._0
    {
        public class Connection : Form
        {

            public string[] ports;
            public string received_data;
            public int data_received_flag = 0;                                  //Setzen durch Eventhandler 
            SerialPort serialPort1;








            public String[] Port()
            {
                ports = SerialPort.GetPortNames();                                
                return ports;                                                    


            }

            public bool check_port(string entry)
            {
                if (entry.Equals("avaible Ports"))                              
                {
                    MessageBox.Show("Please choose an avaible Port");
                    return false;
                }
                else
                {
                    return true;
                }
            }

            public void new_DataReceived()
            {
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(OnSerialDataReceived);
            }

            public void OnSerialDataReceived(object sender, SerialDataReceivedEventArgs args)
            {
                MessageBox.Show("Serialdatareceived");
                received_data = serialPort1.ReadExisting();

                data_received_flag = 1;
            }

            public void start_connection(string port)
            {
                try
                {

                    serialPort1 = new SerialPort();


                    serialPort1.WriteTimeout = 500;
                    serialPort1.PortName = port;
                    serialPort1.BaudRate = 9600;
                    serialPort1.Parity = Parity.None;
                    serialPort1.DataBits = 8;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.Handshake = Handshake.None;
                    serialPort1.DtrEnable = true;

                    serialPort1.Open();
                }
                catch
                {
                    MessageBox.Show("Connection failed, please try again.");
                }
            }


            public void close_port()
            {
                serialPort1.Close();
            }

            public void send_string(string to_send)

            {    
                if (serialPort1.IsOpen)
                {
                    serialPort1.Write(to_send);       
                }
                else
                {
                    MessageBox.Show("Serial Port is closed, please connect !");
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

在Connection类中添加一个事件,让主表单监听该事件。

编辑:忘记代码。

public class Connection
{
    public event Action<string> MessageReceivedEvent;

    public void fire()
    {
        if (this.MessageReceivedEvent != null) MessageReceivedEvent("message");
    }

}
public partial class Form1 : Form
{
    private void Form1_Load(object sender, System.EventArgs e)
    {
        Connection con = new Connection();
        con.MessageReceivedEvent += new System.Action<string>(HandleMessage);

    }


    private void HandleMessage(string message)
    {
        //Update your textbox here
    }



    public Form1()
    {
        InitializeComponent();

    }

}

答案 1 :(得分:0)

从您的代码中,我没有在form1中看到名为tb_received的声明文本框。无论如何,你应该能够公开一个公共变量(可能是tb_received),然后在触发事件处理程序时将它的值设置为received_data。 在这里看到类似的情况(已回答):Access variable from other namespaces