How can I make N label? - c#

This doesn't work:
static int N = word.Length;
int z = 0;
for (int k = 0; k <N; k++)
{
Label l = new Label();
l.Name = string.Format("charLabel{0}", k);
l.Text = "_";
l.Height = 75;
l.Width = 25;
l.Location = new Point(300 + z, 10);
this.Controls.Add(l);
z += 10;
}
It only creates one and I would like to create more Labels, right next to the last. How can I do this?

If you set a larger number than 10 in the line z += 10; you will see the result because it needs more space to show your content. But if you set the AutoSize property like below you will get your desired result also:
l.AutoSize = true;
By setting this property the control is automatically resized to display its entire contents.
Also this is worth to mention that this property is true by default when added to a form using the designer but not when instantiated from code. Based on MSDN:
When added to a form using the designer, the default value is true. When instantiated from code, the default value is false.

Because you assign only one character to the text property of your labels and don't change vertical margin of your labels, the labels are superpositioned.
You should change the code like this :
static int N = word.Length;
int z = 0;
for (int k = 0; k <N; k++)
{
Label l = new Label();
l.Name = string.Format("charLabel{0}", k);
l.Text = "_";
l.Height = 75;
l.Width = 25;
l.Location = new Point(300, 10 + z);
this.Controls.Add(l);
z += 50;
}

Related

My code about creating label dynamically in a loop don't work, only one appear

