tablelayoutpanel within a tablelayoutpanel disposal - c#

I have a tableLayoutPanel called tlpMaster. It has 4 columns and 8 rows. in 3 of the columns and each row, it has another tableLayoutPanel. so thats 3 inner ones. Inside those I have checkboxes that I need to be able to dispose. Here is what I got so far but it seems to be disposing the tableLayoutPanels and not the checkboxes.
for (int i = 0; i < 8; i++)
{
for (int j = 1; j < 4; j++)
{
//loop throught the table layout panels and dispose
Control tlpTemp = tlpMaster.GetControlFromPosition(j, i);
while (tlpTemp.Controls.Count > 0)
{
tlpTemp.Controls[0].Dispose();
}
}
}
What am I doing wrong here?

Figured it out
for (int i = 0; i < 8; i++)
{
for (int j = 1; j < 4; j++)
{
//loop throught the table layout panels and dispose
Control tlpTemp = tlpMaster.GetControlFromPosition(j, i);
foreach(Control ctrl in tlpTemp.Controls)
{
while (ctrl.Controls.Count > 0)
{
ctrl.Controls[0].Dispose();
}
}
}
}

Related

How to rewrite if statement to stop once .Count method has reached a number instead of continuously creating

I am creating a brick breaker clone and need to stop creating blocks after 52 of them are created. The current situation i'm in once one is destroyed it creates more and actually goes into the 60's.
my Block code in the Update() section of Game 1:
if (blocks.Count < 52)
{
for (int j = 0; j < 12; j++)
{
blockXpos += (61);
blocks.Add(new Blocks(Content.Load<Texture2D>("Blocks\\DurasteelBlock"), blockXpos, blockYpos));
if (j == 11)
{
for (int i = 0; i < 1; i++)
{
blockXpos = 3;
blocks.Add(new Blocks(Content.Load<Texture2D>("Blocks\\DurasteelBlock"), blockXpos, blockYpos));
blockYpos += (51);
if (i == 1)
{
j = 0;
}
}
}
}
}
The next bit of code is located directly below the above and is supposed to remove 1 from the .Count:
for (int j = 0; j < blocks.Count; j++)
{
if (blocks[j].alive == false)
{
blocks.RemoveAt(j);
break;
}
}
for (int i = 0; i < blocks.Count; i++)
{
if (blocks[i].alive == false)
{
blocks.RemoveAt(i);
break;
}
}
My question is how do I change the very first line into basically saying 'stop after 52' instead of 'continuously create if count is less than 52'.
After each blocks.Add you'd have to check blocks.Count and break out of the loop if it's >=52.
//remove the if before the for loop
for (int j = 0; j < 12; j++)
{
if (blocks.Count >= 52)
{
break; //exit the for loop
}
blockXpos += (61);
blocks.Add(new Blocks(Content.Load<Texture2D>("Blocks\\DurasteelBlock"), blockXpos, blockYpos));
if (j == 11)
{
for (int i = 0; i < 1; i++)
{
blockXpos = 3;
blocks.Add(new Blocks(Content.Load<Texture2D>("Blocks\\DurasteelBlock"), blockXpos, blockYpos));
blockYpos += (51);
if (i == 1)
{
j = 0;
}
}
}
}
See break (MSDN).
I managed to put it into the LoadContent Section Instead Problem is solved now.

Unsort a listbox in c# winform

I am very new programmer, the user is able to input vales into a ListBox, there is also a option button where the user can sort the numbers in ascending order. However I am also asked to create a option button to unsort back into its original form, however I am not sure how I would do this. I am trying to do this without the use of any containers/arrays. here is my code to sort:
private void sorted()
{
int a = lstHoldValue.Items.Count;
for (int i = 0; i < a - 1; i++)
{
var k = 0;
for (var j = 1; j < a - i; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[j]) < Convert.ToInt32(lstHoldValue.Items[k]))
{
var temp = lstHoldValue.Items[j];
lstHoldValue.Items[j] = lstHoldValue.Items[k];
lstHoldValue.Items[k] = temp;
k = j;
}
else
{
k++;
}
}
}
}

