6 Replies - 681 Views - Last Post: 15 January 2010 - 09:25 PM Rate Topic: -----

#1 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Delete an Array of Panels - using for loop

Posted 15 January 2010 - 04:12 PM

I am looking for more assistance with this code.
My last post was Jan. 10, but suggestion did not work.

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.
			for (int i = PlayerName.Count - 1; i >= 0; --i)
			{
				this.Controls.Remove(MyPanels[i]);
			}

		}


		}

		}




Is This A Good Question/Topic? 0
  • +

Replies To: Delete an Array of Panels - using for loop

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Delete an Array of Panels - using for loop

Posted 15 January 2010 - 04:25 PM

try i-- instead of --i in your loop.

also im going to need the designer code if you want me to test the panels
Was This Post Helpful? 0
  • +
  • -

#3 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Re: Delete an Array of Panels - using for loop

Posted 15 January 2010 - 05:03 PM

Here is the designer code:

The i-- did not work.

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;
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Delete an Array of Panels - using for loop

Posted 15 January 2010 - 06:14 PM

im sorry not quite sure what you want to fix, because your add doesnt really work properly either. do you need help resizing the form, or do you need help deleting the panels you create?

This post has been edited by SwiftStriker00: 15 January 2010 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Re: Delete an Array of Panels - using for loop

Posted 15 January 2010 - 06:39 PM

View PostSwiftStriker00, on 15 Jan, 2010 - 05:14 PM, said:

im sorry not quite sure what you want to fix, because your add doesnt really work properly either. do you need help resizing the form, or do you need help deleting the panels you create?



Looking for help deleting the panels I create.

Best I can tell, creating the panels works fine.
I get the same number of panels as items selected in the checkedlistbox.
So that design criteria works.
I do need to come up with a way to clear selections each time the btnConfirmedPlayersToday is clicked.
But my focus is on using btnClearPlayerRegion of the code to remove the panels.
Kind of like doing a reversal.

Tim
Was This Post Helpful? 0
  • +
  • -

#6 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Delete an Array of Panels - using for loop

Posted 15 January 2010 - 07:06 PM

well your issue is that the Panel[] MyPanels that you create, will leave the scope at the end of the create button method. what you need to do is move the declaration out to the class scope, then you can easily modify them from the delete method.

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 DIC_HELP {
	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 Panel[] MyPanels = new Panel[0];

		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
			}

			MyPanels = new List<Panel>();

			for( int i = 0; i < PlayerName.Count; i++ ) {
				//instantiate new MyPanels
				Panel p = new Panel();
				p = new Panel();
				p.Location = new Point(56, 250 + i * 75);
				//200 + i * 30, 12);
				p.Size = new Size(300, 50);
				p.BackColor = Color.BlanchedAlmond;
				p.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
				MyPanels.Add(p);
				this.Controls.Add(p);
			}

			// 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.
			for( int i = 0; i < 0; i++ ) {
				MyPanels[i].Dispose();
			}
			MyPanels.Clear();
			listBox1.Items.Clear();// = MyPanels;
		}


	}

}



Also I said your add button didnt work, because try clicking on confirmed players a couple times, you should see the error
Was This Post Helpful? 0
  • +
  • -

#7 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Re: Delete an Array of Panels - using for loop

Posted 15 January 2010 - 09:25 PM

View PostSwiftStriker00, on 15 Jan, 2010 - 06:06 PM, said:

well your issue is that the Panel[] MyPanels that you create, will leave the scope at the end of the create button method. what you need to do is move the declaration out to the class scope, then you can easily modify them from the delete method.

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 DIC_HELP {
	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 Panel[] MyPanels = new Panel[0];

		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
			}

			MyPanels = new List<Panel>();

			for( int i = 0; i < PlayerName.Count; i++ ) {
				//instantiate new MyPanels
				Panel p = new Panel();
				p = new Panel();
				p.Location = new Point(56, 250 + i * 75);
				//200 + i * 30, 12);
				p.Size = new Size(300, 50);
				p.BackColor = Color.BlanchedAlmond;
				p.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
				MyPanels.Add(p);
				this.Controls.Add(p);
			}

			// 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.
			for( int i = 0; i < 0; i++ ) {
				MyPanels[i].Dispose();
			}
			MyPanels.Clear();
			listBox1.Items.Clear();// = MyPanels;
		}


	}

}



Also I said your add button didnt work, because try clicking on confirmed players a couple times, you should see the error



Thanks so much.
My goal was to actually remove the newly created panels in the button clear region,
logic being user confirms number of player, panels get created and then user says, oops, I need t add another or delete someone.
Thought would be easier to have button to remove all panels, then using checkedlistbox, check or uncheck to confirm once again.

So in the button clear region I changed the code...

from: listBox1.Items.Clear();// = MyPanels;
to: this.Controls.Remove(MyPanels[i]);

Now I will work on the add button to fix the problem from click over once. Like I need to check for any checked items and clear them before the current for loop.
If you have thoughts toward that, feel free to share them.

Again, thanks - Tim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1