Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
namespace ProjectOne
    {
     public partial class MainForm : Form
     {
        public string DataToSetandGet = String.Empty;

      private void StatusBtn_Click(object sender, EventArgs e)
        {
            
            string StatusInfoToshow = "?S\r";
            string commForMeter = string.Format(StatusInfoToshow);

            try
            {
                if (statusofMeter.serialPortForApp.IsOpen)
                {
                    
                    statusofMeter.serialPortForApp.Write(commForMeter);
                }
            }
            catch (Exception)
            {
                statusofMeter.ShowDataInScreenTxtb.Text = "TimeOUT Exception";
            }
        }

public void SerialPortInApp_DataReceived(object sender, 
SerialDataReceivedEventArgs e)
{
DataToSetandGet = serialPortForApp.ReadExisting();
           
string pattern = @"^[a-zA-Z0-9\s\-?\*?\.?\/\:\\r]{1,}$";
Match rgxs = Regex.Match(DataToSetandGet, pattern);

if (rgxs.Success && DataToSetandGet.Length > 50 && DataToSetandGet.IndexOf("\r") == DataToSetandGet.Length - 1)
{
                            
 lineReadCounter++;
 this.BeginInvoke((Action)delegate ()
{
                                ShowDataInScreenTxtb.AppendText(lineReadCounter.ToString() + DataToSetandGet.Substring(4).Replace("\n", " ").Replace("\r", "\r\n"));

 });

  }
}
}
}


What I have tried:

C#
public void SerialPortInApp_DataReceived(object sender, 
     SerialDataReceivedEventArgs e)
 {
   DataToSetandGet = serialPortForApp.ReadExisting();

   string pattern = @"^[a-zA-Z0-9\s\-?\*?\.?\/\:\\r]{1,}$";

   Match rgxs = Regex.Match(DataToSetandGet, pattern);

   if (rgxs.Success && DataToSetandGet.Length > 50 && DataToSetandGet.IndexOf("\r") 
    == DataToSetandGet.Length - 1)
   {
                            
     lineReadCounter++;
     this.BeginInvoke((Action)delegate ()
     {
      Textbo1.AppendText(lineReadCounter.ToString() + 
      DataToSetandGet.Substring(4).Replace("\n", " ").Replace("\r", "\r\n"));});
     }
   }
 }

 private static int lineReadCounter = 0;
 private void CountReadings()
 {
   //Count Readings
   string[] lines = 
    Regex.Split(textbox1.Text.Trim(), "\r\n");//Trim avoid white spaces
   lineReadCounter = lines.Length;
   label1.Text = "No. Readings: " + lineReadCounter.ToString();

  }



if I press button StatusBtn_Click the first time it will display lineReadCounter starting from 1.
But if I press the button for second time or more times it will return lineReadCounter starting from 2

How to reset the variable to start from 1 every time the button pressed
Posted
Updated 24-Aug-18 1:26am
v4
Comments
Patrice T 21-Aug-18 23:03pm    
And what is the input ?

Quote:
How to reset the variable to start from 1 every time the button pressed ?

the most strait forward way is to reset the value at beginning of button code.
C#
lineReadCounter = 0;

Quote:
if I press button StatusBtn_Click the first time it will display lineReadCounter starting from 1.
But if I press the button for second time or more times it will return lineReadCounter starting from 2

The code is only doing what you asked for by making the variable static.
C#
private static int lineReadCounter = 0;
 
Share this answer
 
v2
You can declare int lineReadCounter = 0 variable inside btn click function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900