Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

add handle at runtime to comboboxes


I have a table configuration that has indefinite records.
I have created two arrays that contain as much labels and coboboxes as records in the table configuration.
I've given each combobox the name of the configuration name.

My question is:

How can I add something like onDisplayMemberChanged on each of these comboboxes?

Tom de Troch
Thursday, April 01, 2004

I'm unsure about what you are asking but maybe this can help you. What the code I show does is to add an event handler for a mouseUp event... of course you can do this for any of the combobox event:

PictureBox pb = new PictureBox();
pb.BackColor = System.Drawing.Color.Green;
pb.MouseUp += new MouseEventHandler(this.YourFunction_Click);

Ok... this creates a new PictureBox, changes its backcolor (optional :-) and asigns a function handler for the MouseUp event. I think the handler is called a 'delegate'. There can be many handlers for an event.

As you see, the delegate is instanced with the 'new' keyword and (IMPORTANT) the correct type of the handler. You can see what type of delegate an event needs in the docs. The MouseUp, as you see, needs a MouseEventHandler. The '+=' is used to wire/link/attach the handler to the event.

Your function must be declared as the handler says (in this case, for MouseEventHandler):

private void YourFunction_Click (object sender, MouseEventArgs e) {}

As you see, it needs as parameters an object and some arguments. Each event has its own EventArgs class which contains the adecuate info.

Hope this helps...

Jose Juan Montes
Thursday, April 01, 2004

yes this helps

Thank you

Tom de Troch
Thursday, April 01, 2004

I am sorry. I it does not work.

Tom de Troch
Thursday, April 01, 2004

What exactly are you looking for? Are you wanting an event that handles when a user changes the selection of one of the combo boxes?

Thursday, April 01, 2004

But I don't know the names of the comboboxes. Only at runtime.
if the table configuration has 2 record (SMS,NetTime)
the coboboxes will have the name SMS and NetTime.

Now I want to now if i can add an event to those 2 coboboxes? So if they appear on the screen and the user selects a value from the combobox the program will do something.

I have tried to do it the same way as Jose Juan Montes  but It does not work. If I do:

Public arr as cobobox() 'in header

'in some sub
redim arr(2)
arr(1) = new cobobox
arr(2) = new cobobox
arr(1).ValueChangeEvent = new ...
He gives me an error that ValueChangeEvent is not a member of arr(1)

Tom de Troch
Thursday, April 01, 2004

Someone told me to use addhandler

Just one problem the comboboxes are created in one from and displayed in an other form. So it does not work. If I find it I will post it on the forum

Tom de Troch
Thursday, April 01, 2004

it works with:

AddHandler Selection.cboarray(l).SelectedIndexChanged, AddressOf Selection.MyEventHandler

Tom de Troch
Thursday, April 01, 2004

So did you get it to work? I have some other ideas if not.

Thursday, April 01, 2004

You can always post your ideas.

Tom de Troch
Friday, April 02, 2004

Why not always have two combo boxes, but hide one if you don't need it.

Then you can attach your events at design time.

Steve Jones (UK)
Friday, April 02, 2004

Well, my thought was inheritance based by looping through the controls collection of the form to attach the proper event handler. But I'm not as sure if that would work for you or not.

Tuesday, April 06, 2004

*  Recent Topics

*  Fog Creek Home