Re: Creating Buttons In Code
cant you use the event drop downlist in code view?
Re: Creating Buttons In Code
Hi Grunt, thanks for replying.
Because I've created the button within the code itself, it does not show up on the event list (which is mildly annoying).
Only controls that I have drawn onto the form show up there.
Re: Creating Buttons In Code
declare it withevents:
Code:
Friend WithEvents MyButton As System.Windows.Forms.Button
Re: Creating Buttons In Code
To add attach a button created at runtime to an event when creating the button add this:
VB Code:
AddHandler MyButton.Click , AddressOf MyButton_Click
I believe another issue you will run into is where you are declaring the button. It should be declared at the class level (outside any function) and have withevents.
VB Code:
Private WithEvents MyButton as Button
Private Sub TheSubWithTheCode
MyButton = New Button
AddHandler MyButton.Click, AddressOf MyButton_Click
'Then the rest that you have
End Sub
Re: Creating Buttons In Code
you also need an actual sub called MyButton_Click, that has the same signature as the actual button click event.
Re: Creating Buttons In Code
VB Code:
Public Class Form3
Inherits System.Windows.Forms.Form
[b]Private WithEvents MyButton As Button[/b]
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
[b]RemoveHandler MyButton.Click, AddressOf SayHelloWorld[/b]
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form3
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(456, 302)
Me.Name = "Form3"
Me.Text = "Form3"
End Sub
#End Region
[b]
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyButton = New Button
MyButton.Location = New Point(100, 100)
MyButton.Size = New Size(64, 64)
MyButton.Text = "Click me to say Hello World"
AddHandler MyButton.Click, AddressOf SayHelloWorld
Me.Controls.Add(MyButton)
End Sub
Private Sub SayHelloWorld(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Hello World")
End Sub
End Class
[/b]
Don't forget to remove any handlers you add
Re: Creating Buttons In Code
Guys, especially Kleinma, thanks very much! Kleinma - your method worked!
The only problem now is that I've got a whole lot of code to re-work...a late night for me, unfortunately!
Thanks again guys, take care.