I have 10 RadioButton inside a panel.
I have 10 panels inside a tableLayoutPanel, Each one in different column.
How can i move between the columns and validate that in each column there is a selected radioButton?
Thank you.
I have no experiences with the TableLayoutPanel, but you could try this:
bool allValid = true;
for(int c = 0; c < panel.ColumnCount; c++)
{
var colRadios = panel.Controls.OfType<RadioButton>()
.Where(rb => panel.GetColumn(rb) == c);
bool colValid = colRadios.Any(rb => rb.Checked);
if(!colValid)
{
allValid = false;
break;
}
}
(panel is the TableLayoutPanel)
Related
I have nine labels with the names "lbl101", "lbl102", ...
I want to do this:
for (int i = 0; i < 9; i++)
{
sting name = "lbl10" + i;
name.BackColor = Color.Red;
}
How can I do this?
You can add the controls to a collection, and loop through that.
var labels = new List<Label> { lbl101, lbl102, lbl103 };
foreach (var label in labels)
{
label.BackColor = Color.Red;
}
Alternatively, if you just want every Label on the Form that starts with "lbl10", you can use LINQ to query the collection of controls:
var labels = this.Controls.OfType<Label>()
.Where(c => c.Name.StartsWith("lbl10"))
.ToList();
If labels are set on the form, you can use Linq:
var labels = Controls // or MyPanel.Controls etc. if labels are on panel
.OfType<Label>()
.Where(label => label.Name.StartsWith("lbl10"));
foreach (var label in labels)
label.BackColor = Color.Red;
Loop through the container they are in and grab a reference to them.
for(int i = 0; i<9; i++)
{
var label = (Label)yourForm.FindControl("lbl10" + i.ToString());
label.BackColor = Color.Red;
}
Probably the simplest thing would be to list them all out:
lbl100.BackColor = Color.Red;
lbl101.BackColor = Color.Red;
lbl102.BackColor = Color.Red;
lbl103.BackColor = Color.Red;
lbl104.BackColor = Color.Red;
lbl105.BackColor = Color.Red;
lbl106.BackColor = Color.Red;
lbl107.BackColor = Color.Red;
lbl108.BackColor = Color.Red;
This is the most straightforward way to do it. If you really want to be fancy, you could put them all in an array and iterate over that:
Label[] labels = new Label[]{
lbl100, lbl101, lbl102, lbl103, lbl104, lbl105, lbl106, lbl107, lbl108
};
for (int i = 0; i < labels.Length; i++)
{
labels[i].BackColor = Color.Red;
}
Or, if you know that all the labels are children of a certain control and there are no other labels in that control, you could do this:
foreach (Control c in someControl.Controls)
{
if (c is Label)
{
((Label)c).BackColor = Color.Red;
}
}
public void changebackground()
{
Label mylabel;
foreach (Control con in this.Controls)
{
if (con.GetType() == typeof (Label)) //or any other logic
{
mylabel = (Label)con;
mylabel.BackColor = Color.Red;
}
}
}
In Windows Form, to point to a control, just call it with the following sentence
this.Controls[control name]
for example
this.Controls["label1"].BackColor = Color.Blue;
So the answer to your question
for (int i = 0; i < 9; i++)
{
this.Controls["lbl10" + i.ToString()].BackColor = Color.Red;
}
I have the WinForm, there I have a lot of controls, and on certain moments I need to change the properties for some of them.. so, I create the Control array and determines what should be changed
controls = new Control[] {loadFromFile_btn, logout_btn, postBtn, waitFrom_tb, waitTo_tb, messageTB, recurs_check};
ChangeStatus.activStatus(controls);
Then in my class ChangeStatus make changes to all of these elements are in an array
public static void activStatus(Control[] controlObj)
{
for (int i = 0; i < controlObj.Count() - 1; i++)
{
controlObj[i].BeginInvoke((Action)delegate
{
if (controlObj[i] is TextBox || controlObj[i] is CheckBox || controlObj[i] is Panel)
controlObj[i].Enabled = true;
else
{
controlObj[i].BackColor = Color.DarkGray;
controlObj[i].Enabled = true;
}
});
}
}
But I have a problem... the change applies only to the last element in the array. Help me please..
That's because of the closure.Try storing i in a local variable and use it in your anonymous method
for (int i = 0; i < controlObj.Count() - 1; i++)
{
int j = i;
controlObj[i].BeginInvoke((Action)delegate
{
if (controlObj[j] is TextBox || controlObj[j] is CheckBox || controlObj[j] is Panel)
controlObj[j].Enabled = true;
else
{
controlObj[j].BackColor = Color.DarkGray;
controlObj[j].Enabled = true;
}
});
}
I have a menustrip consists of Menu and Tools
in "Menu" i have subMenus like msO1,msO2,msO3......., and on "Tools" i have subMenus like msP1,msP2,msP3.......,
on Form load all the subMenus visible is false..., On button Click user want to select which subMenus he want...,
in the textBox(txtSelect) if user enter 1,3..., he get msO1, msO3.....,
my code is a hardcode..., if i have 20 subMenus means this code is not helpfull anybody have an idea...,
private void btnSelect_Click_1(object sender, EventArgs e)
{
msO1.Visible = false;//msO1 is a submenu
msO2.Visible = false;
msO3.Visible = false;
msP1.Visible = false;
msP2.Visible = false;
msP3.Visible = false;
string word = txtSelect.Text;
string[] splt = word.Split(',');
int[] arrayItms = new int[splt.Length];
for (int x = 0; x < splt.Length; x++)
{
arrayItms[x]=Convert.ToInt32(splt[x].ToString());
if (splt.Length > 0)
{
switch (arrayItms[x])
{
case 1:
msO1.Visible = true; break;
case 2:
msO2.Visible = true; break;
case 3:
msO3.Visible = true; break;
case 4:
msP1.Visible = true; break;
case 5:
msP2.Visible = true; break;
case 6:
msP3.Visible = true; break;
}
}
}
}
Create an array of your MenuStrip
MenuStrip[] mstrip = new MenuStrip[]
{
msO1,msO2, msO3, msP1, msP2, msP3 // add other menus here when needed
};
now you could work on the array as a Whole to make visible or not your menus
for(int x = 0; x < menus.Length; x++)
mstrip[x].Visible = false;
and your code could be simplified with
for (int x = 0; x < splt.Length; x++)
{
int menuIndex;
if(Int32.TryParse(splt[x], out menuIndex))
{
menuIndex--;
if(menuIndex >= 0 && menuIndex < mstrip.Length)
mstrip[menuIndex].Visible = true;
}
}
Remember, arrays indexes start at zero (while your user will probably start counting a 1).
You could use something like this
string word = txtSelect.Text;
string[] splt = word.Split(',');
for (int x = 0; x < splt.Length; x++)
{
Control myControl1 = FindControl("ms" + splt[x]);
if ( myControl1 != null )
(ToolStripMenuItem)myControl1.Visible = true;
}
Untested but this should get you half way there I hope.
Loop through each ToolStripMenuItem control in the menu strip items and set them to visible.
You can add further conditions inside the loop to define which of the menu items should be made visible based on the users choice..
foreach (ToolStripMenuItem mi in menuStrip1.Items)
{
mi.Visible = true;
}
I have ten labels on a page. I want to make these invisible in a for loop on page load.
I have tried this (doesn't work):
for (int i = 0; i < 10; i++)
{
my_lbl+i.Visible = false;
}
Therefore, it should do:
my_lbl1.Visible = false;
my_lbl2.Visible = false;
my_lbl3.Visible = false;
my_lbl4.Visible = false;
etc...
Is there a way to do this?
Put all of the labels into a collection:
private List<Label> labels = new List<Label>{my_lbl1, my_lbl2, my_lbl3, my_lbl4};
Then you can iterate the whole collection:
foreach(var label in labels)
label.Visible = false;
Make a List of them;
List<Label> yourlabels = new List<Label>{my_lbl1, my_lbl2, my_lbl3...};
and use foreach loop making them visible.
foreach(var label in yourlabels)
{
label.Visible = false;
}
I don't know if there is a better way but this way seems logical to me.
Putting the labels in a collection (as the previous answers have suggested) is a great solution. You can also retrieve the controls by their name using FindControl method of the Page.
for (int i = 0; i < 10; i++)
{
this.FindControl("my_lbl" + i.ToString()).Visible = false;
}
I guess you can utillize Page's FindControl method:
for (int i = 0; i < 10; i++)
{
FindControl(string.Format("my_lbl{0}", i)).Visible = false;
}
But check the case if control is not found of course.
Or, you can put them into dictionary:
Dictionary<string, Label> nameOfDict = new Dictionary<string, Label>();
nameOfDict.Add("label1", label1);
nameOfDict.Add("label2", label2);
For...
nameOfDict ["label" + incrementator].visible = false;
Or, create them dynamically into an array of labels.
If you're sure that, let's say, you want to uncheck all checkboxes in a groupbox, you can do this, too:
foreach (var item in groupBox1.Controls)
{
if (item.GetType() == typeof(CheckBox))
{
((CheckBox)item).Checked = true;
}
}
with LINQ:
foreach (var item in groupBox1.Controls.Cast<object>().Where(item => item.GetType() == typeof(CheckBox)))
{
((CheckBox)item).Checked = true;
}
I've created a number of buttons on a form based on database entries, and they work just fine. Here's the code for creating them. As you can see I've given them a tag:
for (int i = 0; i <= count && i < 3; i++)
{
btnAdd.Text = dataTable.Rows[i]["deviceDescription"].ToString();
btnAdd.Location = new Point(x, y);
btnAdd.Tag = i;
this.Controls.Add(btnAdd);
}
I use these buttons for visualising a polling system. For example, I want the button to be green when everything is fine, and red when something is wrong.
So the problem I'm running into is referencing the buttons later so that I can change their properties. I've tried stuff like the following:
this.Invoke((MethodInvoker)delegate
{
// txtOutput1.Text = (result[4] == 0x00 ? "HIGH" : "LOW"); // runs on UI thread
Button foundButton = (Button)Controls.Find(buttonNumber.ToString(), true)[0];
if (result[4] == 0x00)
{
foundButton.BackColor = Color.Green;
}
else
{
foundButton.BackColor = Color.Red;
}
});
But to no avail... I've tried changing around the syntax of Controls.Find() but still have had no luck. Has anyone encountered this problem before or know what to do?
If you name your buttons when you create them then you can find them from the this.controls(...
like this
for (int i = 0; i <= count && i < 3; i++)
{
Button btnAdd = new Button();
btnAdd.Name="btn"+i;
btnAdd.Text = dataTable.Rows[i]["deviceDescription"].ToString();
btnAdd.Location = new Point(x, y);
btnAdd.Tag = i;
this.Controls.Add(btnAdd);
}
then you can find it like this
this.Controls["btn1"].Text="New Text";
or
for (int i = 0; i <= count && i < 3; i++)
{
//**EDIT** I added some exception catching here
if (this.Controls.ContainsKey("btn"+buttonNumber))
MessageBox.Show("btn"+buttonNumber + " Does not exist");
else
this.Controls["btn"+i].Text="I am Button "+i;
}
Put these buttons in a collection and also set the name of the Control rather than using its tag.
var myButtons = new List<Button>();
var btnAdd = new Button();
btnAdd.Text = dataTable.Rows[i]["deviceDescription"].ToString();
btnAdd.Location = new Point(x, y);
btnAdd.Name = i;
myButtons.Add(btnAdd);
To find the button use it.
Button foundButton = myButtons.Where(s => s.Name == buttonNumber.ToString());
Or Simply
Button foundButton = myButtons[buttonNumber];
In your case I would use a simple Dictionary to store and retrieve the buttons.
declaration:
IDictionary<int, Button> kpiButtons = new Dictionary<int, Button>();
usage:
Button btnFound = kpiButtons[i];
#Asif is right, but if you really want to utilize tag you can use next
var button = (from c in Controls.OfType<Button>()
where (c.Tag is int) && (int)c.Tag == buttonNumber
select c).FirstOrDefault();
I'd rather create small helper class with number, button reference and logic and keep collection of it on the form.