[英]Update a Textbox in Class Form from another Class with ReceivedEventHandler
I´m new to c# and I know that this topic has been solved before and I tried some of the solutions for about 3 days now but non of them worked, can you please help me? 我是c#的新手,我知道这个主题以前已经解决过了,我现在尝试了大约3天的某些解决方案,但没有一个可行,请您能帮我吗?
My problem is: I Have a class Form1
and a class connection
. 我的问题是:我有一个类Form1
和一个类connection
。 The connection
class handles the connection and communication with the serial port. connection
类处理与串行端口的连接和通信。 Every time class connection
receives some information the OnSerialDataReceived
- Eventhandler is fired and the information string is saved in received_data
. 每次类connection
接收一些信息OnSerialDataReceived
-事件处理程序被触发,信息字符串保存在received_data
。
Now I want to update the text of a textbox in Form1
which is called tb_received
but I don´t know how to get a connection to tb_received
from my connection class. 现在,我想更新Form1
中一个名为tb_received
的文本框的文本,但是我不知道如何从我的连接类中获取到tb_received
连接。
Please help me, thanks. 请帮助我,谢谢。
Form1: 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 !");
}
}
}
}
Add an event in your Connection class and have your main form listen to the event. 在Connection类中添加一个事件,并使主窗体侦听该事件。
Edit: Forgot the code. 编辑:忘记代码。
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();
}
}
from your code, I don't see a declared textbox named tb_received in form1. 从您的代码中,我没有在form1中看到名为tb_received的已声明文本框。 anyway, you should be able to expose a public variable (maybe the tb_received) and then set it's value to received_data when the event handler is fired. 无论如何,您应该能够公开一个公共变量(也许是tb_received),然后在触发事件处理程序时将其值设置为receive_data。 see a similar situation(answered) here: Access variable from other namespaces 在这里看到类似的情况(已回答): 从其他命名空间访问变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.