|
-
August 5th, 2009, 01:24 PM
#1
Problem with collections
Hi all,
When the user clicks on an item in a listbox and clicks edit, it displays the contents of the items collection in the various fields, it is then removed from the listbox and the collection.
The user can make any changes and click add and the item is restored back into the collection. However, I am getting some errors!
This is my Edit method:
Code:
private void btnEditSelected_Click(object sender, RoutedEventArgs e)
{
getSelected = lbnames.SelectedItem.ToString();
foreach (Territories oTerritory in gbl_lisjt.lijst)
{
correctName = oTerritory.Tname.ToString();
if (correctName.Equals(getSelected) == true)
{
tbName.Text = getSelected;
tbsmallx.Text = oTerritory.Sx.ToString();
tbsmally.Text = oTerritory.Sy.ToString();
tblargex.Text = oTerritory.Lx.ToString();
tblargey.Text = oTerritory.Ly.ToString();
if (oTerritory.CbS == true)
{
cbStarting.IsChecked = true;
}
if (oTerritory.CbN == true)
{
cbNeutral.IsChecked = true;
}
if (oTerritory.CbK == true)
{
cbKiller.IsChecked = true;
}
if (oTerritory.CbB == true)
{
cbBombard.IsChecked = true;
}
gbl_lisjt.lijst.RemoveAll(d => d.Tname == lbnames.SelectedValue.ToString());
}
}
}
My Add method:
Code:
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if (tbName.Text == "")
{
System.Windows.MessageBox.Show("Please enter a territory name", "Error");
tbName.Focus();
}
else
{
tname = tbName.Text;
foreach (Territories oCheck in gbl_lisjt.lijst)
{
sReturned = oCheck.Tname.ToString();
if (sReturned.Equals(tname))
{
if (System.Windows.MessageBox.Show("You have identical territory names. Do you wish to overwrite the stored name?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
overwrite();
}
}
}
try
{
// Attempt to parse the numbers, encapsulated in a Try/Catch in case letters are entered
sx = int.Parse(tbsmallx.Text);
sy = int.Parse(tbsmally.Text);
lx = int.Parse(tblargex.Text);
ly = int.Parse(tblargey.Text);
#region IF statement covering the possible selection outcomes
// May rewrite this as a SWITCH statement later.
if (cbBombard.IsChecked == true)
{
if (cbStarting.IsChecked == true)
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, true, false, false, true));
}
else if (cbNeutral.IsChecked == true)
{
if (cbKiller.IsChecked == true)
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, true, true, true));
}
else
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, false, true, true));
}
}
else if (cbKiller.IsChecked == true)
{
if (cbNeutral.IsChecked == true)
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, true, true, true));
}
else
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, false, true, true));
}
}
else
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, false, false, true));
}
}
else
{
if (cbStarting.IsChecked == true)
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, true, false, false, false));
}
else if (cbNeutral.IsChecked == true)
{
if (cbKiller.IsChecked == true)
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, true, true, false));
}
else
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, false, true, false));
}
}
else if (cbKiller.IsChecked == true)
{
if (cbNeutral.IsChecked == true)
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, true, true, false));
}
else
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, false, true, false));
}
}
else
{
gbl_lisjt.lijst.Add(new Territories(tname, sx, sy, lx, ly, false, false, false, false));
}
}
#endregion
}
// In case user enters a non valid character into the x/y field
catch (FormatException fEx)
{
logit.WriteToLog(@"c:\logfile.log", fEx.ToString());
System.Windows.MessageBox.Show("Please enter numbers into the x/y co-ords fields", "Error");
}
// Catch everything
catch (Exception err)
{
logit.WriteToLog(@"c:\logfile.log", err.ToString());
System.Windows.MessageBox.Show("Something went wrong, please PM Premier2k for his email address");
}
lbnames.Items.Add(tbName.Text);
reset();
}
}
and the class that stores my collection:
Code:
class Territories
{
string tname;
int sx, sy, lx, ly;
bool cbS, cbK, cbN, cbB;
public ArrayList aBorder, aBombard;
public enum ArrayListType { border = 1, bombard = 2 }
public string Tname
{
get { return this.tname; }
set { this.tname = value; }
}
public int Sx
{
get { return this.sx; }
set { this.sx = value; }
}
public int Sy
{
get { return this.sy; }
set { this.sy = value; }
}
public int Lx
{
get { return this.lx; }
set { this.lx = value; }
}
public int Ly
{
get { return this.ly; }
set { this.ly = value; }
}
public bool CbS
{
get { return this.cbS; }
set { this.cbS = value; }
}
public bool CbK
{
get { return this.cbK; }
set { this.cbK = value; }
}
public bool CbN
{
get { return this.cbN; }
set { this.cbN = value; }
}
public bool CbB
{
get { return this.cbB; }
set { this.cbB = value; }
}
public Territories(string tname, int sx, int sy, int lx, int ly, bool cbS, bool cbK, bool cbN, bool cbB)
{
this.Tname = tname;
this.Sx = sx;
this.Sy = sy;
this.Lx = lx;
this.Ly = ly;
this.CbS = cbS;
this.CbK = cbK;
this.CbN = cbN;
this.CbB = cbB;
}
public Territories(string tname, ArrayList list, ArrayListType type)
{
this.tname = tname;
if (type == ArrayListType.border)
{
this.aBorder = list;
}
else if (type == ArrayListType.bombard)
{
this.aBombard = list;
}
}
}
static class gbl_lisjt
{
static gbl_lisjt()
{
Debug.WriteLine("** creating application scope instance **");
lijst = new List<Territories> ();
}
static public List<Territories> lijst;
}
Now when I click an item in the listbox and click Edit I get this exception error message: Collection was modified; enumeration operation may not execute.
The error occurs in the Edit method on this line:
Code:
foreach (Territories oTerritory in gbl_lisjt.lijst)
and highlights the word 'in'
What does that mean? I checked the help but that means less to me than the message itself!
Premier2k
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|