【问题标题】:Port Reading (Scale Weight) error somewhere某处的端口读数(秤重)错误
【发布时间】:2013-04-09 18:59:14
【问题描述】:

您好,代码如下所示...

尝试从 com 端口读取并显示发送的重量 从比例指示器到文本框,我收到一条错误消息 -

WindowsFormsApplication1.Form1.textBox1_TextChanged(object, System.EventArgs)' 必须声明一个主体,因为它没有标记为抽象、外部或部分

我是 C# 新手,请帮忙

private void textBox1_TextChanged(object sender, EventArgs e);
}
namespace Read_serial
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            serialPort1.PortName = "COM1";
            serialPort1.BaudRate = 9600;
            serialPort1.DtrEnable = true;
            serialPort1.Open();

            serialPort1.DataReceived += serialPort1_DataReceived;
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string line = serialPort1.ReadLine();
            this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
        }

        private delegate void LineReceivedEvent(string line);
        private void LineReceived(string line)
        {
            //What to do with the received line here
            textBox1.Text = line;

            progressBar1.Value = int.Parse(line);
        }
    }
}

}

【问题讨论】:

    标签: c# serial-port


    【解决方案1】:

    我没有在 Visual Studio 中尝试过,但请注意您的第一行:

    private void textBox1_TextChanged(object sender, EventArgs e);
    

    比较:

    private void textBox1_TextChanged(object sender, EventArgs e)
    

    ;最后几乎说“我完成了!” ...因此没有身体。

    我不确定“}”是如何出现在该行之后的;上面的代码是否丢失?此外,它后面还有通常位于源文件顶部的语句。

    我会尝试删除该行和括号。还要检查带有表单的其他文件,例如 textBox1.TextChanged += textBox1_TextChanged,也将其删除 - 也许您可以在 IDE 中删除该事件(自从我使用 C# 以来已经有一段时间了)。在您的解决方案中搜索 textbox1_textchanged 以确定。

    然后根据需要重新添加该事件。现在它只是给你搞砸了。

    【讨论】:

      猜你喜欢
      • 2017-05-27
      • 2021-09-20
      • 1970-01-01
      • 2018-09-21
      • 2017-01-24
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多