using dynamically created labels c# - c#

So I have
TableLayoutPanel table = new System.Windows.Forms.TableLayoutPanel();
public int d;
private void button1_Click(object sender, EventArgs e)
{
d = (int)numericUpDown1.Value;
table.ColumnCount = d;
table.RowCount = d;
this.Controls.Add(table);
int k = 1;
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
Label lab = new System.Windows.Forms.Label();
lab.Size = new Size(50, 50);
lab.Text = (k).ToString();
lab.TabIndex = k++;
lab.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(lab, j, i);
}
table.Show();
}
That is one Button Click method.
I have Button2 Click method and I want to change lab.TabIndex or/and lab.Text.
How can I do that?
And second question:
how can i do something on Click one of that labels? Let's say that i want to change a color one of the labels i click...how can I do that?
I'm a beginner so...have mercy :)

define a array of labels (global variable, like you did with table):
Label[] labels;
in button1_Click, add the line of code:
labels = new Label[d*d]; // array of d*d labels
inside the loop, define the specific label, like:
labels[i*d+j] = new System.Windows.Forms.Label();
So your loop will look like:
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
labels[i*d+j] = new System.Windows.Forms.Label();
labels[i*d+j].Size = new Size(50, 50);
labels[i*d+j].Text = (k).ToString();
labels[i*d+j].TabIndex = k++;
labels[i*d+j].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(labels[i*d+j], j, i);
}
you can access them from other buttons also in the same way (labels[n])

Related

Creating Labels at run time from a variable

Im trying to create a label in runtime. the number of labels depends on the number of items of another variable and the labels do not show. The code is as follows.
int NoofItems = tillfrm.lvbasket.Items.Count;
for (int i = 0; i < NoofItems + 1; i++)
{
Label lblitems = new Label();
lblitems.Name = "lblItems" + i;
lblitems.Font = new Font ("Calibri",lblitems.Font.Size);
lblitems.Location = new Point(95, (152 + (19 * i)));
lblitems.ForeColor = System.Drawing.Color.Black;
lblitems.Show();
lblitems.AutoSize = true;
lblitems.Text = tillfrm.lvbasket.Items[0].Text;
this.Controls.Add(lblitems);
}
some help would be appreciated thanks.
You should change tillfrm.lvbasket.Items[0].Text to tillfrm.lvbasket.Items[i].Text.
And i < NoofItems + 1 to i < NoofItems, because array size is NoofItems.
Try it like this, create a function, make the array GLOBAL,
protected void myFunction()
{
int NoofItems = tillfrm.lvbasket.Items.Count;
for (int i = 0; i < NoofItems; i++)
{
Label lblitems = new Label();
lblitems.Name = "lblItems" + i;
lblitems.Font = new Font ("Calibri",lblitems.Font.Size);
lblitems.Location = new Point(95, (152 + (19 * i)));
lblitems.ForeColor = System.Drawing.Color.Black;
lblitems.Show();
lblitems.AutoSize = true;
lblitems.Text = tillfrm.lvbasket.Items[i].Text;
this.Controls.Add(lblitems);
}
}
Then call this function in Form_load() function or Page_load() function like this
protected void Form_Load(Object sender , EventArgs e)
{
myFunction();
}

Assign different text name in an array button using different function

