public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
checkxml();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
private void displayMessageBox(object sender, FormClosingEventArgs e)
{
}
private void sluitenToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
}
private void button2_Click(object sender, EventArgs e)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("mp3spelers.xml");
string text = listBox1.GetItemText(listBox1.SelectedItem);
foreach (XmlNode xNode in xDoc.SelectNodes("Players/Player"))
{
if (xNode.SelectSingleNode("make").InnerText == text) xNode.ParentNode.RemoveChild(xNode);
xDoc.Save("mp3spelers.xml");
}
}
public List<string> _items = new List<string>();
public void checkxml()
{
string[] arr = XDocument.Load("mp3spelers.xml").Root
.Descendants("make")
.Select(make => (string)make)
.ToArray();
for (int i = 0; i < arr.Length; i++)
{
_items.Add(arr[i]);
}
listBox1.DataSource = _items;
}
public int indexnumber;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
indexnumber = listBox1.SelectedIndex;
loadListBox(indexnumber, 0, "load");
}
public void loadListBox(int index, int mutatie, string action)
{
List<string> _modle = new List<string>();
string[] arr1 = XDocument.Load("mp3spelers.xml").Root
.Descendants("modle")
.Select(modle => (string)modle)
.ToArray();
List<string> _mbstorage = new List<string>();
string[] arr2 = XDocument.Load("mp3spelers.xml").Root
.Descendants("mbstorage")
.Select(mbstorage => (string)mbstorage)
.ToArray();
List<string> _price = new List<string>();
string[] arr3 = XDocument.Load("mp3spelers.xml").Root
.Descendants("price")
.Select(price => (string)price)
.ToArray();
List<string> _storage = new List<string>();
string[] arr4 = XDocument.Load("mp3spelers.xml").Root
.Descendants("storage")
.Select(storage => (string)storage)
.ToArray();
switch (action)
{
case "load":
textBox1.Clear();
textBox1.Text = "Model: " + arr1[index] + "\r\n" + "Opslag in MB: " + arr2[index] + "\r\n" + "Prijs: " + arr3[index] + "\r\n" + "Voorraad: " + arr4[index];
break;
case "mutate":
int indexnumber = listBox1.SelectedIndex;
int mutatienummer = Convert.ToInt32(arr4[indexnumber]);
int mutatienummer2 = mutatienummer + mutatie;
XmlDocument xDoc = new XmlDocument();
xDoc.Load("mp3spelers.xml");
string text = arr4[indexnumber];
foreach (XmlNode xNode in xDoc.SelectNodes("Players/Player"))
{
if (xNode.SelectSingleNode("storage").InnerText == text)
xDoc.Save("mp3spelers.xml");
}
break;
}
}
private void infoToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Hide();
button1.Hide();
button2.Hide();
button3.Hide();
textBox1.Clear();
textBox1.Text = "Soundsharp";
}
private void mP3SpelersToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Show();
button1.Show();
button2.Show();
button3.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form4 form4 = new Form4();
form4.Show();
}
private void button4_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = 0;
listBox1.DataSource = null;
checkxml();
listBox1.SelectedIndex = 1;
}
}
}
当我尝试从textbox
为selectedindex
分配值但selectedindex
为0 / -1并且崩溃程序时,我收到错误,所以我尝试做的是在刷新按钮(按钮4)上分配selectedindex
但似乎无法正常工作。
类型&#39; System.IndexOutOfRangeException&#39;的例外情况发生了
在尝试通过textbox
关闭indexnumber
来listbox
向{{1}}分配值时,案例加载发生错误,因此它知道所选内容。
答案 0 :(得分:1)
第1步:如果LisBox
选择的索引中没有项目为-1
替换它:
listBox1.SelectedIndex = 0;
有了这个:
listBox1.SelectedIndex = -1;
第2步:,同时获取selctedindex值,首先检查是否有任何选定的项目
替换它:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
indexnumber = listBox1.SelectedIndex;
loadListBox(indexnumber, 0, "load");
}
用这个:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex >= 0)
{
indexnumber = listBox1.SelectedIndex;
loadListBox(indexnumber, 0, "load");
}
}
答案 1 :(得分:0)
如果selectedindex为0,则选择第一个元素, 如果为-1,则不选择任何元素。 如果您尝试在位置-1处访问数组,则应该只获得IndexOutOfRangeException。
第一步是检查所选索引是否为-1,以及是否尝试访问该位置的数组。
在选择更改的事件处理程序中设置断点,并查看是否多次调用它。