How to use different labels in one for cycle - c#

ex: label[i].text. in
int r = GridView1.Rows.Count;
for (int i = 0; i < r; i++)
{
Label[i]+"1".text="something";
}
Here, in one for cycle, I want to fill different labels. Label id's are Label01, Label02,Label03 and so on. What is the correct syntax?

If you can 'predict' the Id of the Label then you can find it:
int r = GridView1.Rows.Count;
for (int i = 0; i < r; i++)
{
string id = "baseName" + i; // your naming scheme
var lbl = (Label) this.FindControl(id);
lbl.text="something";
}

You can add all the labels into a List<Label> or SortedList<Label> and iterate over that.
var labels = new SortedList<Label>();
lables.Add("Lable01", Label01);
lables.Add("Lable02", Label02);
...
int r = GridView1.Rows.Count;
for (int i = 0; i < r; i++)
{
lables["Label" + i.ToString("00")].text = "somthing";
}
There are no control arrays in .NET, as there were in VB6.

Label myLabel = this.FindControl("Label"+i+"1") as Label;
myLabel.Text = "my text";
THIS should solve your problem

Related

Is it possible to call a variable from string input

I have five Labels in my form.
The names of the label are the following.
1) labelTeam1Name.Text
1) labelTeam2Name.Text
3) labelTeam3Name.Text
4) labelTeam4Name.Text
5) labelTeam5Name.Text
now I want to run a loop and get the text values of the label
for( int i = 1; i < 6; i++ )
{
string str = labelTeam(i)Name.Text // Get the values based on the input i
}
I can definitely fill these values in an array or list and then call them in the loop.
is it possible to do something like this labelTeam(i)Name.Text?
You can use Controls and OfType
Filters the elements of an IEnumerable based on a specified type.
var results = Controls.OfType<Label>().Select(x => x.Text);
or
foreach (var ctrl in Controls.OfType<Label>())
{
//var str = ctrl.Text;
}
if you need to base this on the name, you could use Find or
var labels = Controls.OfType<Label>().ToList();
for (int i = 0; i < labels.Count(); i++)
{
var label = labels.FirstOrDefault(x => x.Name == $"blerp{i}derp");
if (label != null)
{
}
}
You can use the Controls.Find() method:
for( int i = 1; i < 6; i++ )
{
string str = ((Label)Controls.Find("labelTeam" + i + "Name",true)[0]).Text // Get the values based on the input i
}
You can use a Label array.
System.Windows.Forms.Label[] Labels = new System.Windows.Forms.Label[5];
Labels[0] = labelTeam1Name;
Labels[1] = labelTeam2Name;
Labels[2] = labelTeam3Name;
Labels[3] = labelTeam4Name;
Labels[4] = labelTeam5Name;
for( int i = 0; i < Labels.Lenth; i++ )
{
string str = Labels[i].Text;
}

How do you retrieve inputted text from a textbox array?

