本文介绍了修复错误{“长度不能小于零。\\\\参数名称:长度”}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在尝试此代码并在文本框中搜索值但是我收到此错误并且应用程序已停止。我怎样才能修复它
Hi
I am trying this code and searching the values in the text box however i am getting this error and application is stopped. How can I FIX IT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SerialPortListener.Serial;
using System.IO;
namespace SerialPortListener
{
public partial class MainForm : Form
{
SerialPortManager _spManager;
public MainForm()
{
InitializeComponent();
UserInitialization();
}
private void UserInitialization()
{
_spManager = new SerialPortManager();
SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;
serialSettingsBindingSource.DataSource = mySerialSettings;
portNameComboBox.DataSource = mySerialSettings.PortNameCollection;
baudRateComboBox.DataSource = mySerialSettings.BaudRateCollection;
dataBitsComboBox.DataSource = mySerialSettings.DataBitsCollection;
parityComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));
_spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_spManager.Dispose();
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
int maxTextLength = 100000; // maximum text length in text box
if (tbData.TextLength > maxTextLength)
tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);
string str = Encoding.ASCII.GetString(e.Data);
tbData.AppendText(str);
string toFind1 = "Total:";
string toFind2 = "Coin:";
int start = str.IndexOf(toFind1) + toFind1.Length;
int end = str.IndexOf(toFind2, start);
string str1 = str.Substring(start, end - start);
dataGridView1.Rows[0].Cells[0].Value = str1;
}
// Handles the "Start Listening"-button click event
private void btnStart_Click(object sender, EventArgs e)
{
_spManager.StartListening();
}
// Handles the "Stop Listening"-button click event
private void btnStop_Click(object sender, EventArgs e)
{
_spManager.StopListening();
}
private void serialSettingsBindingSource_CurrentChanged(object sender, EventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void tbData_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
}
}
推荐答案
int end = str.IndexOf(toFind2, start);
返回一个负数,或者字符串Coin:出现在用户条目中的Total:之前。
因此,这一行:
And returning a negative number, or the string "Coin:" appears before "Total:" in your user entry.
As a result, this line:
string str1 = str.Substring(start, end - start);
获取负长度并抛出异常。
Gets given a negative length and throws an exception.
这篇关于修复错误{“长度不能小于零。\\\\参数名称:长度”}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!