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());
}
Related
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;
}
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 am getting error :
"There is already an open DataReader associated with this Command
which must be closed first."
Code is as follows :
I am generating dynamic table using 1 table & in every row of that table I want sum calculations from another Table
I have called readDr() first using sqlcommand "cmd" & ExecuteReader(). After that I have called CalTotAmt(string CC) which uses sqlcommand "cmdTotAmt" & ExecuteScalar().
I have used 2 diff sqlcommand still its giving error.
protected void readDr()
{
string str = "select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc from ChallanTable;";
cmd = new SqlCommand(str, con);
rdr = cmd.ExecuteReader();
}
protected void CreateChallanTable()
{
table.Caption = "Challan Entry";
table.ID = "Challan Entry";
table.BackColor = System.Drawing.Color.Maroon;
table.ForeColor = System.Drawing.Color.Gray;
readDr(); //call to function readDr()
Panel2.Controls.Add(table);
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
m = 0;
while (rdr.Read())
{
row = new TableRow();
row.ID = "Row" + m;
row.BorderStyle = BorderStyle.Ridge;
for (n = 0; n <= 6; n++)
{
cell = new TableCell();
cell.ID = "cell" + m + n;
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
//cell.BorderColor = System.Drawing.Color.Azure;
for (n = 0; n <= 0; n++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + m + n;
lbl.Text = Convert.ToString(rdr[0]);
lbl.Width = 70;
CCNo = lbl.Text;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 1; n <= 1; n++)
{
Label lbl = new Label();
lbl.ID = "lblTotAmt" + m + n;
lbl.Text = Convert.ToString(rdr[1]);
lbl.Width = 70;
TotAmt = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
Label2.Text = Convert.ToString(CalTotAmt(CCNo));
}
for (n = 2; n <= 2; n++)
{
Label lbl = new Label();
lbl.ID = "lblNoRect" + m + n;
lbl.Text = Convert.ToString(rdr[2]);
lbl.Width = 70;
NoofRects = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 2; n <= 2; n++)
{
Label lbl = new Label();
lbl.ID = "lblEnergy" + m + n;
lbl.Text = Convert.ToString(rdr[3]);
lbl.Width =70;
Energy = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 3; n <= 3; n++)
{
Label lbl = new Label();
lbl.ID = "lblNew" + m + n;
lbl.Text = Convert.ToString(rdr[4]);
lbl.Width =70;
NewSD = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 4; n <= 4; n++)
{
Label lbl = new Label();
lbl.ID = "lblTheft" + m + n;
lbl.Text = Convert.ToString(rdr[5]);
lbl.Width = 70;
Theft = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (n = 5; n <= 5; n++)
{
Label lbl = new Label();
lbl.ID = "lblMisc" + m + n;
lbl.Text = Convert.ToString(rdr[6]);
lbl.Width = 70;
Misc = Convert.ToDouble(lbl.Text);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
//dr.NextResult();
//outer for-loop end
m++;
}
rdr.Close();
}
protected double CalTotAmt(string CC)
{
double Total = 0;
string str = "select Sum(Amount) from MainDataTable Where CC='" + CC + "' and BU ='" + LogInBU + "'";
SqlCommand cmdTotAmt = new SqlCommand(str,con);
Total = Convert.ToDouble(cmdTotAmt.ExecuteScalar());
Label2.Text = Total.ToString();
return Total;
}
Please help me out.
Here you are trying to open multiple recordset open concurrently on a same connection. You can do that by adding MultipleActiveResultSets=True to the connection string.
Try modifying you code, to make sure DataReader is closed properly:
Sample Code:
protected DataTable readDr()
{
con.Open();
string str = "select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc from ChallanTable;";
cmd = new SqlCommand(str, con);
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
rdr.Close();
con.Close();
}
You would generally fix it by writing a single query that computes all of the results in one go - rather than forcing your code to keep querying the database, over and over.
Something like:
select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc,SumTotal
from ChallanTable ct
cross apply
(select Sum(Amount) as SumTotal from MainDataTable Where CC=ct.CCNo) t
Then you just have to process the results.
(Also, you have a bug in your display code at present - you have two attempts to do something when n==2, the second for will never be entered)
I am trying to assign button controls text dynamically by using a loop like the following:
int ButtonNumber = 1;
while (ButtonNumber < 10)
{
//WHAT TO DO HERE!?
ButtonNumber++;
}
I wanna avoid the following:
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
Ideally, don't have button1, button2 etc as variables in the first place. Have a collection (e.g. a List<Button>):
private List<Button> buttons = new List<Button>();
(EDIT: You'd need to populate this somewhere, of course...)
then to update the text later:
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].Text = "I'm button " + i;
}
This doesn't play terribly nicely with the designer, unfortunately.
You can fetch a control by ID, but personally I try not to do that. If you really want to:
for (int i = 1; i <= 10; i++)
{
Control control = Controls["button" + i];
control.Text = "I'm button " + i;
}
Create an array of buttons:
Button[] buttons = new Button[10];
for(int i = 1; i < 10; i++)
{
buttons[i] = new Button();
buttons[i].Text = "";
}
You can always use this.Controls.Find() to locate a named control in WinForms.
You can also do like this simply, assuming if you have all buttons in your form
foreach (Control c in this.Controls)
{
if (c is Button)
{
c.Text = "MyButton" + (c.TabIndex + 1);
}
}
// try this
Button btn;
for (int n_i = 0; n_i < 10; n_i++)
{
btn = (Button)Controls.Find("button" + n_i,true)[0];
btn.Text = "";
}