I can't add more than 2 controls to canvas - c#

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

Related

How I get id and total value of dynamically created text boxes?

I create text boxes dynamically and use to store the data. How do I get the text boxes id and how to get total value from all text boxes?
I used this code for creating dynamic text boxes:
for (int B = 0; B < 4; B++)
{
TextBox tb = new TextBox();
int i = TextBoxes.Count + 1;
tb.Location = new Point(0, i * 28);
tb.Width = 80;
tb.Name = "ID" + i;
tb.Text = i.ToString();
TextBoxes.Add(tb);
panel1.Controls.Add(tb);
}
you can try this if you are sure that all values will only hold numbers
var allTexboxes = this.Controls.OfType<TextBox>();
var sumOfAllTextBoxes = allTexboxes.Sum(x => Convert.ToInt32(x.Text));
if not
var allTexboxes = this.Controls.OfType<TextBox>();
var sum = 0;
foreach (var allTexbox in allTexboxes)
{
if (int.TryParse(allTexbox.Text, out var i))
{
sum = sum + i;
}
}

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 fill every seat number corresponding to the text button with a loop in C#?

First of all, I'm a rookie in programming and I'm not English, I'm sorry if I had a mistake expressing myself :D
I'm doing a program that represents a booking system bus. I had created 64 buttons at Runtime with an array of buttons, but that isn't the problem.
The problem is to represent each text button corresponding to the number of seats of the bus. Here is my code:
var buttonArray = new Button[64];
for (var i = 1; i <= 4; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Size = new Size(75, 23);
buttonArray[i].Name = "button" + i;
buttonArray[i].Text = "Seat " + i;
buttonArray[i].Location = new Point(-50 + (i*100), 10);
panel1.Controls.Add(buttonArray[i]);
for (var j = 5; j <= 19; j++)
{
buttonArray[j] = new Button();
buttonArray[j].Size = new Size(75, 23);
buttonArray[j].Name = "button" + j;
buttonArray[j].Text = "Seat " + j;
buttonArray[j].Location = new Point(-50 + (i * 100), -105 + (j * 30));
panel1.Controls.Add(buttonArray[j]);
}
}
The result would be 64 buttons represented with 4 columns and 16 rows, for example:
Here's how I would do it: Just loop from zero to 63, and calculate the row and column on the fly. Then you set the button properties and add it to the panel. You can change the size of the seats by adjusting the seatWidth and seatHeight variables. Also note that I added a little extra space between the first two columns and the second two columns, because it looks like that's what you had in your diagram. You can adjust this with the middleRowWidth variable. You can also customize the distance between the seats by adjusting the seatSpacing value:
const int seatSpacing = 4;
const int middleRowWidth = 20;
const int seatWidth = 55;
const int seatHeight = 20;
panel1.Width = 4 * (seatWidth + seatSpacing) + seatSpacing + middleRowWidth;
var buttonSize = new Size(seatWidth, seatHeight);
for (var i = 0; i < 64; i++)
{
// Calculate the location for this seat
int thisRow = i / 4;
int thisColumn = i % 4;
int seatTop = thisRow * (seatHeight + seatSpacing);
int seatLeft = thisColumn * (seatWidth + seatSpacing);
// Add some extra distance down the middle
if (thisColumn >= 2) seatLeft += middleRowWidth;
// Create a new button
var thisButton = new Button
{
Size = buttonSize,
Name = "button" + (i + 1),
Text = "Seat " + (i + 1),
Location = new Point(seatLeft, seatTop),
Visible = true,
};
// Add it to the panel
panel1.Controls.Add(thisButton);
}
Result:
UPDATE
After thinking about how to make this more generic, I thought about how some airplanes have their seats laid out. Some have two seats on each side, some have three, some have two on each side with a column of three down the middle. In order to accommodate different types of layouts, I modified the code slightly. Below code has customizable variables for your layout. The one below is for a 2-3-2 layout, where the numbers are the seat count and - are aisles. It also includes two "exit row" aisles running horizontally:
// Seating Layout
const int numberOfRows = 20;
const int numberOfColumns = 7;
const int numberOfSeats = numberOfRows * numberOfColumns;
var columnsToInsertAisleAfter = new List<int> {1, 4};
var rowsToInsertAisleAfter = new List<int> {6, 12};
// Seat sizing
const int seatWidth = 35;
const int seatHeight = 20;
const int seatSpacing = 4;
const int aisleWidth = 20;
var seatSize = new Size(seatWidth, seatHeight);
// Panel and form layout
panel1.Size = new Size(
numberOfColumns * (seatWidth + seatSpacing) + seatSpacing +
aisleWidth * columnsToInsertAisleAfter.Count,
numberOfRows * (seatHeight + seatSpacing) + seatSpacing +
aisleWidth * rowsToInsertAisleAfter.Count);
this.Size =
new Size(panel1.Size.Width + (panel1.Left * 2),
panel1.Size.Height + (panel1.Top * 2)) +
new Size(this.Width - this.ClientSize.Width,
this.Height - this.ClientSize.Height);
// Add seats to panel
for (var i = 0; i < numberOfSeats; i++)
{
// Calculate the location for this seat
int thisRow = i / numberOfColumns;
int thisColumn = i % numberOfColumns;
int seatTop = thisRow * (seatHeight + seatSpacing);
int seatLeft = thisColumn * (seatWidth + seatSpacing);
// Add some extra distance for aisles
var aislesBeforeThisColumn =
columnsToInsertAisleAfter.Count(col => col < thisColumn);
seatLeft += aislesBeforeThisColumn * aisleWidth;
var aislesBeforeThisRow =
rowsToInsertAisleAfter.Count(row => row < thisRow);
seatTop += aislesBeforeThisRow * aisleWidth;
// Add a new button to the panel
panel1.Controls.Add(new Button
{
Size = seatSize,
Name = "button" + (i + 1),
Text = (i + 1).ToString(),
Location = new Point(seatLeft, seatTop),
Visible = true,
});
}
Large Airplane Layout:
This should do what you want,
Point pt;
int y = 20;
int x = 0;
for (int i = 0; i < 16; i++)
{
x = 20;
for (int j = 1; j <= 4; j++)
{
pt = new Point(x, y);
Button btn = new Button() { Location = pt, Size = new Size(75, 23) };
btn.Name = i.ToString() + j.ToString();
btn.Text = string.Format("{0} {1:D2}", "Seat", i * 4 + j);
panel1.Controls.Add(btn);
x += 90;
}
y += 30;
}

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

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