Smarton
Smarton

Reputation: 119

Add an extra control to a button in VB.Net

I have a code that creates 30 buttons at run time on a form which works excellently

For i = 0 To 30
    btn = New Button
    Dim old As Padding = btn.Margin
    Dim old2 As Padding = btn.Padding
    btn.Tag = "X1"
    btn.Name = "Drane"
    btn.Text = "The power of trying"
    btn.Width = 95
    btn.Height = 120
    btn.Margin = New Padding(10, 10, 10, 10)
    btn.Padding = New Padding(0, 4, 0, 2)
    btn.TextAlign = ContentAlignment.BottomCenter
    btn.ImageAlign = ContentAlignment.TopCenter
    btn.Font = New Font("Lucida Sans Unicode", 11)
    btn.ForeColor = Color.Black
    btn.BackColor = Color.White
Next i

Now I'm trying to add an extra control to hold the ids for buttons that I created at run time

btn.id = i

which would hold the ids of each particular button so I can get the value in other functions in my application. I know I have to add this somehow as a control but I just don't know how.

Please any help will be greatly appreciated in resolving this

Upvotes: 0

Views: 316

Answers (1)

Andrew Mortimer
Andrew Mortimer

Reputation: 2370

This will extend the Button class and add an ID property.

Public Class ButtonMod
    Inherits Button

    Public Property ID As Integer

End Class

Then you just create them in a loop as you were doing already:

For i As Integer = 1 To 30
    Dim btn As New ButtonMod
    Dim old As Padding = btn.Margin
    Dim old2 As Padding = btn.Padding
    btn.Tag = "X1"
    btn.Name = "Drane"
    btn.Text = "The power of trying"
    btn.Width = 95
    btn.Height = 120
    btn.Margin = New Padding(10, 10, 10, 10)
    btn.Padding = New Padding(0, 4, 0, 2)
    btn.TextAlign = ContentAlignment.BottomCenter
    btn.ImageAlign = ContentAlignment.TopCenter
    btn.Font = New Font("Lucida Sans Unicode", 11)
    btn.ForeColor = Color.Black
    btn.BackColor = Color.White
    btn.ID = i
Next

Upvotes: 1

Related Questions