I am looking for more assistance with this code.
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.
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 HelpMePanelDelete { public partial class Form1 : Form { public List PlayerName = new List(); // use to decide who playing today
public List MyPanels = new List();
public Form1() { InitializeComponent(); }
private void Form1_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"); }
private void checkedListBox1_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()); }
}
private void btnConfirmedPlayersToday_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 < PlayerName.Count; i++) { //instantiate new MyPanels MyPanels[i] = new Panel(); MyPanels[i].Location = new Point(56, 250 + i * 75); //200 + i * 30, 12); MyPanels[i].Size = new Size(300, 50); MyPanels[i].BackColor = Color.BlanchedAlmond; MyPanels[i].BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Controls.Add(MyPanels[i]); }
// use MessageBox to stop program, to see panels created before deletion // I don't want to delete panels here, I want to delete in the, // btnClearPlayerRegion_Click event (see next event below)
MessageBox.Show("did the panel show: " + PlayerName.Count);
//// reverse for loop; to delete panels (player regions)that were previously confirmed //// for loop proofs iteration works. //for (int i = PlayerName.Count - 1; i >= 0; --i) //{ // this.Controls.Remove(MyPanels[i]); //}
}
private void btnClearPlayerRegion_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:
namespace HelpMePanelDelete { partial class Form1 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null;
/// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(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. /// private void InitializeComponent() { 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();
}
#endregion
private System.Windows.Forms.CheckedListBox checkedListBox1; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button btnConfirmedPlayersToday; private System.Windows.Forms.Button btnClearPlayerRegion; } }
|
theLizardPosted Jan 16, 2010, 3:51 PM
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.....
Tim CamperPosted Jan 16, 2010, 12:11 PM
Tim
naura paxPosted Jan 16, 2010, 3:46 AM
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 :)