Results 1 to 8 of 8

Thread: Creating Buttons In Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    7

    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.

  2. #2
    Hyperactive Member Grunt's Avatar
    Join Date
    Oct 2004
    Location
    Las Vegas
    Posts
    499

    Re: Creating Buttons In Code

    cant you use the event drop downlist in code view?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    7

    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.

  4. #4
    Hyperactive Member Grunt's Avatar
    Join Date
    Oct 2004
    Location
    Las Vegas
    Posts
    499

    Re: Creating Buttons In Code

    declare it withevents:

    Code:
    Friend WithEvents MyButton As System.Windows.Forms.Button

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Creating Buttons In Code

    To add attach a button created at runtime to an event when creating the button add this:

    VB Code:
    1. 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:
    1. Private WithEvents MyButton as Button
    2.  
    3. Private Sub TheSubWithTheCode
    4. MyButton = New Button
    5. AddHandler MyButton.Click, AddressOf MyButton_Click
    6. 'Then the rest that you have
    7.  
    8. End Sub

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Creating Buttons In Code

    VB Code:
    1. Public Class Form3
    2.     Inherits System.Windows.Forms.Form
    3.     [b]Private WithEvents MyButton As Button[/b]
    4.  
    5. #Region " Windows Form Designer generated code "
    6.  
    7.     Public Sub New()
    8.         MyBase.New()
    9.  
    10.         'This call is required by the Windows Form Designer.
    11.         InitializeComponent()
    12.  
    13.         'Add any initialization after the InitializeComponent() call
    14.  
    15.     End Sub
    16.  
    17.     'Form overrides dispose to clean up the component list.
    18.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    19.         [b]RemoveHandler MyButton.Click, AddressOf SayHelloWorld[/b]
    20.         If disposing Then
    21.             If Not (components Is Nothing) Then
    22.                 components.Dispose()
    23.             End If
    24.         End If
    25.         MyBase.Dispose(disposing)
    26.     End Sub
    27.  
    28.     'Required by the Windows Form Designer
    29.     Private components As System.ComponentModel.IContainer
    30.  
    31.     'NOTE: The following procedure is required by the Windows Form Designer
    32.     'It can be modified using the Windows Form Designer.  
    33.     'Do not modify it using the code editor.
    34.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    35.         '
    36.         'Form3
    37.         '
    38.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    39.         Me.ClientSize = New System.Drawing.Size(456, 302)
    40.         Me.Name = "Form3"
    41.         Me.Text = "Form3"
    42.  
    43.     End Sub
    44.  
    45. #End Region
    46. [b]
    47.     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    48.         MyButton = New Button
    49.         MyButton.Location = New Point(100, 100)
    50.         MyButton.Size = New Size(64, 64)
    51.         MyButton.Text = "Click me to say Hello World"
    52.         AddHandler MyButton.Click, AddressOf SayHelloWorld
    53.         Me.Controls.Add(MyButton)
    54.     End Sub
    55.  
    56.     Private Sub SayHelloWorld(ByVal sender As Object, ByVal e As System.EventArgs)
    57.         MessageBox.Show("Hello World")
    58.     End Sub
    59. End Class
    60. [/b]

    Don't forget to remove any handlers you add

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    7

    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
  •  



Click Here to Expand Forum to Full Width