Creating Buttons With Events On Runtime VB.Net? | Programming, .NET, Silverlight & More

Creating Buttons With Events On Runtime VB.Net?

Hello Guys

This is very urgent

=============
i am creating an WPF application using VS 2008 with VB.Net and LINQ
=============
I want to create buttons in runtime with events every button has its event
=============
i will display my code here

‘ this is in construction area

Dim WithEvents copierbtn As Button

‘ This sub to get data from tblcopier and add it to list box

Sub loadlist()
Dim x = From mm In db.tblCopiers Select mm.Copier_id, mm.Copier_Name
With Me.ListBox1
.ItemsSource = x
.SelectedValuePath = "Copier_id"
.DisplayMemberPath = "Copier_Name"
End With

‘ this sub loops inside the listbox and creates buttons in wrap panel as many as items inside the listbox and give it content same as item name

Sub LoadButtons()
For i As Integer = 0 To ListBox1.Items.Count - 1
copierbtn = New Button
copierbtn.Style = CType(FindResource("BlueBottonWithArrow"), Style)
copierbtn.Width = 150
copierbtn.Height = 30
copierbtn.ToolTip = ListBox1.Items(i).Copier_ID.ToString
copierbtn.Margin = New Thickness(10, 10, 10, 10)
copierbtn.Content = ListBox1.Items(i).Copier_name.ToString
WrapCopierBtns.Children.Add(copierbtn)
Next
End Sub

‘ In form load i run these both subs
Private Sub frmDailyCopier_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
loadlist()
LoadButtons()

End Sub

‘ this is the click event

Private Sub copierbtn_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles copierbtn.Click
lblCopierID.Content = sender.tooltip
lblCopierName.Content = sender.content
End Sub

when i run the application it run so good and the click event run so good but when i click on the last created button only

for example

when i load this form it creates 4 buttons the event runs only on the last button

i want to assign this evevnt to every created button

what should i do plz

not sure about what ur code doing…but i think this will help

AddHandler object.EventName, AddressOf Sub/Function

add this for all buttons (remove the Handles copierbtn.Click in ur code,otherwise it will run twice)

No TweetBacks yet. (Be the first to Tweet this post)

One Response

  1. Player Says:

    not sure about what ur code doing…but i think this will help

    AddHandler object.EventName, AddressOf Sub/Function

    add this for all buttons (remove the Handles copierbtn.Click in ur code,otherwise it will run twice)
    References :

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.