索引超出数组范围

lifegreen 2010-06-10 09:01:30
private void MaskIpAddr(System.Windows.Forms.TextBox textBox, KeyPressEventArgs e)
{
int len = textBox.Text.Length;
if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8)
{
if (e.KeyChar != 8)
{
if (len == 2 && e.KeyChar != '.')
{
string tmp = textBox.Text + e.KeyChar;
if (textBox.Name == "textBox1")
{
if (Int32.Parse(tmp) > 223) // 进行验证
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。");
textBox.Text = "223";
textBox.Focus();
return;
}
textBox2.Focus();
textBox2.SelectAll();

}
else if (textBox.Name == "textBox2")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
textBox3.Focus();
textBox3.SelectAll();
}
else if (textBox.Name == "textBox3")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
textBox4.Focus();
textBox4.SelectAll();
}
else if (textBox.Name == "textBox4")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}

}

}
if (e.KeyChar == '.')
{
if (textBox.Name == "textBox1" && textBox.Text != "")
{
textBox2.Focus();
textBox2.SelectAll();
}
if (textBox.Name == "textBox2" && textBox.Text != "")
{
textBox3.Focus();
textBox3.SelectAll();
}
if (textBox.Name == "textBox3" && textBox.Text != "")
{
textBox4.Focus();
textBox4.SelectAll();
}
if (textBox.Name == "textBox4" && textBox.Text != "")
{

}
e.Handled = true;
}
}
else
{
if (textBox.Name == "textBox1" && textBox.Text == "")
{

}
if (textBox.Name == "textBox2" && textBox.Text == "")
{
textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length;
}
if (textBox.Name == "textBox3" && textBox.Text == "")
{
textBox2.Focus();
textBox2.SelectionStart = textBox2.Text.Length;
}
if (textBox.Name == "textBox4" && textBox.Text == "")
{
textBox3.Focus();
textBox3.SelectionStart = textBox3.Text.Length;
}
e.Handled = false;
}
}
else
e.Handled = true;
}

[Browsable(true)]
/// <summary>
/// 获取 IpBox 的文本。
/// </summary>
public new string Text
{
get
{
if (textBox1.Text == ""
|| textBox2.Text == ""
|| textBox3.Text == ""
|| textBox4.Text == "")
{
text ="";
return text;
}
else
{
text = Convert.ToInt32(textBox1.Text).ToString() + "." + Convert.ToInt32(textBox2.Text).ToString() + "." + Convert.ToInt32(textBox3.Text).ToString() + "." + Convert.ToInt32(textBox4.Text).ToString();
return text;
}

}
set
{
String[] elems=new string[4];
elems = value.Split('.');
textBox1.Text = elems[0];
textBox2.Text = elems[1];///[color=#339966]每次运行到这个位置,都会提示索引超出数组界限
textBox3.Text = elems[2];
textBox4.Text = elems[3];
textBox4.Focus();
}


}
}
}

在线等,谢谢了各位[/color]
...全文
472 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
貌似都写的有点复杂....
东莞寻香苑 2010-06-10
  • 打赏
  • 举报
回复
set
{
String[] elems=new string[4];
elems = value.Split('.');
if(elems.Count<4)
{
MessageBox.Show("IP:"+value.ToString()+" IP不正确或 value.Split()分组不正确,请检查");
}

textBox1.Text = elems[0];
textBox2.Text = elems[1];///[color=#339966]每次运行到这个位置,都会提示索引超出数组界限 textBox3.Text = elems[2];
textBox4.Text = elems[3];
textBox4.Focus();
}

试试?会不会提示错
guoweidong 2010-06-10
  • 打赏
  • 举报
回复
关键是开始限制了数组的长度,直接获取string[]数组就可以了。
wuyq11 2010-06-10
  • 打赏
  • 举报
回复
string[] elems = value.Split('.');
xk1126 2010-06-10
  • 打赏
  • 举报
回复
String[] elems=new string[4];
elems = value.Split('.');
改为
string elems = value.Split('.');
这样就不会啦!~
要取数据跟数组一样!~
lifegreen 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 foxdave 的回复:]
elems只有一个元素吧? F10调试之 结贴
[/Quote]

这是一个输入ip的控件,elems是一个输入的ip地址,通过“.”分割获得每一位ip
wuyq11 2010-06-10
  • 打赏
  • 举报
回复
判断elems.Length
单步跟踪值
Justin-Liu 2010-06-10
  • 打赏
  • 举报
回复
elems只有一个元素吧? F10调试之 结贴

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