从事件处理程序更改textBox.text

时间:2019-01-15 17:55:39

标签: c# events serial-port handler

在这里似乎找不到我生命的答案。 无论如何,事件处理程序如何更改Form的textBox.text?

private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            //Do what ever you want with the data
            textBox2.Text = "test"; //alas, this doesn't work
        }

按下按钮时会创建事件处理程序

private void button3_Click(object sender, EventArgs e)
        {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                    serialPort1.RtsEnable = true;
                    serialPort1.DtrEnable = true;
                    serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                    serialPort1.Open();
                }
            }

非常感谢您的帮助

2 个答案:

答案 0 :(得分:0)

DataReceivedHandler方法被标记为static,因此它无法访问实例成员textBox2。删除static是否可以解决问题?

答案 1 :(得分:0)

using System; using System.Collections.Generic; using System.Linq; using System.Web; using ClosedXML.Excel; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Mvc; namespace MyProject.DAL { public class GenerateBook { private DataTable get_TblA() { using (SqlConnection con = Connection.GetConnection()) { using (SqlCommand cmd = new SqlCommand("SELECT cola, colB FROM dbo.tblA;")) { using (SqlDataAdapter da = new SqlDataAdapter()) { DataTable dt = new DataTable(); cmd.CommandType = CommandType.Text; cmd.Connection = con; da.SelectCommand = cmd; da.Fill(dt); return dt; } } } } private DataTable get_TblB() { using (SqlConnection con = Connection.GetConnection()) { using (SqlCommand cmd = new SqlCommand("SELECT cola, colB FROM dbo.tblB;")) { using (SqlDataAdapter da = new SqlDataAdapter()) { DataTable dt = new DataTable(); cmd.CommandType = CommandType.Text; cmd.Connection = con; da.SelectCommand = cmd; da.Fill(dt); return dt; } } } } public DataSet getDataSetExportToExcel() { DataSet ds = new DataSet(); DataTable dtEmp = new DataTable("ALM_List_LOB"); dtEmp = getUIP_ALM_List(); DataTable dtEmpOrder = new DataTable("GPR_LOB_CMS_VDN"); dtEmpOrder = getGPR_LOB_CMS_VDN(); ds.Tables.Add(dtEmp); ds.Tables.Add(dtEmpOrder); return ds; } public DataSet GetDataSetExportToExcel() { //use for multiple sps DataSet ds = new DataSet(); var SPNames = new List<string>() { "storedproc1", "storedproc2" }; foreach (var SPName in SPNames) { DataTable dt = new DataTable(); dt = GetDataTableExportToExcel(SPName); ds.Tables.Add(dt); } return ds; } private DataTable GetDataTableExportToExcel(string SPName) { //use for single sp DataTable dt = new DataTable(); using (SqlConnection con = Connection.GetConnection()) { using (var cmd = new SqlCommand(SPName, con)) { using (var sda = new SqlDataAdapter(cmd)) { cmd.CommandType = CommandType.StoredProcedure; //cmd.Parameters.AddWithValue() sda.Fill(dt); return dt; } } } } public ActionResult ExportToExcel() { try { var sheetNames = new List<string>() { "sheetName1", "sheetName2" }; string fileName = "Example.xlsx"; // for sps DataSet ds = GetDataSetExportToExcel(); DataSet ds = getDataSetExportToExcel(); XLWorkbook wbook = new XLWorkbook(); for (int k = 0; k < ds.Tables.Count; k++) { DataTable dt = ds.Tables[k]; IXLWorksheet Sheet = wbook.Worksheets.Add(sheetNames[k]); for (int i = 0; i < dt.Columns.Count; i++) { Sheet.Cell(1, (i + 1)).Value = dt.Columns[i].ColumnName; } for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { Sheet.Cell((i + 2), (j + 1)).Value = dt.Rows[i][j].ToString(); } } } Stream spreadsheetStream = new MemoryStream(); wbook.SaveAs(spreadsheetStream); spreadsheetStream.Position = 0; return new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = fileName }; } catch(Exception e) { throw e; } } public SetToExport(string channel, string assets ) { ActionResult status = ExportToExcel(); } } 位于其自己的线程上,您需要调用GUI才能更新文本框。

DataReceived