dynamically adding event handlers to dynamic panels in vb.net - vb.net

Ok so i've been struggling to get my head around this for a few fays not and need some help.
so i have a series of panels that are generated
example:
For i as integer 1 to dt.rows.count
dim subpan as new panel
*Code for creating panel"
Next
the problem is i need to be able to add event handlers to each of them including, click, mouseEnter and mouseLeave but i can't figure out how to index each panel so that they can be accessed and identified. i tried using a property but that didn't seem to work or i was doing it wrong.
thanks in advance for the help.

You want the AddHandler function:
For i as integer 1 to dt.rows.count
dim subpan as new panel
subpan.ID = "subpan1" ' REQUIRED AND MUST BE UNIQUE
AddHandler subpan.Click, AddressOf subpan1_Click ' CLICK EVENT HANDLER ALSO UNIQUE
' ETC.
Next
Private Sub subpan1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' CODE TO HANDLE CLICK EVENT
End Sub
I should mention that you can route all click events to the same event handler, they don't have to be unique unless each panel requires custom logic.

You can use AddHandler to add the handler:
For i As Integer = 1 To dt.Rows.Count
Dim subpan As New Panel()
'Code for creating panel
'Attach events:
AddHandler subpan.Click, AddressOf Some_Listener
'etc.
Next
You can then access the current Panel in the event handlers by casting the sender argument to Panel.

Related

How to Handle events from a control array VB.net

To start, hi to everyone im new to stackoverflow, and also new to programing (on 1ยบ year).
I've been searching but ive found nothing that answer my question, or maybe im just to newbie to understand the answers, so im sorry if its too simple, i cant see it!
/* my native lenguage is not english*/
Here is my problem, i'm making a VB form whit 200 pictureboxes that have to change or interact on click
i've made a control array whit all of them, like this:
Dim control(199) As PictureBox = Controles(control, 0)
Function Controles(ByRef control As Array, ByVal cont As Integer)
For Each pic As PictureBox In Me.Controls
control(cont) = pic
cont += 1
Next
Return control
End Function
this should asociate each picturebox to an array position, my problem now is how i can set the event handler to watch at control().click so no matter what box you click the event onclick will proc.
the only way i know is to create a click handler for each box manually.
hope i can find some answers
Using the Addhandler statement you can wire them all to the same routine. Then cast the sender object to interact with the PB that was clicked. OfType function.
Private Sub LoadME() Handles Me.Load
For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)()
'add all PB click events to a event sub
AddHandler pb.Click, AddressOf pb_Click
Next
End Sub
Private Sub pb_Click(sender As Object, e As EventArgs)
Dim pb = DirectCast(sender, PictureBox)
'this is the PB that was clicked
End Sub

onClick Event For Picturebox

I have more than 30 picture box in a windows form named
PictureBox1
PictureBox2
PictureBox3
.....so on
I am having to write the same procedure for click function for every single one of them.
I just want to be able to write a single handler that would handle the click events for all the PictureBox and call the same function.
For a rough technical example.. Sorry for the JavaScript Format
picturebox.onclick(){
var a=get_index(this); //gets address of the clicked picturebox
somefunction(a);
}
If you are using the Handles keyword, which is the default, typical way to handle events from controls, you can add multiple event names to the Handles clause, for instance:
Private Sub OnClick(sender As Object, e As EventArgs) Handles PictureBox1.Click, PictureBox2.Click, PictureBox3.Click
' Handles click events from PictureBox1, PictureBox2, and PictureBox3
End Sub
Alternatively, you can declare the event handler method without the Handles clause, and then manually attach it to the events yourself, like this:
Private Sub OnClick(sender As Object, e As EventArgs)
' ...
End Sub
' ...
AddHandler PictureBox1.Click, AddressOf OnClick
AddHandler PictureBox2.Click, AddressOf OnClick
AddHandler PictureBox3.Click, AddressOf OnClick
Or, if you have list of picture box controls, you could add the event handlers in a loop, like this:
Dim myPictureBoxes() As PictureBox = {PictureBox1, PictureBox2, PictureBox3}
For Each i As PictureBox in myPictureBoxes
AddHandler i.Click, AddressOf OnClick
Next
Or, you could access them from your form's Controls collection by name:
For i As Integer = 1 to 30
Dim c As Control = Me.Controls("PictureBox" & i.ToString())
AddHandler c.Click, AddressOf OnClick
Next
Bear in mind, however, if you do manually call AddHandler, you need to also call RemoveHandler to later detach the event handler.
It seems odd, on the surface, to have so many picture boxes like that, though. You may want to consider custom-drawing your content in a single control or look into dynamically loading the controls at run-time.

How to use dynamically added toolstripmenuitem