I'm creating an application, where for each Char in a txt file (where some text is written), a label containing one char letter from the txt is created and written.
In the txt file, "Hello" is written, and only the H appear
Here's my actual code :
string test = System.IO.File.ReadAllText(#"../../../../../Texte/Test.txt");
int x = 20;
int y = 20;
int i = 10;
foreach (char ch in test) {
Label newlabel = new Label();
newlabel.Location = new System.Drawing.Point(x + i, y);
newlabel.Text = ch.ToString();
panel1.Controls.Add(newlabel);
i += 15;
}
You should set AutoSize property of your label to TRUE. Without setting this property the first label covers the rest, so you can see only the first one. Try the following:
foreach (char ch in test) {
Label newlabel = new Label();
newlabel.Location = new System.Drawing.Point(x + i, y);
newlabel.Text = ch.ToString();
newlabel.AutoSize = true;
panel1.Controls.Add(newlabel);
i += 15;
}

Create an array of labels in C# to show up on Form1.cs

I want to create an array of 361 labels(i.e., label, label1, label2, etc..) and have them physical positioned and showing up on my Form1.cs [Design].
public Form1()
{
InitializeComponent();
Label[] board = new Label[361];
for (int i = 1; i < 362; i++)
{
board[i] = new Label { Name = "label" + i, Height = 55, Width = 55, MinimumSize = new Size(55, 55), Text = "label " + i };
}
int x = 0;
int y = 0;
foreach (var Label in board)
{
if (x >= 580)
{
x = 0;
y = y + Label.Height + 55;
}
Label.Location = new Point(x, y);
this.Controls.Add(Label);
x += Label.Width;
}
}
I get an error with the text = "label" + 1. Not sure why. Thank you for any help.
Arrays are 0-indexed in C#, your last loop iteration will go over the max index.
Label[] board = new Label[361];
this creates an array of Label with indexes 0 - 360. Simply alter your `for loop to:
for (int i = 0; i < 361; i++)
this will also fix your NullReferenceException, which you were seeing because the first item in the array was never initialized
Change the for loop from
Label[] board = new Label[361];
for (int i = 1; i < 362; i++) {
board[i] = new Label { Name = "label" + i, Height = 55, Width = 55, MinimumSize = new Size(55, 55), Text = "label " + i };
}
To
Label[] board = new Label[361];
for (int i = 0; i < 361; i++) {
board[i] = new Label { Name = "label" + i, Height = 55, Width = 55, MinimumSize = new Size(55, 55), Text = "label " + i };
}
your board array is of size 361, So max would be board[360]
in your for loop i<362.. which means it will try to get value for board[360
I modified the value of int in the loop, is this what you want?
for (int i = 0; i < 361; i++)
{
board[i] = new Label { Name = "label" + i, Height = 55, Width = 55, MinimumSize = new Size(55, 55), Text = "label " + i };
}
int x = 0;
int y = 0;
foreach (var Label in board)
{
if (x >= 580)
{
x = 0;
y = y + Label.Height + 55;
}
Label.Location = new Point(x, y);
this.Controls.Add(Label);
x += Label.Width;
}

Filling jagged array with dynamic textboxes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I made a Linear equation solver program for school, I already found an algorithm which works, but it uses jagged arrays.
I need your help to make dynamical textboxes which are filling up a jagged array. For example: 1x+1y=3 and 2x+1y=4 would go into a {1,1,3} and into a {2,1,4} array. Here is my code, which isn't working.
TextBox[][] tb = new TextBox[n][];
for (int i = 0; i < n; i++)
for (int j = 1; j < n+1; j++) {
tb[i][j] = new TextBox();
tb[i][j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
tb[i][j].Left = 36 * j + 10;
tb[i][j].Top = 36 * i + 10;
tb[i][j].Width = 35;
tb[i][j].Font = new Font(tb[i][j].Font.FontFamily, 16);
tb[i][j].BackColor = Color.Cyan;
tb[i][j].TextAlign = HorizontalAlignment.Center;
}
I created a dummy project (as shown in the picture) and added a Panel. The Panel will contain the textboxes that you generate dynamically. I also updated the code behind. Although you created the textboxes, you never added it to any of the existing controls. They merely existed.
The code behind now looks like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int numVars = 2;
// You'll eventually need to obtain the values in the boxes
TextBox[][] tb;
private void button1_Click(object sender, EventArgs e)
{
// Remove existing controls
this.panel1.Controls.Clear();
// Obtain number of variables
numVars = (int)numericUpDown1.Value;
// Create the TextBoxes
tb = new TextBox[numVars][];
// Initialize each jagged array
for(int i = 0; i < numVars; i++)
tb[i] = new TextBox[numVars + 1];
// Create the Textboxes
int height = 20;
int width = 50;
int curX = 10;
int curY = 10;
for(int i = 0; i < numVars; i++)
{
for(int j = 0; j < numVars + 1; j++)
{
TextBox txtbox = new TextBox();
txtbox = new TextBox();
txtbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
txtbox.Left = curX;
txtbox.Top = curY;
txtbox.Width = width;
txtbox.Height = height;
txtbox.Font = new Font(txtbox.Font.FontFamily, 16);
txtbox.BackColor = Color.Cyan;
txtbox.TextAlign = HorizontalAlignment.Center;
tb[i][j] = txtbox;
this.panel1.Controls.Add(tb[i][j]); // Add as a child of panel
curX += width + 15;
}
curX = 10;
curY = curY + height + 20;
}
}
}
Running it with a value of 6 gave:

I can't add more than 2 controls to canvas

I am trying to add controls at run-time for a Windows Phone app but I can't add more than 2 controls to the canvas. I have a textbox in mainscreen, and the user will enter a number which will yield that many textboxes. This code works for 0, 1, or 2 textboxes, but cannot add more than 2:
`int seriuzunlugu;
seriuzunlugu=Convert.ToInt32(SeriUzunluguTxt.Text);
int Xtop, Xleft, Ytop, Yleft;
Xtop = 10;
Xleft = 70;
Ytop = 10;
Yleft = 250;
for (int i = 0; i <seriuzunlugu ; i++)
{
//Xi değeri
TextBox Xi = new TextBox();
Xi.Name = "X" + i.ToString();
Xi.Width = 5;
Xi.Height = 5;
canvas.Children.Add(Xi);
Canvas.SetLeft(Xi,Xleft);
Canvas.SetTop(Xi,Xtop);
Xtop = +60;
//Yi değeri
TextBox Yi = new TextBox();
Yi.Name = "Y" + i.ToString();
Yi.Width = 5;
Yi.Height = 5;
canvas.Children.Add(Yi);
Canvas.SetLeft(Yi, Yleft);
Canvas.SetTop(Yi, Ytop);
Ytop = +60;
}
//X değeri
TextBox x = new TextBox();
x.Name = "xdegeri";
x.Width = 50;
x.Height = 10;
x.Text = canvas.Children.Count.ToString();
canvas.Children.Add(x);
Canvas.SetTop(x, Xtop + 50);
Canvas.SetLeft(x, Xleft);`

Controls strangely shown when looped;

My problem is this:
Those labels are created there via for loop and I think you got what my problem is,
that weird overlapping.
Code:
for (int i = 0; i < maxlabels ; i++)
{
Label x = new Label();
x.Name = string.Format("label{0}", i);
x.Top = 2 + (15 * i); // <---- changed this line
x.Left = 3;
x.Text = x.Name;
x.BringToFront();
x.BackColor = Color.Transparent;
panel1.Controls.Add(x);
}
In the above code, when changed the
x.Top = 2 + (30 * i);
into
(15 * i);
i got the result:
I would suggest you use FlowLayoutPanel. And set direction to top-down (you can do that in property UI also):
yourFlowLayoutPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
You will not need to set top, left etc in this panel:
e.g.
for (int i = 0; i < maxlabels ; i++)
{
Label x = new Label();
x.Name = string.Format("label{0}", i);
x.Text = x.Name;
x.BackColor = Color.Transparent;
yourFlowLayoutPanel.Controls.Add(x);
}
Since you don't explicitly set a Height on the Label it wull use the default.
You could solve this by setting x.AutoSize = true; and using the Labels Height in the multiplier
for (int i = 0; i < maxlabels ; i++)
{
Label x = new Label();
x.Name = string.Format("label{0}", i);
x.AutoSize = true;`
x.Top = 2 + (x.Height * i);
x.Left = 3;
x.Text = x.Name;
x.BringToFront();
x.BackColor = Color.Transparent;
panel1.Controls.Add(x);
}

Categories

Resources