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;
}
}
Related
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;
}
I'm just wondering really.
I have series of if statements that check
if textboxes are empty (or have results strings) after i pass SQL
results to then
.
if (IncidentData.Tables[0].Rows[0]["Property Category"].ToString()
== "RoadVehicle")
{
lbl_alarmOperated.Visible = false; tb_alarmOperated.Visible = false;
}
else
{
lbl_alarmOperated.Visible = true;
tb_alarmOperated.Visible = true;
}
I have been looking into controls and seeing if i can do a check on all textboxes and hide them if they are empty (instead of writing loads of if statements)
i have this at the moment:
public void ChecknHide()
{
HideTextBoxes(this);
}
protected void HideTextBoxes(Control ctrl)
{
foreach (var c in ctrl.Controls)
{
if (c is TextBox) ((TextBox)c).Text = String.Empty;
{
((TextBox)c).Visible = false;
}
}
}
Its mostly put together from reading posts on here. But I've ran into an issue. When i compile and go to view the page i get this:
Unable to cast object of type 'ASP.masterpage_master' to type
'System.Web.UI.WebControls.TextBox'.
Any ideas whats going wrong?
The statement after the if isn't part of the condition. This causes all controls to be casted to a TextBox. You should be able to fix it like so:
protected void HideTextBoxes(Control ctrl)
{
foreach (var c in ctrl.Controls)
{
if (c is TextBox && ((TextBox)c).Text == String.Empty)
{
((TextBox)c).Visible = false;
}
}
}
Weird code line:
if (c is TextBox) ((TextBox)c).Text = String.Empty;
Try something like:
protected void HideTextBoxes(Control ctrl)
{
//Iterate over controlls
foreach (var c in ctrl.Controls)
{
//Check for Textbox controls with the .Text property equal to Null or Empty.
if (c is TextBox && string.IsNullOrEmpty(((TextBox)c).Text))
{
//Set visibility of Textbox control to invisible.
((TextBox)c).Visible = false;
}
}
}
You're checking if c is a TextBox, but then trying to cast c as a TextBox and set it to String.Empty in the same line, regardless of whether it actually is a TextBox.
if (c is TextBox) ((TextBox)c).Text = String.Empty;
Hello all I am slowly learning c#, and I have this problem:
I want to make a function that sets all panels not visible and then set one as visible given to me from a string variable.
public void setMeVisible(string PanelName)
{
PageMainScreen.Visible = false;
PageNewRegistration.Visible = false;
PageSelectedPatient.Visible = false;
["PanelName"].Visible = true; // in this line I want to set the value of
// PanelName.Visible=true.
// what is the format that I must put PanelName?
}
For Windows, The simplest way is loop through all the controls and check if its a panel and set it to false. In the same loop you can check panel.Name for the pass in "PanelName" and set it to true. Something like this
foreach(Control control in form.Controls)
{
if( control is Panel )
{
control.Visible = false;
if(control.Name == "PanelName")
control.Visible = true;
}
}
Use Page.FindControl for ASP.Net:
var control = FindControl(PanelName);
if (control != null)
control.Visible = true;
If your control is nested, you can create a FindControlRecursive function, suggested by this answer:
private Control FindControlRecursive(Control root, string id)
{
return root.ID == id
? root
: (root.Controls.Cast<Control>()
.Select(c => FindControlRecursive(c, id)))
.FirstOrDefault(t => t != null);
}
Then call it by doing:
var control = FindControlRecursive(form1, PanelName); // Or another top-level control other than form1
if (control != null)
control.Visible = true;
Use Controls.Find for a windows form application:
var control = Controls.Find(PanelName, true).FirstOrDefault();
if (control != null)
control.Visible = true;
Looping and finding controls is not really necessary. You should add all your panels to a same container, use the Controls collection and pass in the name to get the control:
public void setMeVisible(string PanelName) {
PageMainScreen.Visible = false;
PageNewRegistration.Visible = false;
PageSelectedPatient.Visible = false;
Control c = sameContainer.Controls[PanelName];
if(c != null) c.Visible = true;
}
If each time there is only 1 panel visible, you should use some variable to track the current shown panel and hide only this (instead of all controls as you did) like this:
Control currentShown;
public void setMeVisible(string PanelName) {
Control c = sameContainer.Controls[PanelName];
if(c != null) {
c.Visible = true;
if(currentShown != null) currentShown.Visible = false;
currentShown = c;
}
}
And the last, if you don't want to use the same container for all your panels. You should declare some List<Panel> to contain all your panels, then you can navigate through them easily:
List<Panel> panels = new List<Panel>();
panels.AddRange(new[]{PageMainScreen, PageNewRegistration, PageSelectedPatient});
public void setMeVisible(string PanelName) {
var c = panels.FirstOrDefault(panel=>panel.Name == PanelName);
if(c != null) {
c.Visible = true;
if(currentShown != null) currentShown.Visible = false;
currentShown = c;
}
}
NOTE: Don't try complicating your UI unnecessarily. I want to mean that you should place all your panels on the same container (such as your form). That's the way we do, that way you can use the first approach, no need to loop, easy to maintain. You should also consider the Dock and the methods like BringToFront() and SendToBack() to show/hide the view.
I have a program written in C# with a clear button, which needs to clear the content of the entire form. For the button I used on foreach for the radiobuttons and one for the checkboxes.
foreach (RadioButton rad in quiztabs.TabPages)
{
rad.Checked = false;
}
foreach (CheckBox chk in quiztabs.TabPages)
{
chk.Checked = false;
}
However, when I click the clear button I get an Unable to cast object of type System.Windows.Forms.TabPage to type System.Windows.Forms.RadioButton.
You misunderstand the behaviour of foreach: It won't filter all elements in TabPages, it will try to cast all of them.
If you want to filter, you can do so explicitly using LINQ:
foreach (RadioButton rad in quiztabs.TabPages.OfType<RadioButton>())
{
rad.Checked = false;
}
foreach (CheckBox chk in quiztabs.TabPages.OfType<CheckBox>())
{
chk.Checked = false;
}
However, this still won't solve your problem, since the TabPages collection only contains TabPage elements. What you probably want is something like this:
foreach (TabPage page in quiztabs.TabPages)
{
foreach (RadioButton rad in page.Controls.OfType<RadioButton>())
{
rad.Checked = false;
}
foreach (CheckBox chk in page.Controls.OfType<CheckBox>())
{
chk.Checked = false;
}
}
Use the OfType<T>() method:
foreach (RadioButton item in yourChildern.OfType<RadioButton>())
{
//your code
}
Use
quiztabs.TabPages.OfType<RadioButton>() and quiztabs.TabPages.OfType<CheckBox>() instead of quiztabs.TabPages in your code.
Alternativly;
foreach(Control c in this.Controls)
{
if(c is RadioButton)
{
c.Checked = false;
}
}
foreach(Control i in this.Controls)
{
if(i is CheckBox)
{
i.Checked = false;
}
}
Your problem is that .TabPages is a list of Tab Pages... I believe you want the controls on each tab page
foreach(TabPage t in quizTabs.TabPages)
{
foreach (RadioButton rad in t.Controls)
{
rad.Checked = false;
}
foreach (CheckBox chk in t.Controls)
{
chk.Checked = false;
}
}
The foreach needs to have an iterator (rad here) of the same type of whatever you want to iterate through (TabPages here):
So it would look something like:
foreach (TabPage tab in quiztabs.TabPages) {
Although TabPages would be whatever type of object that quiztabs.TabPages is.
Hope this makes sense!
Take one List<> of type RadioButton and all radiobutton in that list .
List<RadioButton> list = new List<RadioButton>();
list.Add(radioButton1);
list.Add(radioButton2);
list.Add(radioButton3);
foreach (RadioButton item in list)
{
if(tabControl1.SelectedTab==tabControl1.TabPages[tabControl1.SelectedIndex])
item.Checked = false;
}
because, tabs in not a radioButton, you should do this instead,
foreach(TabPage tp in quizTabs.TabPages)
{
foreach (RadioButton rad in tp.Controls.OfType<RadioButton>())
{
rad.Checked = false;
}
foreach (CheckBox chk in tp.Controls.OfType<CheckBox>())
{
chk.Checked = false;
}
}
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;
}