the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > in the code you provided, there are some spelling mistakes and grammar question, may cause the method cannot run normally. Specifically, you need in a method name SearchAndAddSerialToComboBox with private void, and add some missing brackets in the method body. The modified code is as follows:
private void SearchAndAddSerialToComboBox(object sender, EventArgs e)
{
string Buffer;
comboBox1.Items.Clear(); //清除之前已经扫描的记录
for (int i = 0; i < 20; i++)
{
try
{
Buffer = "COM" + i.ToString(); //获得COM1-20
serialPort1.PortName = Buffer; //获取COM信息
serialPort1.Open(); //串口
comboBox1.Items.Add(Buffer);
comboBox1.Text = Buffer; //添加串口得到记录集
serialPort1.Close(); //关闭串口
}
catch
{
//MessageBox.Show("串口有问题");
//break;
}
}
}
about your second question, if you double-click event is the button1_click, so you can call SearchAndAddSerialToComboBox method in the button1_click method, in order to realize the double click on the button when scanning a serial port.