How to make a click event for runtime created controls in Visual Basic 2010? - vb.net

I added some controls to my form at runtime and I need them to call a function when clicked. I don't know how many controls will be added but they all need to run the same function. How would I define the event? Can I define events based on all controls of a given class?

A simple example :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' creating control
Dim btn1 As Button = New Button()
Dim btn2 As Button = New Button()
btn1.Parent = Me
btn1.Name = "btn1"
btn1.Top = 10
btn1.Text = "Btn1"
btn2.Parent = Me
btn2.Name = "btn2"
btn2.Top = 50
btn2.Text = "Btn2"
'adding handler for click event
AddHandler btn1.Click, AddressOf HandleDynamicButtonClick
AddHandler btn2.Click, AddressOf HandleDynamicButtonClick
End Sub
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "btn1" Then
MessageBox.Show("Btn1 clicked")
ElseIf btn.Name = "btn2" Then
MessageBox.Show("Btn2 Clicked")
End If
End Sub
End Class

Simply:
AddHandler Control.Event, AddressOf MethodExecuting
For example:
AddHandler Button1.Click, AddressOf ClickMethod

Related

Detect multiple control changed in VB.Net

how can I control some controls that will be created automatically during the execution of the application?
My add control
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewCheckbox As CheckBox
For i As Integer = 2 To 14
NewCheckbox = New CheckBox
NewCheckbox.Size = New Drawing.Size(15, 14)
NewCheckbox.Location = New Point(98, 40)
NewCheckbox.Name = "cbcard" & i
Me.Controls.Add(NewCheckbox)
next
My checkbox control add, I gave an example to create a checkbox control, but it should be valid for any form of control, be it textbox or button.
how can i detect these controls that are not created but will be created during execution?
checkbox name will be cbcard1, cbcard2, cbcard3 up to 14.
but I want this to be in the form of a handler, or it should probably work with a timer.
Private Sub Checkbox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Checkbox.Changed
If Checkbox.checked = true
MsgBox("")
End If
End Sub
You can subscribe to the event using the AddHandler syntax.
For i As Integer = 2 To 14
NewCheckbox = New CheckBox
NewCheckbox.Size = New Drawing.Size(15, 14)
NewCheckbox.Location = New Point(98, 40)
NewCheckbox.Name = "cbcard" & i
Me.Controls.Add(NewCheckbox)
'subscribe to the CheckChanged event (as an example)
AddHandler NewCheckbox.CheckChanged, AddressOf Checkbox_Changed
Next
'all dynamically created checkboxes were subscribed to this event
Private Sub Checkbox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)
'sender is the specific checkbox that was changed out of all subscribed checkboxes
Dim checkBox = DirectCast(sender, CheckBox)
If checkbox.checked = true Then MsgBox(checkBox.Name)
End Sub

VB 2010 Form Show

