473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting Button Events from Multiple manually instantiated controls

!NoItAll
297 100+
I have created a simple custom control - we will call it a "panel with buttons". Here is how I instantiate it:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim Panel(PanelCount) As Panel.Panel
  3. Dim iTop As Integer = 5
  4.  
  5.         For I As Integer = 0 To (PanelCount - 1)
  6.             Panel(I) = New Panel.Panel
  7.             Panel(I).Parent = Me
  8.             'the following just position the panels
  9.             Panel(I).Top = iTop + 10
  10.             iTop += 110
  11.             Panel(I).Left = 5
  12.         Next I
  13.  
So what I wind up with are multiple instances of my panel. Each of these panels has a button on it called Button1.
My question is - how do I capture this button event in a way that I will know which panel index it came from?

In the custom control there is only the Button1_click event - and when I click on it in any of the multiple panels I manually instantiated the event does occur, but I'm clueless as to how I can tell which one of my instantiated buttons were clicked...
Aug 30 '11 #1
1 1464
!NoItAll
297 100+
Ok - found the answer. I will try to post some sample code later but for now suffice to say that in my custom control I needed to create an event and then raise that event.
So I wound up doing something like:

Expand|Select|Wrap|Line Numbers
  1. Public Event ButtonPush(Index as Integer)
Then in the Button_Click event I put

Expand|Select|Wrap|Line Numbers
  1. RaiseEvent ButtonPush(CIndex(me.tag))
I will talk about the tag property in a minute...

In the form that is using my custom control there were a few things I needed to do:

When instantiating each control I need to set the tag property to an index (e.g. 0, 1, 2, 3, etc). This is the code I used:

Expand|Select|Wrap|Line Numbers
  1. Panel(I) = New Panel.Panel with {.Tag = I}
Now each panel has a unique tag which I will use as an index.

Next I needed to create a sub in my form to handle the event I wanted to catch

Expand|Select|Wrap|Line Numbers
  1. Sub PanelButtonPush_Clicked(Index as Integer) ' notice there is no Handles directive
  2.    'code to do something here
  3. End Sub
  4.  
Finally I needed to add the event handler so it would get processed in the message pump...

Expand|Select|Wrap|Line Numbers
  1. AddHandler Panel(I).PanelButtonPush, AddressOf PanelButtonPush_Clicked
Now whenever the button on any of the custom control array elements gets pushed the PanelButtonPush_Clicked event fires and the "Index" tells me which panel array element it came from.
Sep 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
3
by: mitsura | last post by:
Hi, I have included a small listing. The test program opens a panel and show a bitmap. What I want is to when the mouse is over the bitmap panel, I want to trap the left mouse click. The...
3
by: Epetruk | last post by:
Hi, I'm trying to design a simple web form, but I cannot get the code in the btnSubmit_ServerClick event to fire. What am I doing wrong? Here's the codebehind: using System; using...
0
by: Pat Sagaser via .NET 247 | last post by:
I'm using a repeater with a dynamic template. I don't know the fields to display (or how many) until runtime. I have everything working except for linking Button events to the repeaters ItemCommand...
2
by: JezB | last post by:
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm having trouble attaching events to these. For example, adding an image button :- ImageButton nb = new ImageButton();...
2
by: Nathan Sokalski | last post by:
I have several Button controls that I created dynamically that I need to handle the Click events for. How do I specify the event handler for dynamically created controls? Thanks. -- Nathan...
8
by: CsaaGuy | last post by:
My button events stopped working on a aspx form. Dont' know why. any suggestions?
2
by: acool | last post by:
I created this cool Usercontrol but now I am at a loss for how to access the events for the dynamic controls I created on the fly?
0
by: Steve Moreno | last post by:
Hi all, I've got a web form that I've written code to create an array of DropDownList controls on the page depending on how many records are pulled back. The code to create the controls is...
1
by: weboweb | last post by:
Hello aspnet experts! I have a design question for the more experienced developers (more than me at least :-)). 1) I have a page in the application I'm building that displays a web user...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.