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]

解决方案 »

  1.   

    elems只有一个元素吧? F10调试之   结贴
      

  2.   

    判断elems.Length
    单步跟踪值
      

  3.   


    这是一个输入ip的控件,elems是一个输入的ip地址,通过“.”分割获得每一位ip
      

  4.   

     String[] elems=new string[4];
      elems = value.Split('.');
    改为
    string elems = value.Split('.');
    这样就不会啦!~
    要取数据跟数组一样!~
      

  5.   

    string[] elems = value.Split('.');
      

  6.   

    关键是开始限制了数组的长度,直接获取string[]数组就可以了。
      

  7.   

     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();
      }试试?会不会提示错