I have TableLayoutPanel and adding rows dynamically. How can I insert the rows with controls at a specific index?
private void AddRowstoTableLayout()
{
for (int cnt = 0; cnt < 5; cnt++)
{
RowStyle newRowStyle = new RowStyle();
newRowStyle.Height = 50;
newRowStyle.SizeType = SizeType.Absolute;
Label lbl1 = new Label();
lbl1.Text = "label-" + (cnt + 1);
TextBox t1 = new TextBox();
t1.Text = "text-" + (cnt + 1);
Label lblMove = new Label();
lblMove.Text = "Move-" + (cnt + 1);
lblMove.MouseDown += new MouseEventHandler(dy_MouseDown);
tableLayoutPanel1.RowStyles.Insert(0, newRowStyle);
this.tableLayoutPanel1.Controls.Add(lbl1, 0, cnt); //correct
this.tableLayoutPanel1.Controls.Add(t1, 1, cnt); //correct
this.tableLayoutPanel1.Controls.Add(lblMove, 2, cnt); //correct
tableLayoutPanel1.RowCount += 1;
}
}
I tried
tableLayoutPanel1.RowStyles.Insert(0, newRowStyle);
but no luck
There is no such function out of the box.
The way is to add a row to your TableLayoutPanel. Then move all controls that are positioned in rows greater equal to your desired row index in a rowindex higher than their current position.
Here how you can do that:
void InsertTableLayoutPanelRow(int index)
{
RowStyle newRowStyle = new RowStyle();
newRowStyle.Height = 50;
newRowStyle.SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles.Insert(index, newRowStyle);
tableLayoutPanel1.RowCount++;
foreach (Control control in tableLayoutPanel1.Controls)
{
if (tableLayoutPanel1.GetRow(control) >= index)
{
tableLayoutPanel1.SetRow(control, tableLayoutPanel1.GetRow(control) + 1);
}
}
}
Related
I have table layout panel. Inside each one I create a Panel to insert two different controls in each cell.
I want to fill TableLayoutPanel with all my data, in this case I have 16 should have 16 different cells. So I try:
private void AddRow(IList<DeliveryBreakdownGetViewModel> rModel)
{
tpnlDeliveryBreakdown.RowCount = rModel.Count / 4; //rModel count = 16
tpnlDeliveryBreakdown.ColumnCount = 4;
var row = 0;
var column = 0;
var fullRow = 1;
for (int i = 0; i < rModel.Count; i++)
{
var panel = new Panel();
Labels.Add(rModel[i].DesignGroupName);
var label = new Label
{
AutoSize = true,
Name = "label" + Labels.Count,
Text = rModel[i].DesignGroupName,
Location = new Point(12, YPos)
};
this.Controls.Add(label);
tpnlDeliveryBreakdown.Controls.Add(panel, column, row);
panel.Controls.Add(label);
DeliveryBreakdownLabelsModel.Add(label);
var numericUpDown = new NumericUpDown
{
Name = "numericUpDown" + Labels.Count,
Maximum = decimal.MaxValue,
Minimum = decimal.MinValue,
Value = Decimal.Round(rModel[i].ContractedAmount, 2),
Location = new Point(12, YPos),
Size = new Size(60, 19),
DecimalPlaces = 2,
Tag = rModel[i].DesignGroupId
};
this.Controls.Add(numericUpDown);
panel.Controls.Add(numericUpDown);
DeliveryBreakdownNumericUpDownModel.Add(numericUpDown);
if (fullRow % 4 == 0)
{
row++;
}
fullRow++;
if(column == 4)
{
column = 0;
}
else
{
column++;
}
}
}
But it only show 2 columns and 7 cells:
Why is not showing all cells and it only show 2 columns instead 4. Regards
How can I add values in my dynamically created texbox in another textbox ? For example I have 6 textbox named from TextBox0 to TextBox6 and I want to add the values in TextBox0 to TextBox5 then set that values in TextBox6. How can I do that?
This is my code for create dynamic textbox:
static int column = 0;
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
if (column > 0)
{
do
{
TextBox tb = new TextBox();
tb.Text = "";
tb.Name = "TextBox" + (i + column * 6);
TextBox t = (TextBox)Controls["TextBox" + i.ToString()];
Point p = new Point(15 + (column * 125), 5 + (i * 25));
tb.Location = p;
this.Controls.Add(tb);
i++;
} while (i <= 5);
}
else
{
do
{
TextBox tb = new TextBox();
tb.Text = "";
tb.Name = "TextBox" + i;
TextBox t = (TextBox)Controls["TextBox" + i.ToString()];
Point p = new Point(15, 5 + (i * 25));
tb.Location = p;
this.Controls.Add(tb);
i++;
} while (i <= 5);
}
column++;
}
private const string _textBoxName = "TextBox";
The method count textboxes sum by given range of text box ids. Be aware this will throw exception if the text box texts / name id are not intgeres or
private int Count(int from, int to)
{
int GetIdFromTextBox(TextBox textBox) => int.Parse(new string(textBox.Name.Skip(_textBoxName.Length).ToArray()));
var textBoxes = Controls.OfType<TextBox>().ToList();
var textBoxesWithIds = textBoxes.Select(textBox => (textBox: textBox, id: GetIdFromTextBox(textBox))).ToList();
var sum = textBoxesWithIds.Where(x => x.id >= from && x.id <= to).Sum(x => int.Parse(x.textBox.Text));
return sum;
}
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;
}
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);`
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());
}