Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# error i dont know where i going wrong it say identifer expected in line 281 Pin
j k Nov202325-Nov-23 0:56
j k Nov202325-Nov-23 0:56 
GeneralRe: C# error i dont know where i going wrong it say identifer expected in line 281 Pin
j k Nov202324-Nov-23 23:32
j k Nov202324-Nov-23 23:32 
QuestionUpdate text box from serial data Pin
Iskander1234519-Nov-23 21:41
Iskander1234519-Nov-23 21:41 
AnswerRe: Update text box from serial data Pin
Victor Nijegorodov20-Nov-23 0:18
Victor Nijegorodov20-Nov-23 0:18 
AnswerRe: Update text box from serial data Pin
lmoelleb20-Nov-23 5:44
lmoelleb20-Nov-23 5:44 
AnswerRe: Update text box from serial data Pin
Gerry Schmitz20-Nov-23 6:39
mveGerry Schmitz20-Nov-23 6:39 
AnswerRe: Update text box from serial data Pin
jschell20-Nov-23 7:34
jschell20-Nov-23 7:34 
GeneralRe: Update text box from serial data Pin
Iskander1234520-Nov-23 19:29
Iskander1234520-Nov-23 19:29 
hi guys, so I changed my code and the problem in that particular place is solved but reemerging elsewhere. So I have two buttons now. One button (button1) is used to open and close the port, second (button3) is used to start filling up text boxes from the serial port. What is happening is that when I open the port and click start it starts filling the text boxes fine. but when I click stop (button3) the command to clear the text boxes is not getting executed. Also subsequently when I close the port (button1) and reopen it (button1 again) then click button 3 again to start filling the text boxes again, the same error repeats (index is out of bounds of the array) . Below is my new code.

private bool isFirstLine = true;
        void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                string dataLine = serialPort1.ReadLine();
                if (dataLine == null || dataLine.Trim().Length == 0)
                {
                    return;
                }



                // Skip the first line
                if (isFirstLine)
                {
                    isFirstLine = false;
                    return;
                }

                // Split data into individual values
                string[] dataValues = dataLine.Split('\t');

                // Update text boxes with corresponding values
                this.Invoke((MethodInvoker)delegate
                {
                    //ClearTextBoxes();
                    textBox2.Text = dataValues[0];
                    textBox3.Text = dataValues[1];
                    textBox4.Text = dataValues[2];
                    textBox5.Text = dataValues[3];
                    textBox6.Text = dataValues[4];
                    textBox7.Text = dataValues[5];
                    textBox8.Text = dataValues[6];
                    textBox9.Text = dataValues[7];
                    textBox10.Text = dataValues[8];
                    textBox11.Text = dataValues[9];
                    textBox12.Text = dataValues[10];
                    textBox13.Text = dataValues[11];
                    textBox14.Text = dataValues[12];
                    textBox15.Text = dataValues[13];
                    textBox16.Text = dataValues[14];
                    textBox17.Text = dataValues[15];
                    // ... and so on for all text boxes


                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            


        }

        private void ClearTextBoxes()
        {
            // Add all your text boxes here
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
            textBox13.Text = "";
            textBox14.Text = "";
            textBox15.Text = "";
            textBox16.Text = "";
            textBox17.Text = "";
            // ... and so on for all text boxes
        }

        private bool isReadingData = false;
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    if (!isReadingData)
                    {
                        // Register the data reception handler
                        serialPort1.WriteLine("1");
                        serialPort1.DataReceived += DataReceivedHandler;
                        isReadingData = true;
                        button3.Text = "STOP";
                        button1.Enabled = false;
                        button1.Visible = false;
                    }
                    else
                    {
                        // Unsubscribe from the DataReceivedHandler
                        serialPort1.WriteLine("0");
                        serialPort1.DataReceived -= DataReceivedHandler;

                        // Set the flag to indicate that data reading is stopped
                        isReadingData = false;
                        button3.Text = "START";
                        button1.Enabled = true;
                        button1.Visible = true;
                        ClearTextBoxes();

                    }

                }
                else
                {
                    MessageBox.Show("Error : Port needs to be open or wrong port selected!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


private void button1_Click(object sender, EventArgs e)
        {
            if(serialPort1.IsOpen)
            {
                PortClosing();
                button1.Text = "Open";
                ClearTextBoxes();
                button3.Visible = false;

            }
            else
            {
                PortOpening();
                button1.Text = "Close";
                button3.Visible = true;
            }
        }


modified 21-Nov-23 2:41am.

GeneralRe: Update text box from serial data Pin
jschell21-Nov-23 5:21
jschell21-Nov-23 5:21 
QuestionSystem.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Member 1612194217-Nov-23 3:09
Member 1612194217-Nov-23 3:09 
AnswerRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Dave Kreskowiak17-Nov-23 3:25
mveDave Kreskowiak17-Nov-23 3:25 
AnswerRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
trønderen17-Nov-23 3:46
trønderen17-Nov-23 3:46 
GeneralRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Member 1612194217-Nov-23 4:02
Member 1612194217-Nov-23 4:02 
GeneralRe: System.Data.SQLite.SQLiteException: 'SQL logic error no such table: Incident' Pin
Dave Kreskowiak17-Nov-23 5:33
mveDave Kreskowiak17-Nov-23 5:33 
GeneralC# 12: New opportunities for auto-foot-wounding Pin
Richard Deeming16-Nov-23 0:24
mveRichard Deeming16-Nov-23 0:24 
GeneralRe: C# 12: New opportunities for auto-foot-wounding Pin
Richard MacCutchan16-Nov-23 1:11
mveRichard MacCutchan16-Nov-23 1:11 
GeneralRe: C# 12: New opportunities for auto-foot-wounding Pin
jschell16-Nov-23 5:18
jschell16-Nov-23 5:18 
QuestionC#, Winforms, Coding Pin
Hoàng Anh Quốc4-Nov-23 0:00
Hoàng Anh Quốc4-Nov-23 0:00 
AnswerRe: C#, Winforms, Coding Pin
Richard MacCutchan4-Nov-23 0:11
mveRichard MacCutchan4-Nov-23 0:11 
AnswerRe: C#, Winforms, Coding Pin
Andre Oosthuizen10-Nov-23 6:28
mveAndre Oosthuizen10-Nov-23 6:28 
QuestionHow to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine1-Nov-23 4:30
Fokwa Divine1-Nov-23 4:30 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Richard Deeming1-Nov-23 5:47
mveRichard Deeming1-Nov-23 5:47 
GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine1-Nov-23 8:51
Fokwa Divine1-Nov-23 8:51 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
jschell2-Nov-23 4:30
jschell2-Nov-23 4:30 
GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine3-Nov-23 6:34
Fokwa Divine3-Nov-23 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.