Display buttons on the form using code

candice

Active member
Joined
Jan 23, 2007
Messages
30
Programming Experience
Beginner
hello guys,

I want to display three buttons on the form when the program is run.
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_load()
[/SIZE][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] AddOKCancelButtons()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Create three buttons and place them on a form using 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' several size and location related properties. 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] AddOKCancelButtons()
[/SIZE][SIZE=2][COLOR=#008000]' Set the button size and location using 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' the Size and Location properties. 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] buttonOK [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Button()
buttonOK.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(136, 248)
buttonOK.Size = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Size(75, 25)
[/SIZE][SIZE=2][COLOR=#008000]' Set the Text property and make the 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' button the form's default button. 
[/COLOR][/SIZE][SIZE=2]buttonOK.Text = [/SIZE][SIZE=2][COLOR=#800000]"&OK"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].AcceptButton = buttonOK
[/SIZE][SIZE=2][COLOR=#008000]' Set the button size and location using the Top, 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Left, Width, and Height properties. 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] buttonCancel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Button()
buttonCancel.Top = buttonOK.Top
buttonCancel.Left = buttonOK.Right + 5
buttonCancel.Width = buttonOK.Width
buttonCancel.Height = buttonOK.Height
[/SIZE][SIZE=2][COLOR=#008000]' Set the Text property and make the 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' button the form's cancel button. 
[/COLOR][/SIZE][SIZE=2]buttonCancel.Text = [/SIZE][SIZE=2][COLOR=#800000]"&Cancel"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CancelButton = buttonCancel
[/SIZE][SIZE=2][COLOR=#008000]' Set the button size and location using 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' the Bounds property. 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] buttonHelp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Button()
buttonHelp.Bounds = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle(10, 10, 75, 25)
[/SIZE][SIZE=2][COLOR=#008000]' Set the Text property of the button.
[/COLOR][/SIZE][SIZE=2]buttonHelp.Text = [/SIZE][SIZE=2][COLOR=#800000]"&Help"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' Add the buttons to the form.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls.AddRange([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Control() {buttonOK, buttonCancel, buttonHelp})
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]
It's a very simple program, but there is nothing on the form when running. Could anybody help me out?
Thank you!
 
The problem you've got here is that you won't be able to do anything with those buttons after that sub has exited because they have a local scope. I.e only exist within that Void. Secondly they will be of practically no use to you as they won't be able to handle events as you haven't declared them using the WithEvents keyword. A revised example would look like....

VB.NET:
PublicClass Form1
 
Friend WithEvents buttonOK as Button
Friend WithEvents buttonCancel as Button
Friend WithEvents buttonHelp as button
 
PrivateSub Form1_load()
'We don't use the 'Call' keyword in .Net
Me.AddOKCancelButtons()
EndSub
' Create three buttons and place them on a form using 
' several size and location related properties. 
 
PrivateSub AddOKCancelButtons()
' Set the button size and location using 
' the Size and Location properties. 
Me.buttonOK = New  Button()
Me.buttonOK.Location = New Point(136, 248)
Me.buttonOK.Size = New Size(75, 25)
' Set the Text property and make the 
' button the form's default button. 
Me.buttonOK.Text = "&OK"
Me.AcceptButton = Me.buttonOK
' Set the button size and location using the Top, 
' Left, Width, and Height properties. 
 
Me.buttonCancel = New Button()
Me.buttonCancel.Top = buttonOK.Top
Me.buttonCancel.Left = buttonOK.Right + 5
Me.buttonCancel.Width = buttonOK.Width
Me.buttonCancel.Height = buttonOK.Height
' Set the Text property and make the 
' button the form's cancel button. 
Me.buttonCancel.Text = "&Cancel"
Me.CancelButton = buttonCancel
' Set the button size and location using 
' the Bounds property. 
Me.buttonHelp = New Button()
Me.buttonHelp.Bounds = New Rectangle(10, 10, 75, 25)
' Set the Text property of the button.
Me.buttonHelp.Text = "&Help"
' Add the buttons to the form.
Me.Controls.AddRange(New Control() {Me.buttonOK, Me.buttonCancel, Me.buttonHelp})
EndSub
EndClass

You do realise that this can all be done in the designer?
 
Thanks vis781,
Yes I know this can be done in the designer. I wanna use the code becasue I may read data in the excel file which represent the size of the button in the next step.
And I found out that if I replace
VB.NET:
PrivateSub Form1_load()
with
VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE]
The codes above both work out...
 
Back
Top