1

嗨,代码如下所示...

试图从 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);
        }
    }
}

}

4

1 回答 1

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 以确定。

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

于 2013-04-09T19:20:24.127 回答