Jump to content
Xtreme .Net Talk

creating a control in code


Recommended Posts

Posted

there is absolutely no help on this - so I tried for quite a while with no luck

 

how is it done

 

my code

   Dim c As System.Windows.Forms.Button
   Private Sub Form1_Load(ByVal sender As System.Object,  _
           ByVal e As System.EventArgs) Handles MyBase.Load
       c = New System.Windows.Forms.Button()
       c.Enabled = True
       c.Visible = True
       c.Text = "One"
       c.Height = 20
       c.Width = 200
       c.Top = 0
       c.Left = 0
   End Sub

Posted

Here you go..

 

   'Instantiate the button.
   Dim WithEvents button As New Windows.Forms.Button()

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
       Handles MyBase.Load

       'Set the button's properties...
       With button
           .Name = "aButton"   'Name.
           .Location = New Drawing.Point(0, 0) 'Where to put the button.
           .Size = New Drawing.Size(50, 50)    'Size of the button.
           .Text = "hi there"  'Text to display.
       End With

       'Add the button to the form.
       Me.Controls.Add(button)
   End Sub

Gamer extraordinaire. Programmer wannabe.
  • 11 months later...
Posted

I am a beginner in VB.NET

 

I think this is an excellent code to try.

I've been looking for this for 2 days...

 

Thanks Wyrd.

 

I will try it and get back on this..

 

However, the downside of this code is that you'd

need to know the number of controls that appears on

the form.. since you need to declare the variable buttons..

etc in order to generate the control itself within the code..

 

what if we want to generate controls on the fly and do

not know how many control there are to generate?

 

Can we REDIM the variable name to different iteration of

variable name to generate the code?

 

Please advice.

 

eg :

 

dim x,y as integer

 

x = <no of bottuns to be generate>

 

for y = 0 to x

 

Dim WithEvents button As New Windows.Forms.Button()

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles MyBase.Load

 

'Set the button's properties...

With button

.Name = "aButton" & y 'SETTING NAME CHANGE HERE FOR EACH BUTTON

.Location = New Drawing.Point(0, 50 * y +2) 'Where to put the button.

.Size = New Drawing.Size(50, 50) 'Size of the button.

.Text = "hi there" 'Text to display.

End With

 

'Add the button to the form.

Me.Controls.Add(button)

End Sub

 

 

next

 

Liz.. <-- Trying to create an application that generates forms elements from database.

  • Administrators

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...