InvalidArgument=“2”的值对于“索引”无效。尝试单独键入字符时
我正在尝试输入一个每个字符之间有小空格的字符串。
然后我使用这个我收到一个错误: InvalidArgument=Value of '2' is not valid for 'index'.
in line: if (currentChar == lbMessage.Items[tickCount] .ToString().Length)
我对文本框做了类似的事情,但我不能对列表框这样做。
private void Space(object sender, EventArgs e)
{
if (tickCount < lbMessage.Items.Count)
{
SendKeys.Send(lbMessage.Items[tickCount].ToString().Substring(currentChar++, 0));
tickCount++;
if (currentChar == lbMessage.Items[tickCount].ToString().Length)
{
tmrSpace.Enabled = false;
SendKeys.Send("{enter}");
}
if (tickCount >= lbMessage.Items.Count) tickCount = 0;
}
tmrSpace.Interval = random.Next(50, 100);
}
这适用于文本框字段:
private void Space(object sender, EventArgs e)
{
SendKeys.Send(txtText.Text.Substring(b++, 1));
tmrSpace.Interval = random.Next(50, 150);
if (b == txtText.TextLength)
{
tmrSpace.Enabled = false;
SendKeys.Send("{enter}");
}
}
I'm trying to type a string with small spaces between each character.
Then I use this I'm getting an error: InvalidArgument=Value of '2' is not valid for 'index'.
in line: if (currentChar == lbMessage.Items[tickCount].ToString().Length)
I did similar with text box but I can't do it for ListBox.
private void Space(object sender, EventArgs e)
{
if (tickCount < lbMessage.Items.Count)
{
SendKeys.Send(lbMessage.Items[tickCount].ToString().Substring(currentChar++, 0));
tickCount++;
if (currentChar == lbMessage.Items[tickCount].ToString().Length)
{
tmrSpace.Enabled = false;
SendKeys.Send("{enter}");
}
if (tickCount >= lbMessage.Items.Count) tickCount = 0;
}
tmrSpace.Interval = random.Next(50, 100);
}
This works for a textbox field:
private void Space(object sender, EventArgs e)
{
SendKeys.Send(txtText.Text.Substring(b++, 1));
tmrSpace.Interval = random.Next(50, 150);
if (b == txtText.TextLength)
{
tmrSpace.Enabled = false;
SendKeys.Send("{enter}");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下:
现在假设
tickCount
恰好是lbMessage.Items.Count - 1
。 第一次索引到它,这很好 - 但随后你增加tickCount
并再次索引,此时tickCount
将等于 lbMessage.Items.Count ,并且您将得到该异常。您的代码对我来说不是特别清楚(我累了),但您可能很想将
tickCount
的增量移到代码的后面......Look at this:
Now suppose that
tickCount
is exactlylbMessage.Items.Count - 1
. The first time you index into it that's fine - but then you incrementtickCount
and index again, at which pointtickCount
will equallbMessage.Items.Count
, and you'll get that exception.Your code isn't particularly clear to me (I'm tired) but you may well want to move the increment of
tickCount
to later in the code...