本文介绍了C#如何第二次重置增量整数变量被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 命名空间 ProjectOne { public partial class MainForm:Form { public string DataToSetandGet = String .Empty; private void StatusBtn_Click(对象发件人,EventArgs e) { 字符串 StatusInfoToshow = ?S\r; string commForMeter = string .Format(StatusInfoToshow); 尝试 { 如果(statusofMeter。 serialPortForApp.IsOpen) { statusofMeter.serialPortForApp.Write(commForMeter); } } catch (例外) { 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,} $; 匹配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 )。替换( \ n, )。替换( \ rr, \\\\ n)); }); } } } } 我尝试过: public void SerialPortInApp_DataReceived( object sender, SerialDataReceivedEventArgs e) { DataToSetandGet = serialPortForApp.ReadExisting(); string pattern = @ ^ [A-ZA-Z0-9\s\- \ * \ / \:???\\r] {1,} $; 匹配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 )。替换( \ n, )。替换( \ rr, \\\\ n));}); } } } private static int lineReadCounter = 0 ; private void CountReadings() { // 计数读数 string []行= Regex.Split(textbox1.Text.Trim(), \\\\ n); // 修剪避免空格 lineReadCounter = lines.Length; label1.Text = No。读数: + lineReadCounter.ToString(); } 如果我第一次按下按钮StatusBtn_Click显示lineReadCounter从1开始。 但是如果我按下第二次或更多次按钮它将从2开始返回lineReadCounter 如何每次按下按钮时重置变量从1开始解决方案 ; 匹配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 )。替换( \ n, )。替换( \ r, \\\\ n)); }); } } } } 我尝试过: public void SerialPortInApp_DataReceived( object sender, SerialDataReceivedEventArgs e) { DataToSetandGet = serialPortForApp.ReadExisting(); string pattern = @ ^ [A-ZA-Z0-9\s\- \ * \ / \:???\\r] {1,} ; 匹配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 )。替换( \ n, )。替换( \ rr, \\\\ n));}); } } } private static int lineReadCounter = 0 ; private void CountReadings() { // 计数读数 string []行= Regex.Split(textbox1.Text.Trim(), \\\\ n); // 修剪避免空格 lineReadCounter = lines.Length; label1.Text = No。读数: + lineReadCounter.ToString(); } 如果我第一次按下按钮StatusBtn_Click显示lineReadCounter从1开始。 但是如果我按下第二次或更多次按钮它将从2开始返回lineReadCounter 如何每次按下按钮时重置变量从1开始 Quote:如何重置每次按下按钮时从1开始的变量? 最直接的方法是重置按钮代码开头的值。 lineReadCounter = 0 ; 引用:如果我第一次按下按钮StatusBtn_Click它将从1开始显示lineReadCounter 但是如果我第二次按下按钮或更多次它会返回lineReadCounter从2开始 代码只会通过使变量变为静态来执行您所要求的操作。 private static int lineReadCounter = 0 ; 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: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 2How to reset the variable to start from 1 every time the button pressed 解决方案 ";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: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 2How to reset the variable to start from 1 every time the button pressedQuote: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.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 2The code is only doing what you asked for by making the variable static.private static int lineReadCounter = 0; 这篇关于C#如何第二次重置增量整数变量被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 09:36