Combo Boxes and Controls

S

syslock

I have an application that has 6 combo boxes in a groupbox. When one combo
box is selected and it's text properties are displayed, I want to be able to
reset the other combo boxes text property. How can this be achieved??
 
F

Family Tree Mike

I have an application that has 6 combo boxes in a groupbox. When one
combo box is selected and it's text properties are displayed, I want to
be able to reset the other combo boxes text property. How can this be
achieved??

The fact that they are all in one group box, to the best of my
knowledge, should not affect the answer. You want to set up a handler
for all six combo boxes' SelectedIndexChanged event. Within the event
handler, you need to unsubscribe for the other five combo boxes, because
otherwise they will fire a SelectedIndexChanged event as they refill
themselves.

The code in the constructor (or somewhere) will be something like this:

// repeat for each combo box
comboBox1.SelectedIndexChanged
+= new EventHandler(ComplexChangeHandler);

then the handler would be:

void ComplexChangeHandler (object sender, EventArgs e)
{
ComboBox cb = (ComboBox) sender;

// repeat for each combo box
comboBox1.SelectedIndexChanged
-= new EventHandler(ComplexChangeHandler);

// setup each of the other combo boxes' items. compare
// to 'cb' to see if this is the particular combo box.
// for example:

if (comboBox1 != cb) {ResetComboBox1();}

// Now reset all the handlers for selected index changed.
// repeat for each combo box
comboBox1.SelectedIndexChanged
+= new EventHandler(ComplexChangeHandler);
}
 
K

kndg

I have an application that has 6 combo boxes in a groupbox. When one
combo box is selected and it's text properties are displayed, I want to
be able to reset the other combo boxes text property. How can this be
achieved??

The fact that they are all in one group box, to the best of my
knowledge, should not affect the answer. [...]

Hi Mike,

If the OP is using .NET 3.5, he can take advantage of the extension
methods in the Enumerable class. Just wired all the combox boxes
SelectedIndexChanged event to below handler,

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string text = ((ComboBox)sender).Text;

groupBox1.Controls.OfType<ComboBox>().ToList().ForEach(p => p.Text = "");

((ComboBox)sender).Text = text;
}

Regards.
 
P

Peter Duniho

kndg said:
If the OP is using .NET 3.5, he can take advantage of the extension
methods in the Enumerable class. Just wired all the combox boxes
SelectedIndexChanged event to below handler,

Minor nit:
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string text = ((ComboBox)sender).Text;

groupBox1.Controls.OfType<ComboBox>().ToList().ForEach(p => p.Text = "");

There is no need to use the ToList() method. In fact, doing so simply
makes the code less efficient, because it forces extra allocations and a
second enumeration of the collection.

Pete
 
K

kndg

[...]
groupBox1.Controls.OfType<ComboBox>().ToList().ForEach(p => p.Text = "");

There is no need to use the ToList() method. In fact, doing so simply
makes the code less efficient, because it forces extra allocations and a
second enumeration of the collection.

:) Yeah, I already knew someone would argue on this, and I should blame
MS for not providing the ForEach extension method for IEnumerable<T>
(though you could made one very easily).
Anyway, to the OP, here is how the code should be,

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string text = ((ComboBox)sender).Text;

var comboBoxs = groupBox1.Controls.OfType<ComboBox>();

foreach (var comboBox in comboBoxs)
{
comboBox.Text = "";
}

((ComboBox)sender).Text = text;
}
 
P

Peter Duniho

kndg said:
[...]
groupBox1.Controls.OfType<ComboBox>().ToList().ForEach(p => p.Text =
"");

There is no need to use the ToList() method. In fact, doing so simply
makes the code less efficient, because it forces extra allocations and a
second enumeration of the collection.

:) Yeah, I already knew someone would argue on this, and I should blame
MS for not providing the ForEach extension method for IEnumerable<T>
(though you could made one very easily).

Opinions may vary, but IMHO the lack of ForEach() in the Enumerable
class is sensible. The point of LINQ is to make code more expressive,
but also to provide an expression-based approach to manipulating
enumerations.

One particular characteristic of that is that the Enumerable class
methods are all "fluent" in nature. That is, they can be chained to
produce new IEnumerable<T> instances.

The ForEach() method is a dead-end method, and would be a bit of an
outsider in the Enumerable class.

My guess is that there was a pretty hearty debate at Microsoft regarding
whether to include a ForEach() in Enumerable or not. I think they
probably made the right choice.

There is also, of course, the question of whether a ForEach() method
would really help the code be more expressive or not. Personally, I
think an explicit loop is just as expressive, if not more so. YMMV. :)

Pete
 
P

Peter Duniho

Peter said:
[...]
One particular characteristic of that is that the Enumerable class
methods are all "fluent" in nature. That is, they can be chained to
produce new IEnumerable<T> instances.

Slight miswording: obviously, not all Enumerable methods return
IEnumerable<T>. But, they do return _something_. That is, they are all
useful in _expressions_, in that the result can then be passed along to
something else.

Sorry for the imprecise statement.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top