I am creating a calculator where the user enters a number into a textbox specifing how many inputs (textboxes) the user wants to have (Code not shown). I have used a textbox array to create these textboxes. The problem comes when I want to get the text from these textboxes to perform the calculations, the code I have written so far for this is shown below:
int n;
TextBox[] textBoxes;
Label[] labels;
double[] values;
public void GetValue()
{
n = Convert.ToInt16(txtInputFields.Text);
values = new double[n];
textBoxes = new TextBox[n];
for (int i = 0; i < n; i++)
{
}
}
I am unsure what to put in the for loop for this; I have tried the following:
values[n] = Convert.toDouble(textBoxes[n].Text);
but it gives me the error: Index was outside the bounds of the array.
I am new to C# and programming in general so any help would be much appreciated.
Thanks.
EDIT: Code to create textboxes is shown here:
public void InstantiateTextFields()
{
n = Convert.ToInt16(txtInputFields.Text);
int posLeft = 100;
textBoxes = new TextBox[n];
labels = new Label[n];
// Creates number of inputs and labels as specified in txtInputFields (n).
for (int i = 0; i < n; i++)
{
textBoxes[i] = new TextBox();
textBoxes[i].Top = 100 + (i * 30);
textBoxes[i].Left = posLeft;
textBoxes[i].Name = "txtInput" + (i + 1);
labels[i] = new Label();
labels[i].Top = 100 + (i * 30);
labels[i].Left = posLeft - 50;
labels[i].Text = "Input " + (i + 1);
labels[i].Name = "lblInput" + (i + 1);
}
for (int i = 0; i < n; i++)
{
this.Controls.Add(textBoxes[i]);
this.Controls.Add(labels[i]);
}
}
Your code in the GetValue method recreates the array of textboxes and doing so destroys the orginal content (the textboxes dynamically created InstantiateTextFields).
In this way your loop fails with Object Reference not set.
You just need to use the global variable without reinitiaizing it
public void GetValue()
{
n = Convert.ToInt16(txtInputFields.Text);
values = new double[n];
// textBoxes = new TextBox[n];
for (int i = 0; i < n; i++)
{
values[i] = Convert.ToDouble(textBoxes[i].Text);
}
}
There is something to be said about reading the input text and converting it to double without checks. If your user types something that cannot be converted to a double your code will crash on the Convert.ToDouble line. Use instead
double temp;
for (int i = 0; i < n; i++)
{
if(double.TryParse(textBoxes[i].Text, out temp)
values[i] = temp;
else
{
// Not a double value....
// A message to your user ?
// fill the array with 0 ?
// Your choice....
}
}
values[n] = Convert.toDouble(textBoxes[n].Text); gives you error because n is outside of the array. You allocate an array with the size of n which is zero indexed aka the last element is at position n-1.
for (int i = 0; i < n; i++)
{
values[i] = Convert.toDouble(textBoxes[i].Text);
}

How to get entire no of rows present in data table into single label using button click event in window form c#

How to get entire rows present in data table into single label(for every row we should not take new label,we should take only single label for "N" no of rows present in datatable) using button click event in window form c#
for (int i = 1, r = 0; i <= dt.Rows.Count; i++, r++)
{
label19.Text = Convert.ToInt32(i).ToString();
label20.Text = dt.Rows[r]["Ques"].ToString();
}
For All Rows and columns
line.Text = "";
for(int i = 0; i<dt.Rows.Count; i++)
{
for(int j = 0; j<dt.Columns.Count; j++)
{
line.Text +=dt.Rows[i][j].ToString()+" ";
}
line.Text +=Environment.NewLine;
}
this should work
for your example
for(int i = 0; i<dt.Rows.Count; i++)
{
line.Text +=(i+1).ToString()+" "+dt.Rows[i]["Ques"].ToString()+" ";
line.Text +=Environment.NewLine;
}
you can format it To Look Porper in web project
line.Text ="<table>";
for(int i = 0; i<dt.Rows.Count; i++)
{
line.Text ="<tr>";
for(int j = 0; j<dt.Columns.Count; j++)
{
line.Text +="<td>"+dt.Rows[i][j].ToString()+"</td>";
}
line.Text +="</tr>";
}
line.Text +="</table>";

how can I reach radiobuttonlist?

I write this codes :
TabContainer TabContainer1 = new TabContainer();
TabContainer1.ID = "TabContainer1";
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
for (int toplam_grp = 0; toplam_grp < 1; toplam_grp++)
{
Panel my_panel= new Panel();
my_panel.ID = toplam_grp + "my_panel";
for (int j = 0; j < 10; j++)
{
Label my_label = new Label();
my_label.ID = j + 10 * toplam_grp + "mylabel";//burda 10 j nin sınırı olcak
my_label.Text = "asdasdas";
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.ID = j + 10 * toplam_grp + "radioButton";
radioButtonList.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
for (int i = 0; i < 5; i++)
{
radioButtonList.Items.Add(((char)(i + 65)).ToString());
}
my_panel.Controls.Add(my_label); /////////////////////////burda yanyana gözükse daha iyi olur
my_panel.Controls.Add(radioButtonList);
}
TabPanel tab = new TabPanel();
tab.ID = toplam_grp+"tab1";
tab.HeaderText =toplam_grp+"nbr";
TabContainer1.Tabs.Add(tab);
TabContainer1.Tabs[toplam_grp].Controls.Add(my_panel);
}
cell.Controls.Add(TabContainer1);
row.Cells.Add(cell);
myplace.Rows.Add(row);
I create a tabcontainer and one tab and in tab I create 10 radiobuttonlist(I give them ids each one) which have 5 members.
And I write this code to reach radiobuttonlist but ı dont reach because dont find their ids :
string ss = "";
for (int i = 0; i < 10; i++)
{
ss += ((RadioButtonList)(FindControl(i + "radioButton"))).SelectedIndex + "\n";
}
MessageBox.Show(ss);
for example first radiobuttonlist id : 0radioButton but it cant found.What can I do.Thanks for answering...
The whole code is a little confusing for me but I try to add my 2 cents.
You need to execute FindControl against the RadiobuttonList container and not against the page.
So, for instance, if you added a control named RadioButtonList1 to a asp.net panel named Panel1 you would have to do
Panel1.FindControl("RadioButtonList1 ")

c# what is the efficent way to do this code - style php "eval"

i have a windows application in c# , in the form i have 12 labels with the names
label1, label2 , ...............
i have array of 12 numbers (after some calculate)
like :
int[] nums = new int[12] {1, 0, 4, 6,.............};
i want to assign in loop to every label item a value from the array Respectively
something like
for (int i = 1; i <= 12; i++) {
label+i.Text = nums[i-1].ToString();
}
what is the efficient way to do this ?
thanks
Create a corresponding array of labels:
Label[] labels = new Label[12] { label1, label2, ... };
for(int i = 0; i < 12; i++)
{
labels[i].Text = nums[i].ToString();
}
If all of the labels belong to the same control (a Panel for instance), you can use the find control to perform this assignment:
for(int i = 0;i < 12; i++)
{
Label lbl = myPanel.FindControl("Label" + i.ToString());
lbl.Text = nums[i].ToString();
}
You can use the FindControl method to locate a control based on the name:
for (int i = 0; i < nums.Length; i++) {
(Form.FindControl("label" + i.ToString()) as Label).Text = nums[i].ToString();
}

Categories

Resources