i currently have multiple forms (around 30 forms) and i am switching between forms. The Main form (Form1) has 29 buttons and each button will take me to the respective form number (example: button3 = form3, button20=form20, etc).
I understand that I can use the code:
me.hide
form1.show
I want a method to pass the form name dynamically, something along the lines of:
me.controls(FormName).show
Is this possible?
Create a new project with three forms (Form1, Form2, and Form3). Put two buttons on Form1 (named Button2 and Button3), then place the following source code in Form1:
Option Strict On
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Call FormFromButton(DirectCast(sender, Button))
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Call FormFromButton(DirectCast(sender, Button))
End Sub
Public Sub FormFromButton(btn As Button)
Dim i As Integer = CInt(btn.Name.Substring(6)) 'Get number after "button" (6 characters)
Dim f As Form = GetForm("Form" & i.ToString)
f.Show()
f.Activate()
End Sub
Public Function GetForm(formClassName As String) As Form
'see if it is already instanced
For Each f As Form In My.Application.OpenForms
If f.GetType.Name = formClassName Then Return f
Next f
'create new instance
Dim strFullName As String = Me.GetType.Namespace & "." & formClassName
Dim o As Object = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(strFullName)
Dim frm As Form = DirectCast(o, Form)
Return frm
End Function
End Class
Myself created 4 forms one is parent another 3(Form3,Form4,Form5) is child and i was created 3 buttons in parent form and that button Text is Form3,Form4,Form5
Imports System
Imports System.Reflection
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
Dim btn As Button
btn = DirectCast(ctrl, Button)
AddHandler btn.Click, AddressOf Me.buttonclick
End If
Next
End Sub
Private Sub buttonclick(sender As Object, e As EventArgs)
Dim frmname As Button = DirectCast(sender, Button)
Dim frmAssembly As Assembly = Assembly.LoadFile(Application.ExecutablePath)
For Each type As Type In frmAssembly.GetTypes
If type.BaseType = GetType(Form) Then
If (type.Name = frmname.Text) Then
Dim frmshow As Form = DirectCast(frmAssembly.CreateInstance(type.ToString()), Form)
For Each frm As Form In Me.MdiChildren
frm.Close()
Next
frmshow.Show()
End If
End If
Next
End Sub
End Class
Private Sub ButtonClick(ByVal sender As Object, e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim formname As String = "form" & btn.Name(btn.Name.Length - 1)
Dim frm As Form = GetForm(formname)
frm.Show()
End Sub
Private Function GetForm(ByVal Formname As String) As Form
Dim t As Type = Type.GetType(Formname) ', True, True)
If t Is Nothing Then
Dim Fullname As String = Application.ProductName & "." & Formname
t = Type.GetType(Fullname, True, True)
End If
Return CType(Activator.CreateInstance(t), Form)
End Function
Private Sub AddHandlers()
AddHandler Button1.Click, AddressOf ButtonClick
AddHandler Button2.Click, AddressOf ButtonClick
AddHandler Button3.Click, AddressOf ButtonClick
AddHandler Button4.Click, AddressOf ButtonClick
End Sub
Private Sub RemoveHandlers()
RemoveHandler Button1.Click, AddressOf ButtonClick
RemoveHandler Button2.Click, AddressOf ButtonClick
RemoveHandler Button3.Click, AddressOf ButtonClick
RemoveHandler Button4.Click, AddressOf ButtonClick
End Sub
Private Sub Form2_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
AddHandlers()
End Sub
Private Sub Form2_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
RemoveHandlers()
End Sub

How to handle a variable amount of buttons clicked?

I have a pretty basic problem with button clicks in VB.Net I cannot seem to figure out.
First, I am creating a variable amount of buttons and adding them to the parent form.
Private Sub CreateUIObjects()
For i As Integer = 1 To NumberOfButtons
Dim button As Button = New Button()
Me.Controls.Add(button)
Next
End Sub
I know it is possible to handle a fixed amount of buttons clicked with the following code
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click, Button2.Click, Button3.Click '... And so on
Dim b As Button = CType(sender, Button)
End Sub
But what do I do with not 3, but a variable amount of buttons?
Some code to experiment with
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateUIObjects(3)
End Sub
Dim myButtonNames As String = "foobar"
Private Sub myButtonsClick(sender As Object, e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
Debug.WriteLine(b.Name)
End Sub
Private Sub CreateUIObjects(NumberOfButtons As Integer)
Static ct As Integer = 0
For i As Integer = 1 To NumberOfButtons
ct += 1
Dim btn As Button = New Button()
btn.Name = myButtonNames & ct.ToString
btn.Text = btn.Name
btn.Location = New Point(ct * 20, ct * 20)
AddHandler btn.Click, AddressOf myButtonsClick
Me.Controls.Add(btn)
Next
End Sub
You can use something like this
AddHandler Button1.Click, AddressOf Button_Click
take a look http://msdn.microsoft.com/en-us/library/ms172877.aspx

vb2010 getting name values from multiple buttons

At the moment, I have a button that sends a value to another form and displays result in a label. The problem is, I have 20 buttons that are labeled a to w that need to be coded and I am stumped as to how I can pass values from multiple buttons. Would it be a case statement in the form being passed to? I am a new user to VB.Net and still finding my way so any help would be gratefully received. I have included code sample for the first button 'A'. Thanks
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = MouseButtons.Right) Then
'Dim curButton As Button = DirectCast(sender, Button)
'frmRacks.buttonName = curButton.Name 'Dynamic alternative to frmRacks.buttonName = "A"
frmRacks.buttonName = "A"
frmRacks.Show()
ElseIf (e.Button = MouseButtons.Left) Then
MessageBox.Show("To be coded")
End If
End Sub
frmRacks
Public Class frmRacks
Public buttonName As String
Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
EDIT: New project
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
Form2.buttonName = curButtonName
Form2.Show()
'MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub
End Class
Public Class Form2
Public buttonName As String
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
End Class
Here you have a sample code which hopefully will help you to get clearer ideas:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub

vb.net control button in popup

I'm new to programming and I need help with something really basic..
I have a form with a datagridview and an "insert" button that opens a popup window (new form). This popup contains labels and textboxes, and 2 buttons (insert & cancel). How do you add event handlers to the buttons inside the popup? For example, if the user clicks "Cancel", the popup should close.
Here's the code I have so far:
Public Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click
Dim insertPopup As New Form
insertPopup.Size = New System.Drawing.Size(300, 400)
insertPopup.StartPosition = FormStartPosition.CenterScreen
insertPopup.Show()
Dim acceptButton As New Button
Dim cancelButton As New Button
acceptButton.Location = New System.Drawing.Point(100, 325)
acceptButton.Text = "Insert"
acceptButton.Size = New System.Drawing.Size(85, 24)
acceptButton.TabIndex = 1
cancelButton.Location = New System.Drawing.Point(190, 325)
cancelButton.Text = "Cancel"
cancelButton.Size = New System.Drawing.Size(85, 24)
cancelButton.TabIndex = 2
insertPopup.Controls.Add(acceptButton)
insertPopup.Controls.Add(cancelButton)
End Sub
Thanks for your help.
You use the AddHandler Method to add the proper EventHandler. You will need to be sure to create your handler with the correct signature.
AddHandler acceptButton.Click, AddressOf acceptButton_Click
AddHandler cancelButton.Click, AddressOf cancelButton_Click
Private Sub acceptButton_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub cancelButton_Click(sender As System.Object, e As System.EventArgs)
CType(CType(sender, Button).Parent, Form).Close()
End Sub