|
-
Nov 23rd, 2005, 12:36 PM
#1
Thread Starter
New Member
Creating Buttons In Code
Apologies if this has been asked many times before, but I'm a fairly new VB.Net programmer, and I'm having a slight problem here with VB.Net
Without elaborating on my reasoning, I am adding controls to my form in code rather in designer mode. For example, buttons are added like this
' *** REST OF CODE IN HERE ***
Dim MyButton as New Button
MyButton.Location = New Point (100, 100)
MyButton.Size = New Size (64, 64)
MyButton.Text = "Click me to say Hello World"
Me.Controls.Add(MyButton)
' *** REST OF CODE IN HERE ***
rather than by the traditional way of "drawing" buttons on the form.
My question is, having done this, how do I write code to (say) display a message box to say "Hello World"?
What I mean is that, if I had drawn the button on using the designer, I would click on the button to generate a function within which I could enter code, e.g.
Private Sub MyButton_Click(etc.etc.) Handles MyButton.Click
MessageBox("Hello World!") ' or similar
End Sub
or similar. However, because I've created the button in code, VB.Net will not allow me to do this.
Anybody have any ideas? Any help would be greatly appreciated!!!!
Thanks again.
-
Nov 23rd, 2005, 12:39 PM
#2
Hyperactive Member
Re: Creating Buttons In Code
cant you use the event drop downlist in code view?
-
Nov 23rd, 2005, 12:42 PM
#3
Thread Starter
New Member
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.
-
Nov 23rd, 2005, 12:48 PM
#4
Hyperactive Member
Re: Creating Buttons In Code
declare it withevents:
Code:
Friend WithEvents MyButton As System.Windows.Forms.Button
-
Nov 23rd, 2005, 12:51 PM
#5
Frenzied Member
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
-
Nov 23rd, 2005, 12:52 PM
#6
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.
-
Nov 23rd, 2005, 12:53 PM
#7
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
-
Nov 23rd, 2005, 01:03 PM
#8
Thread Starter
New Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|