I have a 2x2 button array and I want to give it a different text name.
Button[,] btnSeat = new Button[2, 2];
private void initializeBoard()
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
btnSeat[i, j] = new Button();
btnSeat[i, j].Width = 90;
btnSeat[i, j].Height = 90;
pnlSeat.Controls.Add(btnSeat[i, j]);
}
}
IT will have an output like this
How can I assign a text using different function
public void assignName()
{
//assign button names
}
Is there anything stopping you from passing the button as a parameter?
public void assignName(Button button)
{
button.Text = "ex";
}
Edit
Since your naming convention is known ahead of time, you can just use a map.
string[,] buttonNames = new string[2,2];
buttonNames[0,0] = "dog";
// continue
Then if you want to use the separate function, you would need to pass in 3 parameters.
public void assignName(Button button, int row, int column)
{
string[,] buttonNames = new string[2,2];
buttonNames[0,0] = "dog";
// continue
buttonText.Text = buttonNames[row][column];
}
Notes
I would either make the name map static at the class level and assigned in a static constructor or dispense with the function, declare the name map in the initializeBoard method and just use it in there.
For example
private void initializeBoard()
{
string[,] buttonNames = new string[2,2];
buttonNames[0,0] = "dog";
// continue
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
btnSeat[i, j] = new Button();
btnSeat[i, j].Width = 90;
btnSeat[i, j].Height = 90;
btnSeat[i, j].Text = buttonNames[row][column];
pnlSeat.Controls.Add(btnSeat[i, j]);
}
}
}
Does that help?

how to get array button to a place on form?

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

How to add a matrix of OvalShapes to the window?

I want to draw 16*64 matrix, each one containing a:
Microsoft.VisualBasic.PowerPacks.OvalShape.
I used this:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape =
new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
}
How can I show it in the Window?
Creating a separate container for each shape is not required. Also you can skip the additional container list for the shapes. So you could use this very compact code.
var ovalShapes = new Microsoft.VisualBasic.PowerPacks.ShapeContainer()
{
Dock = DockStyle.Fill,
Margin = new Padding(0),
Padding = new Padding(0),
};
for (int i = 0; i < 16; i++)
for (int j = 0; j < 64; j++)
ovalShapes.Shapes.Add(
new Microsoft.VisualBasic.PowerPacks.OvalShape()
{
Width = 20,
Height = 20,
FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid,
FillColor = Color.Green,
Location = new Point(20 * i, 20 * j),
});
this.Controls.Add(ovalShapes);
From MSDN:
An OvalShape control cannot be displayed directly on a form or
container control; it must be contained in a ShapeContainer object.
After you initialize an OvalShape, you will have to set its Parent
property either to an existing ShapeContainer or to a new instance of
ShapeContainer.
Try to set Location and add your controls to the Form:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovl.Location = new Point(ovl.Width*i, ovl.Height*j);
ovalShape.Add(ovl);
}
}
foreach(OvalShape os in ovalShape)
{
Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
Control c = new Control();
shapeContainer.Parent = c;
os.Parent = shapeContainer;
myForm.Controls.Add(c);
}
First simplify
int totalCount = 1024; //16*64
const int shapeWidth =20;
const int shapeHeight = 20;
for (int j = 0; j < totalCount; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = shapeWidth;
ovl.Height = shapeHeight;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
After pickup your drawing area and decide how much shapes per row you would like to have.
So some hypothetical code could look like this:
int shapesPerRowCount = 5;
int yPos = 0;
for(int i=0;i<ovalShape.Count;i++)
{
if(i % shapesPerRowCount == 0) //reach end of the row, so offset Y position by Height
yPos += shapeHeight;
int xPos = i*shapeWidth;
DrawShapeAtPos(ovalShape[i], xPos, yPos); //some special function that draws the shape
}
A very generic code, but just to provide you an idea.
If it's not what you're searching for, please clarify.

How to create list of checkboxes with scroll?

I'm trying this code:
arrList = new List<CheckBox>();
for (int j = 0; j < 20; j++)
{
CheckBox check = new CheckBox();
arrList.Add(check);
}
CheckBox[] cb = arrList.ToArray();
for (int i = 0; i < 20; i++)
{
cb[i].Text = "sometext";
cb[i].Location = new System.Drawing.Point(10, 15 + i * 20);
cb[i].BackColor = System.Drawing.Color.Silver;
cb[i].Name = "somename";
cb[i].Size = new System.Drawing.Size(59, 17);
cb[i].Checked = true;
groupBox1.Controls.Add(cb[i]);
}
How can I add a vertical scroll bar there? (maybe I should use something instead of groupbox?)
You could use a Panel control and set it's AutoScroll property to True

Categories