Memduh Cüneyt Memduh Cüneyt -4 years ago 107
C# Question

Getting wrong text in a label array in c#

I created a control array containing labels, but there are wrongs in the text of the third row in the array, I couldn't find a logical reason for this.
the code is as follow

Label[,] array_lbl = new Label[16, 7];


for (int w = 0; w < array_lbl.Length; w++)
{
for (int w1 = 0; w1 < 7; w1++)
{

var labelToRemove = this.panel1.Controls["lbl" + w1.ToString() + w.ToString()];
this.panel1.Controls.Remove(labelToRemove);

}



}

for (int i = 0; i < 16; i++)
{
for (int i1 = 0; i1 < 7; i1++)
{


array_lbl[i, i1] = new Label();
if (i1 == 0) { array_lbl[i, i1].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(123)))), ((int)(((byte)(185)))), ((int)(((byte)(40))))); }
else { array_lbl[i, i1].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(53)))), ((int)(((byte)(86)))), ((int)(((byte)(100))))); }
if (i1 == 6) { array_lbl[i, i1].Size = new System.Drawing.Size(352, 32); }
else { array_lbl[i, i1].Size = new System.Drawing.Size(191, 32); }
array_lbl[i, i1].Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(i)));
array_lbl[i, i1].ForeColor = System.Drawing.Color.LightCyan;
array_lbl[i, i1].Location = new System.Drawing.Point(22 + 205 * i1, 60 + i * 46);
array_lbl[i, i1].Text = "";
array_lbl[i, i1].TabIndex = 6;
array_lbl[i, i1].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//array_lbl[i, i1].Cursor = Cursors.Hand;
array_lbl[i, i1].Name = "lbl" + i.ToString() + i1.ToString();
//array_lbl[i, i1].Click += new System.EventHandler(this.lbl_Bant_1_name_Click);
this.panel1.Controls.Add(array_lbl[i, i1]);
array_lbl[i, i1].Text = array_lbl[i, i1].Name;


}

}


The result that I got it as following:enter image description here

Answer Source

You are setting the GdiCharSet to symbols:

array_lbl[i, i1].Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(i)))

In the last argument ((byte)(i)) when i is 2, it will set the GdiCharSet to symbols. You should just simply set 0 (ANSI) or 1 (DEFAULT).

Reference: https://msdn.microsoft.com/en-us/library/system.drawing.font.gdicharset(v=vs.110).aspx

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download