3 Replies - 15011 Views - Last Post: 10 January 2010 - 08:51 PM Rate Topic: -----

#1 timcamper   User is offline

  • New D.I.C Head

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

Create & Delete an Array of Panels - using for loop

Posted 10 January 2010 - 01:23 PM

I am able to create & delete my panels in one event.

My overall goal is to be able to delete the whole array of panels if playercount changes by using another event, button, etc.
Keep in mind part, the major part of this program is to create the right amount of panels based on playercount.

I use MessageBox.Show to stop the program so I can see the panels on the form,
then as you can see I use a reverse for loop to delete the panels.
I have played with the performance counter and get myself confused.
I am open to suggestion. This is not homework. I am hobbyist.
Thanks - Tim

		public void btnConfirmedPlayersToday_Click(object sender, EventArgs e)
		{
			long count = 0;

			List<string> PlayerName = new List<string>();
			listBoxPlayerArrayList.Items.Clear();
			listBox2.Items.Clear();
			tabControl1.ResetText();

			foreach (string strItem in listBox1.Items)
			{
				PlayerName.Add(strItem); // adds player name to array list called PlayerName
				//MessageBox.Show("Player Count is: " + PlayerName.Count);
			}

			Panel[] MyPanels = new Panel[PlayerName.Count];
			//MessageBox.Show("PlayerName Count is: " + PlayerName.Count);

			//performanceCounter1.Increment();
			//MessageBox.Show("performanceCounter1 Value: " + performanceCounter1.NextValue());

			if (count == 0)
			{
				for (int i = 0; i < PlayerName.Count; i++)
				{
					//instantiate new MyPanels 
					MyPanels[i] = new Panel();
					MyPanels[i].Location = new Point(56, 680 + 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]);

					//performanceCounter1.Increment();
					//////MessageBox.Show("performanceCounter1 Value: " + performanceCounter1.NextValue());
					//count = performanceCounter1.RawValue * 10;
					//////MessageBox.Show("count = " + count);
				}

				MessageBox.Show("did the panel show?" + PlayerName.Count); // use to stop program before next reverse loop below

			// reverse for loop; to delete panels (player regions)that were previously confirmed
			if (PlayerName.Count > 1)
			{
				for (int i = PlayerName.Count - 1; i >= 0; --i)
				{
					this.Controls.Remove(MyPanels[i]);
					//MessageBox.Show("Removing MyPanels: " + MyPanels[i]);
				}

			}
}



Is This A Good Question/Topic? 0
  • +

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

#2 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Create & Delete an Array of Panels - using for loop

Posted 10 January 2010 - 02:28 PM

Just make your MyPanels array a global variable so it is accessible from various methods and events.
Was This Post Helpful? 0
  • +
  • -

#3 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Create & Delete an Array of Panels - using for loop

Posted 10 January 2010 - 02:50 PM

You could do something like this:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PanelsApp
{
	public partial class Form1 : Form
	{
		private List<string> PlayerName = new List<string>();
		private List<Panel> Panels = new List<Panel>();

		public Form1()
		{
			InitializeComponent();
		}

		private void btnConfirmedPlayersToday_Click(object sender, EventArgs e)
		{
			//Remove previous panels
			foreach (Panel panel in Panels)
			{
				this.Controls.Remove(panel);
			}
			//Clear players list
			PlayerName.Clear();
			//Clear panels list
			Panels.Clear();

			foreach (string name in playersListbox.Items)
			{
				PlayerName.Add(name);
			}

			for (int i = 0; i < PlayerName.Count; i++)
			{
				Panel pnl = new Panel();
				pnl.Location = new Point(300, 10 + i * 75);
				pnl.Size = new Size(300, 50);
				pnl.BackColor = Color.BlanchedAlmond;
				pnl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

				Panels.Add(pnl);
				this.Controls.Add(pnl);
			}
		}

		//Test method to check the amount of panels that form contains
		private void button1_Click(object sender, EventArgs e)
		{
			int cnt = 0;
			foreach (Control c in this.Controls)
			{
				if (c is Panel)
					cnt++;
			}
			MessageBox.Show("Number of panels: " + cnt.ToString());
		}

		//Remove the panels
		private void btnRemovePanels_Click(object sender, EventArgs e)
		{
			foreach (Panel pnl in Panels)
			{
				this.Controls.Remove(pnl);
			}
			Panels.Clear();
		}
	}
}




Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 timcamper   User is offline

  • New D.I.C Head

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

Re: Create & Delete an Array of Panels - using for loop

Posted 10 January 2010 - 08:51 PM

View PostFlashM, on 10 Jan, 2010 - 01:50 PM, said:

You could do something like this:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PanelsApp
{
	public partial class Form1 : Form
	{
		private List<string> PlayerName = new List<string>();
		private List<Panel> Panels = new List<Panel>();

		public Form1()
		{
			InitializeComponent();
		}

		private void btnConfirmedPlayersToday_Click(object sender, EventArgs e)
		{
			//Remove previous panels
			foreach (Panel panel in Panels)
			{
				this.Controls.Remove(panel);
			}
			//Clear players list
			PlayerName.Clear();
			//Clear panels list
			Panels.Clear();

			foreach (string name in playersListbox.Items)
			{
				PlayerName.Add(name);
			}

			for (int i = 0; i < PlayerName.Count; i++)
			{
				Panel pnl = new Panel();
				pnl.Location = new Point(300, 10 + i * 75);
				pnl.Size = new Size(300, 50);
				pnl.BackColor = Color.BlanchedAlmond;
				pnl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

				Panels.Add(pnl);
				this.Controls.Add(pnl);
			}
		}

		//Test method to check the amount of panels that form contains
		private void button1_Click(object sender, EventArgs e)
		{
			int cnt = 0;
			foreach (Control c in this.Controls)
			{
				if (c is Panel)
					cnt++;
			}
			MessageBox.Show("Number of panels: " + cnt.ToString());
		}

		//Remove the panels
		private void btnRemovePanels_Click(object sender, EventArgs e)
		{
			foreach (Panel pnl in Panels)
			{
				this.Controls.Remove(pnl);
			}
			Panels.Clear();
		}
	}
}




Hope this helps.



Thanks, I will try this approach and see what happens.
Tim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1