Reputation: 31
I tried :
var comboBoxes = this.Controls
.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("comboBox"));
foreach (var cmbBox in comboBoxes)
{
CMBXWMICLASSES.AddRange(cmbBox.Items[
}
But inside the loop the foreach how do I add the Items of each ComboBox ?
This is a working code:
In the top of form1 i did:
List<string> CMBXWMICLASSES = new List<string>();
Then i added a method that make recursively process all controls and their children:
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
Then adding the items from each ComboBox in the constructor:
var c = GetAll(this, typeof(ComboBox));
foreach (ComboBox cc in c)
{
foreach (string ccc in cc.Items)
{
CMBXWMICLASSES.Add(ccc);
}
}
Upvotes: 1
Views: 5527
Reputation: 430
I have tried all solutions with foreach and it raise an exception when casting. If you want to get all the strings in the ComboBox, the only code that worked for me is:
for (int i = 0; i < myComboBox.Items.Count; i++)
{
string value = myComboBox.GetItemText(myComboBox.Items[i]);
}
Upvotes: 1
Reputation: 43
Try this.
List<string> ItemsList = new List<string>();
foreach (Control c in cntrls.Controls) // you can change cntrls.Controls to your container or if its the form that holds the combobox then use this.Controls
{
if (c is ComboBox) // check if control is checkbox
{
ComboBox cmb = c as ComboBox
foreach(var comboitems in cmb ) // add combobox items to list
{
ItemsList.Add(comboitems.ToString());
}
}
}
Upvotes: 0
Reputation: 864
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
using System.Collections;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (var result in this.Controls.OfType<ComboBox>())
{
foreach (var values in Enumerable.Range(0, 10))
{
result.Items.Add(values);
}
result.Items.Add(result.Name);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.AddComboItemsToList();
}
private void AddComboItemsToList()
{
var comboEntries = this.GetComboBoxItems(this.Controls, "comboBox");
foreach (var comboEntry in comboEntries)
{
this.listBox1.Items.Add(comboEntry);
}
}
private IEnumerable<object> GetComboBoxItems(Control.ControlCollection controls, string cmbName)
{
var cmbs = controls.OfType<ComboBox>().Where(x => x.Name.StartsWith(cmbName));
var ret = new List<object>();
foreach (var cmbBox in cmbs)
{
ret.AddRange(cmbBox.Items.Cast<object>());
}
return ret;
}
}
}
Upvotes: 0
Reputation: 24
foreach (ComboBox box in _boxList)
{
ComboBox.ObjectCollection _items = box.Items;
foreach (var item in _items)
{
_itemsList.Add(item);
}
}
Upvotes: 0
Reputation: 222532
Try this,
List<string> Items = new List<string>();
var comboBoxes = this.Controls
.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("comboBox"));
foreach (var cmbBox in comboBoxes)
{
foreach (var cmbitem in cmbBox.Items)
{
Items.Add(cmbitem.ToString());
}
}
Upvotes: 5