I'm trying to make a tool strip item that contains bookmarks and each bookmark should go to the page. How do make each button work?.
For Each b In New System.IO.DirectoryInfo("Bookmarks").GetFiles
BookmarksToolStripMenuItem.DropDownItems.Add(b.Name)
Next
You should first create a too ToolStripMenuItem, then add handlers and put it to your toolstripmenu object instead of adding to toolstripmenu a string object.
For Each b In New System.IO.DirectoryInfo("Bookmarks").GetFiles
Dim menuItem As New ToolStripMenuItem(b.Name)
'Add any handlers here
'Click handler to your menuItem.
AddHandler menuItem.Click, AddressOf menuItem_Click 'CLICK EVENT HANDLER ALSO UNIQUE
'Add menuItem to ToolStripMenu
BookmarksToolStripMenuItem.DropDownItems.Add(menuItem)
Next
Private Sub menuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'CODE TO HANDLE CLICK EVENT
End Sub
If you don't know how to dynamicaly add handlers then take a look at examples.

Get to know which picturebox was clicked

In my current VBA project I have several pictureboxes which will activate a messagebox when you click on one of them. Is there any way for me to get to know which of the pictureboxes was clicked? I've added the code I have currently which doesnt allow me to pass any information to the sub so each picturebox will active the exact same message.
AddHandler newPictureBox.Click, AddressOf pic_Click
Public Sub pic_Click()
MsgBox("test")
End Sub
I see above you are using VB2010. Try this...
AddHandler newPictureBox.Click, AddressOf pic_Click
Private Sub pic_Click(sender As Object, e As EventArgs)
Dim thisPic As PictureBox = DirectCast(sender, PictureBox)
'thisPIC now is a reference to the box, you can use .Name, etc. to get it's properties.
End Sub
You could add a different handler to each picturebox, which would then delegate to another sub, passing along the info about which picturebox was clicked.
Why don't you use the picturebox.click event?
Just double click on your picturebox and it will automatically be using that event.
Then you can add your own code into the picturebox.click so they will have their own function.
If that's not what you want, please describe what it is that you're trying to do more clearly.

Dynamic button click event handler

I've 100 buttons created dynamically in a form. How can I an add event handler to them?
You can use AddHandler to add a handler for any event.
For example, this might be:
AddHandler theButton.Click, AddressOf Me.theButton_Click
Just to round out Reed's answer, you can either get the Button objects from the Form or other container and add the handler, or you could create the Button objects programmatically.
If you get the Button objects from the Form or other container, then you can iterate over the Controls collection of the Form or other container control, such as Panel or FlowLayoutPanel and so on. You can then just add the click handler with
AddHandler ctrl.Click, AddressOf Me.Button_Click (variables as in the code below),
but I prefer to check the type of the Control and cast to a Button so as I'm not adding click handlers for any other controls in the container (such as Labels). Remember that you can add handlers for any event of the Button at this point using AddHandler.
Alternatively, you can create the Button objects programmatically, as in the second block of code below.
Then, of course, you have to write the handler method, as in the third code block below.
Here is an example using Form as the container, but you're probably better off using a Panel or some other container control.
Dim btn as Button = Nothing
For Each ctrl As Control in myForm.Controls
If TypeOf ctrl Is Button Then
btn = DirectCast(ctrl, Button)
AddHandler btn.Click, AddressOf Me.Button_Click ' From answer by Reed.
End If
Next
Alternatively creating the Buttons programmatically, this time adding to a Panel container.
Dim Panel1 As new Panel()
For i As Integer = 1 to 100
btn = New Button()
' Set Button properties or call a method to do so.
Panel1.Controls.Add(btn) ' Add Button to the container.
AddHandler btn.Click, AddressOf Me.Button_Click ' Again from the answer by Reed.
Next
Then your handler will look something like this
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
End Sub
#Debasish Sahu, your answer is an answer to another question, namely: how to know which button (or any other control) was clicked when there is a common handler for a couple of controls? So I'm giving an answer to this question how I usually do it, almost the same as yours, but note that also works without type conversion when it handles the same type of Controls:
Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim selectedBtn As Button = sender
MsgBox("you have clicked button " & selectedBtn.Name)
End Sub
I needed a common event handler in which I can show from which button it is called without using switch case... and done like this..
Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox.Show("you have clicked button " & CType(CType(sender, _
System.Windows.Forms.Button).Tag, String))
End Sub
Some code for a variation on this problem. Using the above code got me my click events as needed, but I was then stuck trying to work out which button had been clicked.
My scenario is I have a dynamic amount of tab pages. On each tab page are (all dynamically created) 2 charts, 2 DGVs and a pair of radio buttons. Each control has a unique name relative to the tab, but there could be 20 radio buttons with the same name if I had 20 tab pages. The radio buttons switch between which of the 2 graphs and DGVs you get to see. Here is the code for when one of the radio buttons gets checked (There's a nearly identical block that swaps the charts and DGVs back):
Private Sub radioFit_Components_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If sender.name = "radioFit_Components" And sender.visible Then
If sender.checked Then
For Each ctrl As Control In TabControl1.SelectedTab.Controls
Select Case ctrl.Name
Case "embChartSSE_Components"
ctrl.BringToFront()
Case "embChartSSE_Fit_Curve"
ctrl.SendToBack()
Case "dgvFit_Components"
ctrl.BringToFront()
End Select
Next
End If
End If
End Sub
This code will fire for any of the tab pages and swap the charts and DGVs over on any of the tab pages. The sender.visible check is to stop the code firing when the form is being created.