Reputation: 101
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;
}
}
}
I get the error when I try to assign value to a textbox
from the selectedindex
but the selectedindex
is 0/-1 and crashes the program so what I tried to do was assign a selectedindex
on a refresh button(button 4) but doesn't seem to be working.
An exception of type 'System.IndexOutOfRangeException' occurred
Error happens on the case load when it try to assign a value to the textbox
by getting the indexnumber
off the listbox
so it knows what is selected.
Upvotes: 0
Views: 3759
Reputation: 26209
Step 1: if there are no items in the LisBox
make selected index as -1
Replace This:
listBox1.SelectedIndex = 0;
With This:
listBox1.SelectedIndex = -1;
Step 2: while getting the selctedindex value first check if there are any selected items or not
Replace This:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
indexnumber = listBox1.SelectedIndex;
loadListBox(indexnumber, 0, "load");
}
WIth This:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex >= 0)
{
indexnumber = listBox1.SelectedIndex;
loadListBox(indexnumber, 0, "load");
}
}
Upvotes: 1
Reputation: 734
if selectedindex is 0, the first element is selected, if its -1, then no element is selected. You should only get an IndexOutOfRangeException if you try to access your arrays at position -1.
First step is to check if selected index is -1, and if, not try to access an array at that position.
Set a breakpoint at your selection changed event handler, and look if it gets called multiple times maybe.
Upvotes: 0