HelpAdd Value

Posts 1–2 of 2 · Page 1 of 1
Add Value
Alright, I want to add a value everytime i click a button. What i mean is,

Lets say, I want Button1, to add another button(I already know the code to add the button and everything).

But, everytime i click Button1, It adds a button with a different value at the end, like, Click once, Adds "Button2", Click again, Adds "Button3", How can i do this? I tried something like

Dim newBtn As New Button
Dim x As Integer
newBtn.Text = "Button " + x + Val(1)


But it just says that Type Double(The Val(1)), Can not be converted from Double to String, any ideas? Thanks
Maybe
Dim newBtn As New Button
Dim x As Integer
newBtn.Text = "Button " + (x + Val(1)).ToString()


Heres a small function i just made for you to test. Sorry i dont code in VB so it might be noobish

Code:
    Private Sub AddControl(ByVal C As Control)
        Dim Index As Integer = 0
        For Each Con As Control In Controls
            If Con.GetType Is C.GetType Then 'Counts the controls of the same type
                Index += 1
            End If
        Next
        C.Name = C.GetType.Name
        C.Text = C.GetType.Name + (Index + 1).ToString 'New Index
        Controls.Add(C)
        C.BringToFront()
    End Sub
This isnt just for buttons and it does nothing with the position on the form.
But to use it
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddControl(New Button)
        AddControl(New TextBox)
        AddControl(New CheckBox)
        AddControl(New Label)
    End Sub
It'l scan and count all the controls of the same type on the form to get a new index value.
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us