question

DanielMouyal-8389 avatar image
0 Votes"
DanielMouyal-8389 asked DanielMouyal-8389 answered

Error on Windows Form That Run Serial Port

Hello to All Helper I make a small project that runs windows form with a serial port select and run a command all good but when I add if and else to this form I get error

this is the project


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;
using System.Diagnostics;
using System.IO.Ports;

namespace Daniel_Mouyal____Ultimate_Tool
{
public partial class Samsung_Factory_Reset : Form
{

     public Samsung_Factory_Reset()
     {

         InitializeComponent();
         getallports();
     }
     void getallports()

     {
         string[] ports = SerialPort.GetPortNames();
         cmbPort.Items.AddRange(ports);
     }
     private void guna2Button1_Click(object sender, EventArgs e)
     {
         cmbPort.Text = "";
     }

     private void pictureBox1_Click(object sender, EventArgs e)
     {

     }

     private void Exit_Click(object sender, EventArgs e)
     {
         this.Close();
     }

     private void btnFactoryReset_Click(object sender, EventArgs e)
     {


         if (cmbPort.Text == "")
             MessageBox.Show("Please Select Port");



         else

         {
         }
            

        System.IO.Ports.SerialPort sport = new System.IO.Ports.SerialPort(cmbPort.Text, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);

         try


         {
             sport.Open();
             sport.Write("AT+FACTORST=0,0\r\n");
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.ToString());


         }



         sport.Close();

     }
         
      

     private void btnExit_Click(object sender, EventArgs e)
     {
         this.Close();
     }
 }
 }

this is the error that I get


142860-error.jpg


windows-forms
error.jpg (108.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

Try adding '{ }' and 'return':

 . . .
 if (cmbPort.Text == "")
 {
    MessageBox.Show("Please Select Port");
    return;
 }
 . . .


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielMouyal-8389 avatar image
0 Votes"
DanielMouyal-8389 answered

Thanks it's work you are really help me

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.