Getting System.ArgumentOutOfRangeException while copy between two Datagridviews

If somebody could please help solve the following issue:
System.ArgumentOutOfRangeException : Index was out of range. Must be
non-negative and less than the size of the collection. Paarameter name
: index
The code:
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
{
for (int j = 0; j <= dataGridView3.Rows.Count; j++)
{
if (!string.IsNullOrEmpty(dataGridView2.Rows[i].Cells["supplier_name"].Value as string) && !string.IsNullOrEmpty(dataGridView3.Rows[j].Cells["brands_supplier"].Value as string))
{
if (dataGridView2.Rows[i].Cells["supplier_name"].Value.ToString() == dataGridView3.Rows[j].Cells["brands_supplier"].Value.ToString())
{
dataGridView2.Rows[i].Cells["brands_name"].Value += dataGridView3.Rows[j].Cells["brands_nume"].Value + " ";
}
}
else
{
break;
}
}
}
You try to access an element of your datagrid which doesn't exist.
You have to set your for condition to
for (int i = 0; i < dataGridView2.Rows.Count ; i++)
and
for (int j = 0; j < dataGridView3.Rows.Count; j++)
don't use <= because the index of dataGridView.Rows[] is 0 based.
For example, if your datagrid contains 3 elements you can reach them with:
var row1 = dataGrid.Rows[0]
var row2 = dataGrid.Rows[1]
var row3 = dataGrid.Rows[2]
And you try to access
var row4 = dataGrid.Rows[3] // Error because this item doesn't exist (System.ArgumentOutOfRangeException)
also, but this item doesn't exist
I think problem in cycle
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
You run one more time
use
for (int i = 0; i < dataGridView2.Rows.Count ; i++)

C# - Initialize multidimensional array [,] Using a For-Loop to make 100 * 100 int grid

I am wanting to create a 2d array to produce a grid of int values the size of the grid needs to be 100 * 100
Example Grid
1 2 3 .........100
101 102 103.........200
201 202 303.........300
I produced ths
int rows = 100;
int columns = 100;
public int[,] InitIntArray() {
int[,] grid = new int[rows,columns];
int number = 1;
for (int i = 0; i != rows; i++)
{
for (int j = 0; j != columns; j++)
grid[i, j] = number;
number++;
}
return grid;
}
When I use my code everything seems to work fine until I try to access the columns. all I get when I use grid.getValue() is 1.
Missed one curvy
for (int i = 0; i != rows; i++)
{
for (int j = 0; j != columns; j++)
{
grid[i, j] = number;
number++;
}
}
for (int i = 0; i < rows; i++)
And the other for-loop
for (int j = 0; j < columns; j++)
{
grid[i, j] = number;
number ++;
}
You might want to read on how for-loops work, http://www.thegeekstuff.com/2012/12/c-loops-examples/

Flling a two-dimensional array with buttons

I have a litte problem here. I have a two-dimensional array which is [5,5] big.
I have a windows form with 25 buttons. Now I want to store the buttons in the object array, but my problem is, how do I tell the program to know which button to put in the array? Is it possible in some kind of this way:
//_array[i] = button(i);
This is my first time storing objects in an array and I dont know how to do that.
EDIT: The buttons have all the standard names (button1,button,button3...)
EDIT2: I know how to it by hand (_array[x,y] = button1) but I want to know how to it ith a for-loop.
You could filter the controls by using IEnumerable.OfType():
//get all buttons and order them by name
var buttons = Controls.OfType<Button>().OrderBy(x => x.Name).ToList();
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
_array[i, j] = buttons[i*5+j];
You can access the buttons using their name as index of the Controls collection:
for (int i = 0; i < 5; i++) {
for (int k = 0; k < 5; k++) {
_array[i, k] = Controls["Button" + (5 * i + k + 1).ToString()];
}
}
Try this:
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
_array[i,j] = Controls.Item["Button" + (i*5 +j).ToString()];

Categories