Tim Camper

Tim Camper

  • NA
  • 10
  • 0

Delete an Array of Panels - using for loop

Jan 15 2010 7:14 PM
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<string> PlayerName = new List<string>(); // use to decide who playing today

public List<Panel> MyPanels = new List<Panel>();

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
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}





Answers (3)