Controls strangely shown when looped; - c#

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

Related

How can I make N label?

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

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

How to delete a row in a grid in C# windows phone 8.1

I have started developing a windows phone application where I'm creating the row in a grid dynamically. Upon some conditions I need to the delete the row and all its content in that row.
Here is my sample code.
result = e.Parameter as string;
string[] acst = result.Split('|');
int j = 0;
root2.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(10) });
for (int i = 6; i < acst.Length - 1; i++)
{
TextBlock mytextblock = new TextBlock();
mytextblock.Name = "txtDetails" + root2.Children.Count + 1;
mytextblock.Text = acst[i + 1];
if (mytextblock.Text != "")
{
if (mytextblock.Text.Trim() != "0.00") // if result is 0.00 then i have to delete all the content in that row.
{
if (j == 0)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.HorizontalAlignment = HorizontalAlignment.Stretch;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 1);
Grid.SetRow(mytextblock, (i) / 6);
j++;
}
else if (j == 1)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.Visibility = Visibility.Collapsed;
mytextblock.HorizontalAlignment = HorizontalAlignment.Center;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 2);
Grid.SetRow(mytextblock, (i) / 6);
j++;
}
else if (j == 2)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.TextWrapping = TextWrapping.Wrap;
mytextblock.HorizontalAlignment = HorizontalAlignment.Left;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 3);
Grid.SetRow(mytextblock, (i) / 6);
j=0;
root2.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(60) });
}
}
root2.Children.Add(mytextblock);
}
else
{
root2.RowDefinitions.RemoveAt((i / 6)); // here I'm getting Arugument Exception
}
}
for example if I get mytextblock.text = 0.00 in third column (j=2 in this case). i need to remove the content in column 1 and 2 or to delete thst particular row.
I've tried "root2.RowDefinitions.RemoveAt" but there I am getting Arugument Exception. Where I'm missing?
Any help would be highly appreciated.
Initially your grid has 1 row you are creating whose index is 0. But when you are executing
root2.RowDefinitions.RemoveAt((i / 6));
actually you are getting
root2.RowDefinitions.RemoveAt(1);
So you should use
root2.RowDefinitions.RemoveAt((i / 6)-1); would solve the exception
To answer the second question the code is too much confusing. I am listing some
1: Why are you starting loop from 6
2: Why dividing i/6
3: What exactly you are getting from result

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

Increase the label name

I need a command code that increase the label name.
I need to display in 10 (example) labels some texts.
Example:
label1.Text = "1";
label2.Text = "2";
label3.Text = "3";
label4.Text = "4";
label5.Text = "5";
label6.Text = "6";
I need to increase the number from label name (label1, label2, etc.) in a foreach where I will increase the variable i (i will be use in a structure like this label.Name = "label" + i.ToString();).
I hope that you understand what I want to say.
I try this but don't work:
Label[] label = new Label[2];
int ii = 0;
foreach(...) // go through a list
{
label[ii] = new Label();
label[ii].Text = x.materie + tip + "\nsala " + x.sala;
label[ii].Visible = true;
label[ii].Location = new System.Drawing.Point(cX, cY);
label[ii].SetBounds(cX, cY, 98, cH);
label[ii].MinimumSize = new Size(98, cH);
label[ii].MaximumSize = new Size(98, cH);
ii++;
}
int count = 10;
for (int i = 1; i <= count; i++)
{
// setup label and add them to the page hierarchy
Label lbl = new Label();
lbl.Name = "label" + i;
lbl.Text = i.ToString();
//assuming form1 is a form in your page with a runat="server" attribute.
this.Controls.Add(lbl);
}
Assuming you have an array of Label controls, label, you can do the following:
int i = 1;
for (int i = 1; i < label.Length; i++)
{
lbl.ID = String.Format("label{0}", i.ToString());
}

Categories

Resources