In C#, I want to assign all 10 Checklistboxes on my page with a value (e.g. 1). What is the syntax so that I can use a variable? I.e. checklistbox(i).selectedIndex = 1;
for (int i = 1; i < 11; i++)
{
checklistbox1.selectedIndex = 1;
checklistbox2.selectedIndex = 1;
checklistbox3.selectedIndex = 1;
...
checklistbox10.selectedIndex = 1;
}
I guess you should use "FindControl" method inorder to do that as shown below.
for (int i = 1; i <= 10; i++)
{
(Page.FindControl("checklistbox" + i) as CheckBox).SelectedIndex = 1;
}
"checklistbox" is assumed as "ID" that is prefixed for all the checkboxes.
Hope this helps!!
You can loop over all the controls on the page and pick out the ones you need as described in this blog post by Kris Steele:
foreach (Control masterControl in Page.Controls)
{
if (masterControl is MasterPage)
{
foreach (Control formControl in masterControl.Controls)
{
if (formControl is System.Web.UI.HtmlControls.HtmlForm)
{
foreach (Control contentControl in formControl.Controls)
{
if (contentControl is ContentPlaceHolder)
{
foreach (Control childControl in contentControl.Controls)
{
if(childControl is CheckBoxList)
{
((CheckBoxList)childControl).SelectedIndex = 1;
}
}
}
}
}
}
}
}
This may not be a good idea if you have a lot of controls on the page.
You should create a List<CheckBoxList>:
var cbs = new List<CheckBoxList> { thingy, otherThingy, ... };
foreach (var cb in cbs)
cb.SelectedIndex = 0;
Related
I am developing a Windows Forms Application for lending. When I click on the datagrid1 add (green) button the equipment is added on datagrid2, but I would like that to only happen once for each item.
I am using this code to pass the line data:
private void DataGridEquipamento_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
{
return;
}
if (e.ColumnIndex == 2)
{
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
}
There's a couple of ways to achieve what I believe you're asking for.
The first way would be to actually remove the item from the first list once it's been added to the second. This prevents the user from even attempting to add more than one, and ensures what the interface is presenting makes contextual sense.
With that said, to answer the question exactly, because you're effectively cloning the Row, you can perform a comparison to determine if it's already been added:
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
if (!this.dataGridEmprestimo.Rows.Contains(rowData))
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
In terms of optimisation, you may only need to check for a single value from the Row - such as the Id - to determine if it already exists. You could write a simple method to return a count of Rows with that Id. If there are none, or if the number is less than the maximum allowed, then you're free to add that Row.
public int CountObjectsInRows(string idValue)
{
int count = 0;
if (string.IsNullOrEmpty(idValue))
{
return count;
}
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
if (row.Cells[0].Value.ToString() == idValue)
{
count++;
}
}
return count;
}
You could use this method in place of the above "this.dataGridEmprestimo.Rows.Contains(rowData)" condition.
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
if (CountObjectsInRows(rowData.Cells[0].ToString()) == 0)
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
I dynamicly generate some Textboxes and put them into a canvas.
private void create_textbox(int number)
{
for(int i = 1; i < number; i++)
{
TextBox MyBox = new TextBox();
darsteller_feld.Name = Convert.ToString("MyBox" + number);
MyBox.Height = 25;
MyBox.Width = 250;
MyBox.Text = "New Box" ;
MyBox_canvas.Children.Add(MyBox);
}
}
So i thought i could pt all Texts into a string [] like this:
for(int i = 0; i < number; i++)
{
ARR[i] = ?Unknown_Textbox_Name?.Text;
}
How can i replace the "?Unknown_Textbox_Name?" Part?
Either you save all your textbox in a list when you create them and the just add them to your array.
Or if you want something more generic
U need to find all the childreen inside your canvas using that method.
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
Then you enumerate over the controls like so
foreach (TextBox tb in FindVisualChildren<TextBox>(MyBox_canvas))
{
ARR.Add(tb.Text);
}
Either use #Moez Rebai solution, or, if your TextBoxes are direct children of your Canvas:
var textboxes= MyBox_canvas.Children.OfType<TextBox>();
Also, you may want to give each TextBox a nema, to distinguish them later:
MyBox.Name = "MyTextBox"+i;
I have checkboxes named CheckBox1,CheckBox3,CheckBox5,CheckBox7,CheckBox9,CheckBox11. I wants to iterate through these check boxes and wants to disable them. I have written like this
for (int i=1; i < 12; i++)
{
((CheckBox)(i.ToString())).Enabled = false;
i=i+2;
}
but this is not the proper way. please help
Try using FindControl like this if you have id of controls like Checkbox1,Checkbox3 etc.
for (int i = 1; i < 12; i+=2)
{
CheckBox cb = (CheckBox)Page.FindControl("Checkbox" + i);
cb.Enabled = false;
}
Your code would like to set the enabled=false for checkboxes that have an odd number at the end of their id? Then this will do that
foreach (Control ctrl in Page.Controls)
{
if (ctrl is CheckBox && ctrl.id.length > 9)
{
int chkboxNumber = int.Parse(ctrl.id.SubStr(9));
if ( chkboxNumber % 2 == 1) // Check for odd numbers
{
(CheckBox)ctrl.Enabled = false;
}
}
}
May this help you.
foreach (Control c in Page.Controls)
{
if (c is CheckBox)
{
CheckBox myCheckBox = (CheckBox)c;
myCheckBox.Enabled = false;
}
}
This will disable you all CheckBoxes you have.
Edit: Sorry didnt see the +2 at the i. My fault.
I'm wondering how can I access an IEnumerable of all the elements in a page derived from PhoneApplicationPage?
If it was ASP.Net WebForms, it would be something like below:
foreach (Control control in this.Controls)
{
//Do something
}
But cannot find the equivalent in Windows Phone 7.5!
You can probably achieve this using the VisualTreeHelper class
Do something like:
LoopThroughControls(LayoutRoot);
private void LoopThroughControls(UIElement parent)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
string childTypeName = child.GetType().ToString();
LoopThroughControls(child);
}
}
}
i've 6 label controls in a form: label1, label2...label6.
How to 'refer' to the control in a loop like this:
for (i=1;i<=6;i++) {
label[i].text = ...;
}
Thank you
Try,
Label []labels={Label1,Label2,Label3};
Here's another way:
for (int n = 1; n < 4; n++)
{
Control[] Temp = Controls.Find("Label" + n, false);
Temp[0].Text = n.ToString();
}
Let's assume this is WinForms and that your "labels" are controls - the Form has a Controls property, which is a collection of controls associated with that container, so, we ought to be able to use Linq to query this, get the controls of the type we want, then iterate them, as such:
using System.Linq;
var labels = from control in Controls where control is Label select control;
for (i = 1; i <= controls.Count; i++)
{
labels[i].text = i.ToString();
}
A little rough, but you aren't very specific - it should be a decent starting point if nothing else.
EDIT:
OK, I thought I'd take the time to look into it, and Form.Controls doesn't like being used in Linq (in that straightforward way, at least), so as an alternative, this should help:
private List<Label> GetLabels()
{
var result = new List<Label>();
foreach (var control in Controls)
{
if (control is Label)
{
result.Add(control as Label);
}
}
return result;
}
The above method could even be factored in a genericised way rather simply; but then you can proceed:
var labels = GetLabels();
for (int i = 0; i <= labels.Count; i++)
{
labels[i].Text = i.ToString();
}
You can implement something like this:-
int y = 0;
int index = 0;
Label[] labels = new Label[6];
foreach (Student std in StudentList)
{
labels[index] = new Label();
labels[index].Text = std.Name;
labels[index].ForeColor = Color.Red;
labels[index].Location = new Point(0, y);
labels[index].Size = new Size(50, 12);
y = y + 10;
++index;
}
// Add the Label control to the form.
mPanel.Controls.AddRange(labels);