I have got the below code to generate labels using for loop, however there is a problem with the for loop, if the productList has 4 items, it generates 1 label instead of 4. I can't figure out what the problem is.
List<models.Car> carList = carController.getCars();
for (int i = 0; i < carList.Count; i++)
{
List<models.Product> productList = productController.getProducts(carList[i].Model);
for (int j = 0; j < productList.Count; j++)
{
productLabels.Add(new Label());
var productLabelsPoint = new System.Drawing.Point(200, 40 + i * 50);
(productLabels[j] as Label).Location = productLabelsPoint;
(productLabels[j] as Label).Size = new System.Drawing.Size(150, 15);
(productLabels[j] as Label).Text = productList[j].Title;
this.Tab.TabPages["tab1"].Controls.Add((productLabels[j] as Label));
}
}
This only relies on i, not on j:
System.Drawing.Point productLabelsPoint = new System.Drawing.Point(200, 40 + i * 50);
So you might be drawing the labels one on top of the other.
In which case, you'd need to add j into the mix, for example like this:
System.Drawing.Point productLabelsPoint = new System.Drawing.Point(200, 40 + i * 50 + j * 50);
I would also change the way you are referencing the label. (I can't tell from the context if what you are doing is okay or not, as it depends how that productLabels variables has been instantiated.)
Related
private Button[,] arrButton = new Button[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
arrButton[i, j] = new Button();//getting System.IndexOutOfRangeException
arrButton[i, j].Size = new Size(30, 30);
arrButton[i, j].Location = new Point(i * 30, j * 30);
arrButton[i, j].Click += new EventHandler(arrButton_Click);
this.Controls.Add(arrButton[i, j]);
}
}
this.ClientSize = new Size(300, 300);
After create, array button come up top left corner on form.
how to get array button to a place on form
#choz comment is correct, but a couple things to also consider.
Put the row value and column value in constant variables and use the variables, instead of directly using a number repeatedly.
Example:
private const ROW = 10;
private const COL = 10;
private Button[,] arrButton = new Button[ROW, COL];
...
for (int i = 0; i < ROW; i++)
{
// Change from your code -|
// |----------
// V
for (int j = 0; j < COL; j++)
{
// Create your buttons
}
}
If you are going to use constant numerals, then reference the GetUpperBound() from your array to know the last index value of each dimension.
Example:
private Button[,] arrButton = new Button[10, 10];
...
// GetUpperBound(0) = last index of rows (9 in this case)
for (int i = 0; i <= arrButton.GetUpperBound(0); i++)
{
// Change from your code -|
// |----------
// V GetUpperBound(1) = last index of columns (9 in this case)
for (int j = 0; j <= arrButton.GetUpperBound(1); j++)
{
// Create your buttons
}
}
Syntax error! Your second loop should be:
for (int j = 0; j < 10; j++)
You put "i" instead of "j",
Normally, when you get an System.IndexOutOfRangeException. The debugger error is telling you that the loop or loops you are using are counting the elements more than you specific. Check The count of elements you tinkering with.
I think you are making a mistake in inner loop. Can you please paste this and try to run:
private Button[,] arrButton = new Button[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)//Changed i to j
{
arrButton[i, j] = new Button();
arrButton[i, j].Size = new Size(30, 30);
arrButton[i, j].Location = new Point(i * 30, j * 30);
arrButton[i, j].Click += new EventHandler(arrButton_Click);
this.Controls.Add(arrButton[i, j]);
}
}
this.ClientSize = new Size(300, 300);
This sounds wierd but i have no idea how to do this, despite the fact i've read lots of articles and answers about it.
this one seems to be the most clear, but unfortunately it doesn't help me.
Like this
so i have an array of labels that should be updated during the for cycles:
public const int NUM_OF_PAGES = 128;
...
int[] PEcnt = new int[NUM_OF_PAGES];
Label[] PElabels = new Label[NUM_OF_PAGES];
for (int i = 0; i < NUM_OF_PAGES; i++) // initialization
{
PEcnt[i] = 0;
PElabels[i] = new Label();
PElabels[i].Content = 0;
PElabels[i].Margin = new Thickness(50 + 80 * (i % 16), 50 + 20 * (i / 16), 0, 0);
Grid1.Children.Add(PElabels[i]);
}
...
for (int i = 0; i < NUM_OF_PAGES; i++)
{
PEcnt[i] += 2;
PElabels[i].Content = PEcnt[i];
BindingOperations.GetBindingExpressionBase(PElabels[i], Label.ContentProperty).UpdateTarget(); //trying to update
}
but this throws me "System.NullReferenceException"
hope u will help me, learning is difficult, but im trying hard
don't .UpdateTarget(); and it should be okay I believe
you don't need to get binding expressions for this, if you are using it else where do not update target here.
so first of all:
The Label.Content Property stores an object! So you can store an Integer in it without casting!
You are trying to update an BindingExpression on an NOTbinded Property!
So the way you should solve your problem, is by just NOT updating it.
Because your Label isn't using any bindings your called function returns null.
So just Store your Integer in the content property and YOLO. Here is the working code:
public const int NUM_OF_PAGES = 128;
Label[] PElabels = new Label[NUM_OF_PAGES];
...
for (int i = 0; i < NUM_OF_PAGES; i++) // initialization
{
PElabels[i] = new Label();
PElabels[i].Content = 0; //Storing your Data as int in object
PElabels[i].Margin = new Thickness(50 + 80 * (i % 16), 50 + 20 * (i / 16), 0, 0);
Grid1.Children.Add(PElabels[i]);
}
...
for (int i = 0; i < NUM_OF_PAGES; i++)
{
PElabels[i].Content = (int)PElabels[i].Content + 2; //Storing your new Data
}
SO!!!!
If you realy need to use this update binding, you have to set your content property to an Binding. To do so, you have to store your data in some property for example in the Label.DataContext. Now you need to set the Binding (pointing at the DataContext) to you Label.ContentProperty. After doing that you can update your BindingExpression and the Label will change.
Working Code here:
public const int NUM_OF_PAGES = 128;
Label[] PElabels = new Label[NUM_OF_PAGES];
...
for (int i = 0; i < NUM_OF_PAGES; i++) // initialization
{
PEcnt[i] = 0;
PElabels[i] = new Label();
PElabels[i].DataContext = 0; //Storing your Data
Binding PEcntBind = new Binding("DataContext");
PEcntBind.Source = PElabels[i];
PElabels[i].SetBinding(Label.ContentProperty, PEcntBind); //Binding to your stored Data
PElabels[i].Margin = new Thickness(50 + 80 * (i % 16), 50 + 20 * (i / 16), 0, 0);
Grid1.Children.Add(PElabels[i]);
}
...
for (int i = 0; i < NUM_OF_PAGES; i++)
{
PEcnt[i] += 2;
PElabels[i].DataContext = PEcnt[i]; //Storing your new Data
BindingExpression LabBindEx = (PElabels[i] as FrameworkElement).GetBindingExpression(Label.ContentProperty);
LabBindEx.UpdateTarget(); //Updating the Bindings
}
(Oh and by the way you have to use GetBindingExpression not GetBindingExpressionBase)
I am using code as below to create a list of link labels :
LinkLabel[] lnkArray = new LinkLabel[10];
for (int i = 0; i < 10; i++)
{
lnkArray[i] = new LinkLabel();
lnkArray[i].Text = "test" + i;
lnkArray[i].Location = new System.Drawing.Point(20 + (i + 5), 50);
lnkArray[i].Size = new Size(200, 25);
}
panel1.Controls.AddRange(lnkArray);
Here is a image of the result :
It looks good to me but this always makes one linklabel in the panel with text = test0 .So basically it is adding just the first one in the list any solution ?
There is no problem with AddRange.
The problem in your code is that the LinkLabel(s) is overlapping.
The width of the LinkLabel in your code is 200. Therefore, you should leave at least 200px gap between the labels.
Try changing your code to this:-
LinkLabel[] lnkArray = new LinkLabel[10];
for (int i = 0; i < 10; i++)
{
lnkArray[i] = new LinkLabel();
lnkArray[i].Text = "test" + i;
lnkArray[i].Location = new System.Drawing.Point(20 + (i + 200), 50);
lnkArray[i].Size = new Size(200, 25);
}
panel1.Controls.AddRange(lnkArray);
simply use this instead of array
for (int i = 0; i < 10; i++)
{
LinkLabel lnkLbl = new LinkLabel();
// add properties i.e Text , Location , size
panel1.Controls.Add(lnlLbl);
}
Im trying to create a chess strategy application in c#. I have placed the panels in the form designer where they have been named panel1, panel2, ect.... I am needing to know how I can assign the panels to a 2D Array like 'chessBoardPanels[0,0]' this would allow me to actually control the backgrounds of the panels with a command like:
chessBoardPanels[0,0].Background=Color.Black;
But it says I need a some sort of object reference.
I would rather go for something like this
int numBlocks = 8;
Panel[,] chessBoardPanels = new Panel[numBlocks, numBlocks];
for (int iRow = 0; iRow < numBlocks; iRow++)
for (int iColumn = 0; iColumn < numBlocks; iColumn++)
{
Panel p = new Panel();
//set size
p.Size = new Size(50, 50);
//set back colour
p.BackColor = (iRow + (iColumn % 2)) % 2 == 0 ? Color.Black : Color.White;
//set location
p.Location = new Point(50 * iRow, 50 * iColumn);
chessBoardPanels[iRow, iColumn] = p;
this.Controls.Add(p);
}
This would allow you to create the Panels on the fly, without having to create them in the designer.
You will however have to work on a formula to handle the spacing for you.
EDIT
I have also added an example of how to space/set the panel blocks.
The syntax for creating such 2D array would be:
Panel[,] chessBoardPanels = new Panel[8, 8];
chessBoardPanels[0, 0] = panel1;
chessBoardPanels[0, 1] = panel2;
chessBoardPanels[0, 2] = panel3;
//...
chessBoardPanels[0, 7] = panel8;
chessBoardPanels[1, 0] = panel9;
//...
I kept a bool in my loop for the creation of my board to determine whether to use black or white as the background color. Before setting each field you must initiate the array (usually a two-dimensional one):
this.Fields = new PieceButton[Board.Size, Board.Size];
board = new Board(this);
for (int i = 0; i < Board.Size; i++)
{
for (int j = 0; j < Board.Size; j++)
{
this.Fields[i, j] = new PieceButton(even ? white : black, i, j);
this.Fields[i, j].Size = fieldSize;
this.Fields[i, j].Location = new Point(
i * PieceSize + widthOffset,
(Board.Size - j - 1) * PieceSize + heightOffset);
Fields[i, j].MouseDown += this.Piece_MouseDown;
this.Controls.Add(Fields[i, j]);
even = !even;
}
even = !even;
}
Here is the code I'm using
It's supposed to create labels for each module name entered by a user and all the assessment names for that module under it. Should work with any number of modules and assessments. The problem is that it only shows assessments for the last module displayed.
dat.Modules and dat.Assessments are arraylists, each of them holds 4 elements with info about a module or an assessments, that is why i divide the count by 4.
private void testingButton_Click(object sender, EventArgs e)
{
int pos1 = 50;
int pos2 = 150;
int modLength = dat.Modules.Count;
modLength = modLength / 4;
int assessLength = 0;
int arrayData = 0;
int displayCount = 0;
for (int i = 0; i < modLength; i++)
{
this.moduleLabels.Add(new Label());
System.Drawing.Point pLabel1 = new System.Drawing.Point(50, pos1);
(moduleLabels[i] as Label).Location = pLabel1;
(moduleLabels[i] as Label).Size = new System.Drawing.Size(100, 13);
(moduleLabels[i] as Label).Text = dat.Modules[arrayData].ToString();
tabPage5.Controls.Add((moduleLabels[i] as Label));
String asd = dat.Modules[arrayData + 2].ToString();
Console.WriteLine(asd);
assessLength = int.Parse(asd);
pos2 = pos1 + 25;
for (int y = 0; y < assessLength; y++)
{
this.assessLabels.Add(new Label());
System.Drawing.Point pLabel2 = new System.Drawing.Point(70, pos2);
(assessLabels[y] as Label).Location = pLabel2;
(assessLabels[y] as Label).Size = new System.Drawing.Size(250, 13);
(assessLabels[y] as Label).Text = dat.Assessments[displayCount + 1].ToString() + " weights " + dat.Assessments[displayCount+2].ToString() +"%, Enter your mark:";
textboxComputer.Add(new TextBox());
System.Drawing.Point pText1 = new System.Drawing.Point(400, pos2);
(textboxComputer[y] as TextBox).Location = pText1;
(textboxComputer[y] as TextBox).Size = new System.Drawing.Size(20, 20);
tabPage5.Controls.Add(assessLabels[y] as Label);
tabPage5.Controls.Add(textboxComputer[y] as TextBox);
pos2 = pos2 + 25;
displayCount = displayCount + 4;
}
pos1 = pos2+25;
arrayData = arrayData + 4;
}
}
this is an example of what it displays
http://dc540.4shared.com/download/YI8IENYI/tsid20120501-211723-cbc785f9/asd.jpg
The first two modules should have their assessments listed. The first one doesn't display any. For Java it only displays the last one, out of 3 total for that module. And for the last Module "Another Module" it displays all assessments.
For each increment of i, y starts at 0. You then add new labels to assessLabels, but attempt to access the one you added by using assessLabels[y] which would usually yield the labels created for the previous value of i. This causes labels created for the first module to be reused by the next, and so forth.
A quick solution is not to use assessLabels[y] but assessLabels[assessLabels.Count - 1].
A better solution is to create a local variable for the labels, set their properties, and then add them to the list:
for (int y = 0; y < assessLength; y++)
{
Label assessLabel = new Label();
assessLabel.Location = ...;
// etc.
tabPage5.Controls.Add(assessLabel);
assessLabels.Add(assessLabel);
}
This would also remove the need to continuously cast the ArrayList members and unneeded access to the list.
PS. If assessLabels only contains objects of type Labels, consider using a List<Label> instead.