I just saw answer on another question for hiding all panels in form. So i wonder how to make exception to this code. This code is in C#.
foreach (Control c in this.Controls)
{
if (c is Panel) c.Visible = false;
}
I tried to add another if to check if c is my panel but that does not work.
if (c is MyPanel) continue;
MyPanl is name of my panel.
Error list say A constant value is expected
Can someone help?
From your comment you can try to use c == MyPanel to be the condition instead of c is MyPanel because ... is ... checked the type instead of compare instance.
foreach (Control c in this.Controls)
{
if (c == MyPanel) continue;
else if (c is Panel) c.Visible = false;
}
I would use linq where to set the condition to let the code clearer
var panels = this.Controls
.Cast<Control>()
.Where(c => c != MyPanel && c is Panel);
foreach (var c in panels)
{
c.Visible = false;
}
Related
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);
}
}
}
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.
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 VB code here and i wanna convert it into C# format, i'm new in C#. can anybody help me?
here is VB code:
Dim o As Object
For Each o In Me.Controls
If o.tag= "1" Then
o.BackColor = Color.Blue
End If
i want to modify an option in some of my form's controls with tag "1". how can i do that in C#?
That makes rather heavy use of vb.net's support for dynamic typing. Avoid using sample code like that.
foreach (Control ctl in this.Controls) {
if (ctl.Tag.ToString() == "1") {
ctl.BackColor = Color.Blue;
}
}
This will do what you want in C#
foreach (Control o in this.Controls)
{
if (o.Tag.ToString() == "1")
o.BackColor = Color.Blue;
}
for (int i = 0; i < Me.Controls.Count; i++)
{
var o = Me.Controls[i];
if (o.tag == "1")
o.BackColor = Color.Blue;
}
I'm creating some checkbox's from codebehind (adding through Panel.Controls.Add()).
My question is: How can i modify the values?
I've already tried creating the control, use the method FindControl and them change some properties but with no sucess.
CheckBox c = new CheckBox();
c.FindControl("CheckBoxP");
c.Checked = true;
Any ideas? Thanks
CheckBox _C = (CheckBox)this.Controls.Find("checkBox1", true).FirstOrDefault();
if (_C != null)
{
_C.Checked = true;
}
replace the 'checkBox1' with the name of the desired control
Try something like this (assuming you're using Windows Forms):
foreach (Control c in this.Controls)
{
if (c.Name == "MyName" && c is CheckBox)
{
((CheckBox)c).Checked = true;
}
}