btnConfirmedPlayersToday_Click to create panels: works fine I have a pause built in via messagebox Then reverse for loop to delete the panels: works fine This is just the testing area to see if logic on the reverse for loop is correct. And it works.
btnClearPlayerRegion_Click: This is were I want to delete the panels that were created in btnConfirmedPlayersToday_Click However, I get the out of bounds error message.
If you run the code and I hope someone does to help me out, make the size of your form about 1084, 1000 so you see the panels when made.
Thanks again and here is the code.
C#
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;
namespaceHelpMePanelDelete
{
publicpartialclassForm1 : Form
{
public List PlayerName = new List(); // use to decide who playing todaypublic List MyPanels = new List();
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(object sender, EventArgs e)
{
this.checkedListBox1.Items.Add("Tim Camper");
this.checkedListBox1.Items.Add("Al Foster");
this.checkedListBox1.Items.Add("Rick Hall");
this.checkedListBox1.Items.Add("John Brookhart");
this.checkedListBox1.Items.Add("Shane Collins");
this.checkedListBox1.Items.Add("Shay Camper");
this.checkedListBox1.Items.Add("Mike Collins");
this.checkedListBox1.Items.Add("Tim Camper Jr");
this.checkedListBox1.Items.Add("George Clark");
this.checkedListBox1.Items.Add("Larry Crammer");
this.checkedListBox1.Items.Add("George Rentfrow");
this.checkedListBox1.Items.Add("Santa Claus");
}
privatevoidcheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
this.listBox1.Items.Add(this.checkedListBox1.SelectedItem.ToString());
}
else
{
this.listBox1.Items.Remove(this.checkedListBox1.SelectedItem.ToString());
}
}
privatevoidbtnConfirmedPlayersToday_Click(object sender, EventArgs e)
{
foreach (string strItem in listBox1.Items)
{
PlayerName.Add(strItem); // adds player name to array list called PlayerName
}
Panel[] MyPanels = new Panel[PlayerName.Count];
for (int i = 0; i = 0; --i)
//{// this.Controls.Remove(MyPanels[i]);//}
}
privatevoidbtnClearPlayerRegion_Click(object sender, EventArgs e)
{
// does not work here but does in player confirm region above. driving me nuts.// run this to see error, typical out of bounds, etc error.for (int i = PlayerName.Count - 1; i >= 0; --i)
{
this.Controls.Remove(MyPanels[i]);
}
}
}
}
Here is designer code:
C#
namespaceHelpMePanelDelete
{
partialclassForm1
{
////// Required designer variable.///private System.ComponentModel.IContainer components = null;
////// Clean up any resources being used.////// true if managed resources should be disposed; otherwise, false.protectedoverridevoidDispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code////// Required method for Designer support - do not modify/// the contents of this method with the code editor.///privatevoidInitializeComponent()
{
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.btnConfirmedPlayersToday = new System.Windows.Forms.Button();
this.btnClearPlayerRegion = new System.Windows.Forms.Button();
this.SuspendLayout();
// // checkedListBox1// this.checkedListBox1.AllowDrop = true;
this.checkedListBox1.CheckOnClick = true;
this.checkedListBox1.FormattingEnabled = true;
this.checkedListBox1.Location = new System.Drawing.Point(138, 13);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(120, 154);
this.checkedListBox1.TabIndex = 0;
this.checkedListBox1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
// // listBox1// this.listBox1.AllowDrop = true;
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(12, 12);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 147);
this.listBox1.TabIndex = 1;
// // btnConfirmedPlayersToday// this.btnConfirmedPlayersToday.AutoSize = true;
this.btnConfirmedPlayersToday.Location = new System.Drawing.Point(265, 13);
this.btnConfirmedPlayersToday.Name = "btnConfirmedPlayersToday";
this.btnConfirmedPlayersToday.Size = new System.Drawing.Size(134, 23);
this.btnConfirmedPlayersToday.TabIndex = 2;
this.btnConfirmedPlayersToday.Text = "Confirmed Players Today";
this.btnConfirmedPlayersToday.UseVisualStyleBackColor = true;
this.btnConfirmedPlayersToday.Click += new System.EventHandler(this.btnConfirmedPlayersToday_Click);
// // btnClearPlayerRegion// this.btnClearPlayerRegion.AutoSize = true;
this.btnClearPlayerRegion.Location = new System.Drawing.Point(265, 43);
this.btnClearPlayerRegion.Name = "btnClearPlayerRegion";
this.btnClearPlayerRegion.Size = new System.Drawing.Size(110, 23);
this.btnClearPlayerRegion.TabIndex = 3;
this.btnClearPlayerRegion.Text = "Clear Player Region";
this.btnClearPlayerRegion.UseVisualStyleBackColor = true;
this.btnClearPlayerRegion.Click += new System.EventHandler(this.btnClearPlayerRegion_Click);
// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1076, 966);
this.Controls.Add(this.btnClearPlayerRegion);
this.Controls.Add(this.btnConfirmedPlayersToday);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.checkedListBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregionprivate System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button btnConfirmedPlayersToday;
private System.Windows.Forms.Button btnClearPlayerRegion;
}
}
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
naura paxRank #2,843 · 502 reputationPosted Jan 15, 2010, 10:46 PM EST
0
hi,
you would need to remove this line of code
Panel[] MyPanels = new Panel[PlayerName.Count];//change MyPanels to something else to avoid name clash.
from btnConfirmedPlayersToday_Click event and put it along with the declaration of
public List MyPanels = new List(); or anywhere within Form class scope
Problem: The Panel array goes out of scope as soon as code leaves the button click event.
hope I'm right cause I have not tested it :)
theLizardRank #1,079 · 1.8K reputationPosted Jan 16, 2010, 10:51 AM EST
0
Hello Tim,
From what I see there is nothing wrong with your code.
Rather than a List why not deal with an Array of Panels, yes a list gives you pretty much the same but an array is simpler for in your case and simplicity shou8ld always be the aim.
public partial class fm : Form { Panel[] p = new Panel[0]; public fm() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { int left = 10; for(int i = 0; i < 5; i++) // 5 just here for example { Array.Resize(ref p, p.Length+1); //increase size of array dynamically, same as Add method of list.
//this deletes the panels, notice it does NOT need a for loop. //Do this until there are no more panels left. // In this case you are directly deleting the panels not Controls elements which must be faster since you are not looping through ALL the Controls on your form.
PS. if you wanted to delete a particular panel, you could set the Tag property to be the same as the list index of the player and remove that array element, but you would need to manage the array by deleting the element from the array using a temp array like this;
Panel[] temp = new Panel[p.Length];
for(int i = 0; i < p.Length; i++) { if(p[i].Tag != listSelectedIndex) { Array.Resize(ref temp, temp.Length+1); //as long as the index is not the the one we want temp[temp.Length-1] = p[i]; //add it to the temp array. }
naura paxRank #2,843 · 502 reputationPosted Jan 15, 2010, 10:46 PM EST
you would need to remove this line of code
Panel[] MyPanels = new Panel[PlayerName.Count];//change MyPanels to something else to avoid name clash.
from btnConfirmedPlayersToday_Click event and put it along with the declaration of
public List
or anywhere within Form class scope
Problem: The Panel array goes out of scope as soon as code leaves the button click event.
hope I'm right cause I have not tested it :)
Tim Camper10 reputationPosted Jan 16, 2010, 7:11 AM EST
Tim
theLizardRank #1,079 · 1.8K reputationPosted Jan 16, 2010, 10:51 AM EST
From what I see there is nothing wrong with your code.
Rather than a List why not deal with an Array of Panels, yes a list gives you pretty much the same but an array is simpler for in your case and simplicity shou8ld always be the aim.
public partial class fm : Form
{
Panel[] p = new Panel[0];
public fm()
{
InitializeComponent();
}
private void fm_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int left = 10;
for(int i = 0; i < 5; i++) // 5 just here for example
{
Array.Resize(ref p, p.Length+1); //increase size of array dynamically, same as Add method of list.
p[p.Length-1] = new Panel();
p[p.Length-1].Parent = this;
p[p.Length-1].BorderStyle = BorderStyle.Fixed3D;
p[p.Length-1].Width = 200;
p[p.Length-1].Height = 200;
p[p.Length-1].Left = left;
left += p[p.Length-1].Width + 10;
}
}
private void button2_Click(object sender, EventArgs e)
{
//this deletes the panels, notice it does NOT need a for loop.
//Do this until there are no more panels left.
// In this case you are directly deleting the panels not Controls elements which must be faster since you are not looping through ALL the Controls on your form.
while(p.Length > 0)
{
p[p.Length-1].Dispose();
Array.Resize(ref p, p.Length-1);
}
}
}
This is how I would do things.
PS. if you wanted to delete a particular panel, you could set the Tag property to be the same as the list index of the player and remove that array element, but you would need to manage the array by deleting the element from the array using a temp array like this;
Panel[] temp = new Panel[p.Length];
for(int i = 0; i < p.Length; i++)
{
if(p[i].Tag != listSelectedIndex)
{
Array.Resize(ref temp, temp.Length+1); //as long as the index is not the the one we want
temp[temp.Length-1] = p[i]; //add it to the temp array.
}
p = temp;
This should work for you at first run.....