Know which dynamically added control was clicked

C

Charles May

I have a form that creates a button for each employee in the database. I
have added the click handler and have this working. The buttons contain the
employee's name for their respective .Text and I have included a number in
the tag field. My problem is now, finding out which button is being clicked
so that I can pull the information from the database.

How can I get the tag value of the button clicked?

Also... what type of information can I store in the tag field of the
control? I would like to store the EmployeeID (which is generated by
MSAccess Autonumber) so that when the button is clicked, I can query the
database to get the information needed.

Thanks

Charlie
 
Z

zacks

I have a form that creates a button for each employee in the database. I
have added the click handler and have this working. The buttons contain the
employee's name for their respective .Text and I have included a number in
the tag field. My problem is now, finding out which button is being clicked
so that I can pull the information from the database.

How can I get the tag value of the button clicked?

Also... what type of information can I store in the tag field of the
control? I would like to store the EmployeeID (which is generated by
MSAccess Autonumber) so that when the button is clicked, I can query the
database to get the information needed.

In the Button Click event hander add a line:

Dim btn as Button = sender

Then you can reference all properties of the button that signalled the
event with the btn variable, as in btn.Tag.
 
C

Charles May

Thanks Zacks

I knew it had something to do with sender but I couldn't figure it out.

Now you might be able to answer this for me.

Right now, I have a button on the form and when I click the button the new
buttons are created and added.

In my code I have the following

AddHandler btn.Click, AddressOf btn_Click

What I would like to do is generate everything while the frmSplash is up.
The only thing I am having a problem with is creating the handler on one
form to be used on another.

so frmSplash is displayed at startup and querys the database, then from
there creats the buttons on frmMain and creates the handler for the
btn_Click to work on frmMain when it is displayed.

Any ideas?

Thanks for all your help
Charlie

I have a form that creates a button for each employee in the database. I
have added the click handler and have this working. The buttons contain
the
employee's name for their respective .Text and I have included a number in
the tag field. My problem is now, finding out which button is being
clicked
so that I can pull the information from the database.

How can I get the tag value of the button clicked?

Also... what type of information can I store in the tag field of the
control? I would like to store the EmployeeID (which is generated by
MSAccess Autonumber) so that when the button is clicked, I can query the
database to get the information needed.

In the Button Click event hander add a line:

Dim btn as Button = sender

Then you can reference all properties of the button that signalled the
event with the btn variable, as in btn.Tag.
 
C

Chris Dunaway

so frmSplash is displayed at startup and querys the database, then from
there creats the buttons on frmMain and creates the handler for the
btn_Click to work on frmMain when it is displayed.

frmSplash should not be querying the database. frmSplash should just
be informational only. Basically, in frmMain, you should show
frmSplash, then query the database and set up your buttons, finally
hide frmSplash and show frmMain. In other words, frmMain should be
the one that queries the database, creates the buttons, sets up the
event handlers for the buttons.

Chris
 
Z

zacks

frmSplash should not be querying the database.  frmSplash should just
be informational only.  Basically, in frmMain, you should show
frmSplash, then query the database and set up your buttons, finally
hide frmSplash and show frmMain.   In other words, frmMain should be
the one that queries the database, creates the buttons, sets up the
event handlers for the buttons.

I agree with Chris. Just close the spash form in the main form's load
event after it finishes creating all of the dynamic controls. The main
form's load event completes before the main form is actually shown.
 
C

Charles May

Thanks Chris,
That's actually what I ended up doing. It was simpler and more easy for
following the code.

Thanks for the input though

Charlie
 

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