今天联系这个实例,总是出现错误“索引超出范围。必须为非负值并小于集合大小的解决方法”,找了半天,最终解决。产生原因是数组中少元素或者没元素。
以下是实现全部代码,statusStrip1控件中要添加toolStripStatusLabel元素,每怎加1个,statusStrip1.Items[1]的元素也增加1个,如果不加就会出现上述错误。
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; namespace lx_004 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (this.checkBox1.Checked) { //this.toolStripStatusLabel1.Text = "日期:"+DateTime.Now.ToString("yyyy年MM月dd日"); statusStrip1.Items[1].Text = "日期:" + DateTime.Now.ToString("yyyy年MM月dd日"); } else { //this.toolStripStatusLabel1.Text = ""; statusStrip1.Items[1].Text = ""; } } } }