On my Visual C# Form application, I have a combobox inside a groupbox to help organize / look neat. However, once I put the combobox inside the groupbox, I am no longer able to find it by looping through all of the controls on my form.
For example, if I run this code with the Combobox inside the Groupbox I get a different result than if its outside the group box:
foreach (Control contrl in this.Controls)
{
richTextBox1.Text += "\n" + contrl.Name;
}
If the combobox is inside the groupbox, it won't find it.
I also noticed in the Form1.Designer.cs file that whenever I add the combobox inside the groupbox, the following line of code appears to the groupbox:
this.groupBox4.Controls.Add(this.myComboBox);
..
this.groupBox4.Location = new System.Drawing.Point(23, 39);
this.groupBox4.Name = "groupBox4";
... etc...
And this line will be removed:
this.Controls.Add(this.myComboBox);
If I try to edit it manually, it automatically switches back once I move the combobox back inside the groupbox.
Any help would be appreciated! Thanks!
Brian
As you said, you added combo box to group box, so it is added to Controls collection of group box and the designer generates this code:
this.groupBox4.Controls.Add(this.myComboBox);
So if you want to find the combo box programmatically, you can use this options:
Why not simply use: this.myComboBox ?
Use var combo = (ComboBox)this.Controls.Find("myComboBox", true).FirstOrDefault();
Use var combo = (ComboBox)this.groupBox4.Controls["myComboBox"]
Also if you want too loop, you should loop over this.groupBox4.Controls using:
foreach(Control c in this.groupBox4.Controls) {/*use c here */}
this.groupBox4.Controls.Cast<Control>().ToList().ForEach(c=>{/*use c here */})
Just like the Form object, the Group object can hold a collection of controls. You would need to iterate through the Group control's controls collection.
One more idea for getting at all or one ComboBox in a GroupBox, in this case groupBox1. Granted Resa's suggestion for using Find with FirstOrDefault would be best to access one combobox.
List<ComboBox> ComboBoxes = groupBox1
.Controls
.OfType<ComboBox>()
.Select((control) => control).ToList();
foreach (var c in ComboBoxes)
{
Console.WriteLine(c.Name);
}
string nameOfComboBox = "comboBox1";
ComboBox findThis = groupBox1
.Controls
.OfType<ComboBox>()
.Select((control) => control)
.Where(control => control.Name == nameOfComboBox)
.FirstOrDefault();
if (findThis != null)
{
Console.WriteLine(findThis.Text);
}
else
{
Console.WriteLine("Not found");
}
You can use the ControlCollections Find Method, it has a parameter that will search the parent and its Children for your control.
ComboBox temp;
Control[] myControls = Controls.Find("myComboBox", true); //note the method returns an array of matches
if (myControls.Length > 0) //Check that it returned a match
temp = (ComboBox)myControls[0]; //use it
Related
I using gecko browser and i need select a specific listbox or combobox but same page have in more than one listbox and combobox. I try the following method but it applies to all. And there is no id tag, just a name tag.
GeckoElementCollection ListeBoxKomboBox = Tarayıcı.Document.GetElementsByTagName("option");
foreach (GeckoHtmlElement Element in ListeBoxKomboBox)
{
if (Element.GetAttribute("value") == "1")
{
Element.SetAttribute("selected", "selected");
}
if (Element.GetAttribute("value") == "2")
{
Element.SetAttribute("selected", "selected");
}
}
I do not want you to pick the items with the same value in other boxes. Is this like solution available for gecko?
I notice that there is a label tag ('Turu' or something:)).
So, you can determine which select box is the proper one by:
Select the LI element that has got a first child which content is 'Turu'
Then selecting the 'combobox' inside that LI element
Notice also, that this code is not really right:
GeckoElementCollection ListeBoxKomboBox = Tarayıcı.Document.GetElementsByTagName("option");
You are getting a collection of ALL options in ALL comboboxes on the page. So, the combo box is actually a parent of the option elements (the select element).
Also, option tags are GeckoOptionElements (can be casted safely),
so you can do:
var optionElements= selectBox.GetElementsByTagName("option");
foreach (GeckoOptionElement optionElement in optionElements)
{
if (optionElement.Value == "Foo")
{
optionElement.Selected = true;
}
}
Lastly - yes, the solution like in your link is possible in Gecko.
I have a form which contains more than 4 group boxes.Each group box has some text fields.What I am seeking is to get values from only that group box which is enabled. So my question is : Is it possible to scan all the available group boxes to find out if any of them is enabled and if one of them is enabled get and save values from only that group box into the database?
You can try using Linq; providing that GroupBoxes are placed directly on the form and TextBox of interest are directly on their GroupBoxes:
string[] values = Controls
.OfType<GroupBox>()
.Where(box => box.Enabled)
.SelectMany(box => box
.Controls
.OfType<TextBox>()
.Select(tb => tb.Text))
.ToArray();
You can scan the controls like this:
GroupBox gBox = this.Controls.OfType<GroupBox>().FirstOrDefault(c => c.Enabled);
List<string> values = new List<string>();
if(gBox != null)
{
foreach(var txtBox in gBox.Controls.OfType<TextBox>())
{
values.Add(txtBox.Text);
}
}
Note this assumes that the GroupBoxes are added directly to the form, and not to any panel. Also the TextBoxes are added directly to the GroupBox with no additional panels.
Alternatively, you can do it in one go:
List<string> result = this.Controls.OfType<GroupBox>()
.Where(gBox => gBox.Enabled)
.SelectMany(gBox => gBox.Controls.OfType<TextBox>())
.Select(txtBox => txtBox.Text).ToList();
I have added a GroupBox to my main Grid and am populating it dynamically with controls. I need to get a specific textbox within that GroupBox in an onClick event. I am able to loop through the GroupBox and fine, like this...
foreach (Control ctl in ((Grid)gpMccEngineProperties.Content).Children)
{
if (ctl.GetType() == typeof(TextBox))
{
TextBox textbox = (TextBox)ctl;
PropertyValue propertyValue = new PropertyValue();
propertyValue.Value = textbox.Text;
}
}
... but if i just want to access a specific TextBox i keep coming back with a null value. here is how i'm trying to get it...
TextBox txt = ((Grid)gpMccEngineProperties.Content).Children.OfType<TextBox>().Where(t => t.Name == "PropertyId_9") as TextBox;
... where PropertyId_9 is the name of a textbox that i added dynamically to the GroupBox. Any idea how i get that textbox so i can get it's value?
Thanks!
You're using the wrong Linq method. That code returns an IEnumerable of TextBoxes, not just the TextBox. Use Single or SingleOrDefault instead of Where:
TextBox txt = ((Grid)gpMccEngineProperties.Content).Children.OfType<TextBox>().Single(t => t.Name == "PropertyId_9");
I've a Listbox but when I say :
txtSelectedTables.Text += lbxTafels.GetItemText(lbxTafels.SelectedValue);
It only shows one thing in my textbox (even though I've selected multiple rows). So if I want to select multiple rows it doesn't put the values in txtSelectedTables.Text (it only shows one item).
So how can I select multiple rows and show it in a textbox.
Like RadioSpace mentioned you probably want to look at the selectedItems property
Here is an example
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Here is my full test function. If you are seeing a DataViewRow type name in the text box then maybe you have more going on than a ListBox and TextBox.
ListBox lbxTafels = new ListBox();
System.Windows.Forms.TextBox txtSelectedTables = new TextBox();
//Add 2 items
lbxTafels.Items.Add("Hello");
lbxTafels.Items.Add("World");
//Select both items
lbxTafels.SetSelected(0,true);
lbxTafels.SetSelected(1, true);
//Set the textbox
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Console.WriteLine(txtSelectedTables.Text);
The above function prints out
HelloWorld
I have a form with few radio buttons that suppose to represent different integer values.
1) Is there a built-in way to put a value "behind" a radio button?
an example:
RadioButton rad = new RadioButton();
rad.Value = 5; // This is what im trying to achieve.
2) I have few RadioButtons that are logically related to each other and i want to know which one was selected (without putting them inside a GroupBox control) and get the selected value for that RadioButton.
note: I would like to "Iterate" these 3 controls and get the selected one instead of doing 3 IF statements.
an example:
RadioButton radColor1 = new RadioButton();
RadioButton radColor2 = new RadioButton();
RadioButton radColor3 = new RadioButton();
// ...
RadioButton radSelected = ?? // Get selected Radio Button from the 3 above.
//here i can get to radSelected.Value
If you want to assign an arbitrary value to the control, use its Tag property:
radColor1.Tag = 5;
But why can't you simply use the Checked property?
if (radColor1.Checked)
//do something
if (radColor2.Checked)
//do something else
//...however many more you need to check
EDIT: iteration...
foreach (Control c in this.Controls)
{
if (c is RadioButton)
{
if (((RadioButton)c).Checked)
//do something
}
}
put these radio btns in a stackPanel and iterate to check If Checked & than use tag property to store the values of each radio button
foreach(var child in stack.Children)
{
if((child as RadioButton).Checked == true)
var value = (child as RadioButton).tag;
}
RadioButton radSelected = (from RadioButton rb in this.Controls
where rb.Checked
select rb).SingleOrDefault();