Before opening the form I used following code to check if its label then change the font
foreach (Label ctl in frm.Controls)
{
ctl.Font = usefontgrid;
}
But on first line return error because it check other control types such as textbox or button,etc.
How can I check if the object is only label then go to for each?
Try this;
foreach (Control c in this.Controls)
{
if (c is Label)
c.Font = usefontgrid;
}
Or
foreach (var c in this.Controls.OfType<Label>())
{
c.Font = usefontgrid;
}
Its not clear where you place this code (should be after initialize component) but try
foreach (Label ctl in frm.Controls.OfType<Label>())
{
ctl.Font = usefontgrid;
}
There is also the following Linq to do the same thing
foreach (Label ctl in frm.Controls.Where(x => x is Label))
try this.
foreach (Control ctl in frm.Controls)
{
if(ctl.GetType()==typeof(Label)){
ctl.Font = usefontgrid;
}
}
frm.controls will give all controls
You need to check whether the control is a type of Label.
Related
I have 2 listviews in tab page. however I am looking for a function to find the right control by name:
I have
foreach (Control c in form.Controls) // loop through form controls
{
if (c is TabControl)
{
TabControl f = (TabControl)c;
foreach (Control tab in f.Controls)
{
TabPage tabPage = (TabPage)tab;
foreach (Control control in tabPage.Controls)
{
MessageBox.Show(control.Name);
// code to go here
}
}
}
}
The Controls collection has a Find function that returns an array:
Control[] ctrls = this.Controls.Find("listView1", true);
if (ctrls.Length == 1) {
MessageBox.Show("Found " + ctrls[0].Name);
}
This will search for a control with the specified name in the controls of the specified control and all his sons.
public Control findControlbyName(String name, Control parent){
foreach (Control ctr in parent.Controls)
{
if (ctr.Name.Equals(name)) return ctr;
else return findControlbyName(name, ctr);
}
return null;
}
You just need to do:
findControlbyName("NameOfTheListView",this);
I was wondering why below did not work as expected...
if if-statement changed to (!ctrl.checked) it returns all radio-button's names)
myForm f = new myForm();
foreach (RadioButton ctrl in f.Controls.OfType<RadioButton>())
{
if (ctrl.Checked)
MessageBox.Show(ctrl.Name);
}
I also tried
foreach (Control c in f.controls)
if (c is radiobutton)
{
if (c.Checked)
{
messagebox.show(c.name);
}
when I then put all radio-buttons in a group-box and used below code:
foreach (RadioButton c in groupBox1.Controls)
{
if (c.Checked)
{
MessageBox.Show(c.Name);
}
}
it worked fine.
what's the difference here.
any help appreciated
I'm guessing that your radio button is the child of a control other than the form. You need to recursively search for the radio buttons.
public void DisplayRadioButtons()
{
Form f = new Form();
RecursivelyFindRadioButtons(f);
}
private static void RecursivelyFindRadioButtons(Control control)
{
foreach (Control childControl in control.Controls)
{
RecursivelyFindRadioButtons(childControl);
if (childControl is RadioButton && ((RadioButton) childControl).Checked)
{
MessageBox.Show(childControl.Name);
}
}
}
Basically what I do is: Add (n) textboxes via a button click. When I again click on it, this code runs:
foreach (Control c in this.Controls)
{
TextBox tb = c as TextBox;
if (tb != null)
{
this.Controls.Remove(tb);
tb.Dispose();
}
}
I than again add (n) textboxes, but every second item from the old textboxes remains. Any ideas?
Removing items from the collection you are iterating on is a bad idea. Try this:
List<Control> toBeRemoved = new List<Control>();
foreach (Control c in this.Controls)
{
if (c instanceof TextBox)
{
toBeRemoved.Add(c);
}
}
foreach (Control c in toBeRemoved)
{
this.Controls.Remove(c);
c.Dispose();
}
I have a C# form with some types of controls, i throught this code for loop all Labels and re-parent:
private void MakeAllLabelTrans(Control frm, Control parent)
{
foreach (Control ctl in frm.Controls)
{
if (ctl is Label)
{
ctl.Parent = parent;
// ctl.BackColor = Color.Transparent;
}
else if (ctl.HasChildren)
{
MakeAllLabelTrans(ctl, parent);
}
}
}
and call as: MakeAllLabelTrans(this, picBackground); in Form_Load event, but some label was missed (i have puted a messagebox in the loop body - it really not in the loop), but i don't know why?
You're changing the collection while enumerating it. This leads to some items being skipped.
You should rewrite this to first build a list of controls that will need to be reparented, and then reparent them in a second pass.
You should not modify the collection on which you are iterating.
private static IEnumerable<Labels> FindAllLabels(Control container)
{
foreach (Control ctl in container.Controls)
{
var lbl = ctl as Label;
if (lbl != null)
{
yield return ctl;
}
else if (ctl.HasChildren)
{
foreach (var innerResult in FindAllLabels(ctl))
yield return innerResult;
}
}
}
// now call the method like this if you want to change a property on the label
foreach (var label in FindAllLabels(theForm))
label.BackgroundColor = Color.White;
// or like this (note the .ToList()) if you want to move the labels around:
foreach (var label in FindAllLabels(theForm).ToList())
label.Parent = someOtherControl;
I'm needing to change a bunch of properties in a large amount of controls. I'm having trouble getting it to work. Am I on the right track?
foreach(var c in this.Controls.OfType<Label>())
{
c.Text = "test";
}
What's happening is var c is just creating a new object and not editing the existing one. How can I access the real control?
you can try this
List<Control> controls = Controls.OfType<Label>().Cast<Control>().ToList();
foreach (Control m in controls)
{
m.Text = "test";
}
Try the following:
foreach(var c in this.Controls)
{
var label = c as Label;
if(label != null) label.Text = "test";
}