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;
}
}
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'm working on a program right now, and I was wondering if it were possible to have a return function that will return the object/value/variable generated during every loop? Below is the code that I want to work. My only error is the return values.
for (int i = 1; i < ProductArray.Length; i++)
{
Label lbl = new Label();
ThresholdPanel.Controls.Add(lbl);
lbl.Top = A * 28;
lbl.Left = 15;
lbl.Font = new Font(lbl.Font, FontStyle.Bold);
lbl.Text = ProductArray[i];
lbl.Name = "Label" + ProductArray[i];
TextBox txt = new TextBox();
ThresholdPanel.Controls.Add(txt);
txt.Top = A * 28;
txt.Left = 125;
//txt.Text = "Text Box All" + this.A.ToString();
txt.Name = "txt" + A;
textBoxes[txt.Name] = txt;
A = A + 1;
return txt;
return lbl;
}
Thanks in advance and I'm sorry if this is really a simple question....
use yield return instead of return as long as the method returns an IEnumerable<T> where T is the type that you're wanting to yield. It will result in a method that returns a sequence of items, and adds an item to that sequence for every item you yield return.
Use yield return as in provided sample:
IEnumerable<string> Test()
{
for (int i = 1; i < ProductArray.Length; i++)
{
Label lbl = new Label();
ThresholdPanel.Controls.Add(lbl);
lbl.Top = A * 28;
lbl.Left = 15;
lbl.Font = new Font(lbl.Font, FontStyle.Bold);
lbl.Text = ProductArray[i];
lbl.Name = "Label" + ProductArray[i];
TextBox txt = new TextBox();
ThresholdPanel.Controls.Add(txt);
txt.Top = A * 28;
txt.Left = 125;
//txt.Text = "Text Box All" + this.A.ToString();
txt.Name = "txt" + A;
textBoxes[txt.Name] = txt;
A = A + 1;
yield return txt;
}
}
More details on IEnumerable
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());
}