Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All

Need your advice in getting my code corrected.

I have dynamic buttons and textbox created.

I need the textbox value to be incremented by 1 for every click of the corresponding button.

for design template

Button Textbox Button

+ ( text ) -


On clicking + value should increase one by one. ( for every click on + )

below is my code which is pointing to my last textbox alone. Please Advice.

VB
  Dim i As Integer
    For i = 1 To ListBox1.Items.Count
        but = New Button
        but.Location = New Point(500, 150 + (i * 25))
        but.Name = "Button" & i
        but.Size = buttonSize
        but.Text = "-"
        Me.Controls.Add(but)
        AddHandler but.Click, AddressOf but_click
    Next i
Dim lbl As Label
Dim j As Integer
    For j = 1 To ListBox1.Items.Count
        For Each listboxitem In ListBox1.Items
            lbl = New Label
            lbl.Location = New Point(300, 150 + (j * 25))
            lbl.Name = "Label" & j
            lbl.Size = labelSize
            lbl.Text = ListBox1.GetItemText(listboxitem)
            Me.Controls.Add(lbl)
            j = j + 1
        Next listboxitem
    Next j
Dim but2 As Button

    For Me.k = 1 To ListBox1.Items.Count
        but2 = New Button
        but2.Location = New Point(590, 150 + (k * 25))
        but2.Name = "Button" & k
        but2.Size = buttonSize
        but2.Text = "+"
        Me.Controls.Add(but2)
    Next k
    Me.k = 1

    For Me.l = 1 To ListBox1.Items.Count
        txt = New TextBox
        txt.Location = New Point(545, 150 + (l * 25))
        txt.Name = "TextBox" & l
        txt.Size = textSize
        txt.Text = ""
        Me.Controls.Add(txt)
        'AddHandler txt.Click, AddressOf txt_click
    Next l

Dim supbut As Button

    For Me.si = 1 To ListBox1.Items.Count
        supbut = New Button
        supbut.Location = New Point(670, 150 + (si * 25))
        supbut.Name = "Button" & si
        supbut.Size = buttonSize
        supbut.Text = "-"
        Me.Controls.Add(supbut)
    Next si

Dim supbut2 As Button
Dim sk As Integer
    For sk = 1 To ListBox1.Items.Count
        supbut2 = New Button
        supbut2.Location = New Point(760, 150 + (sk * 25))
        supbut2.Name = "Button" & sk
        supbut2.Size = buttonSize
        supbut2.Text = "+"
        Me.Controls.Add(supbut2)
    Next sk
Dim suptxt As TextBox
Dim sl As Integer
    For sl = 1 To ListBox1.Items.Count
        suptxt = New TextBox
        suptxt.Location = New Point(715, 150 + (sl * 25))
        suptxt.Name = "TextBox" & sl
        suptxt.Size = textSize
        suptxt.Text = "TextBox" & sl
        Me.Controls.Add(suptxt)
    Next sl

Dim abbut As Button
Dim ai As Integer
    For ai = 1 To ListBox1.Items.Count
        abbut = New Button
        abbut.Location = New Point(840, 150 + (ai * 25))
        abbut.Name = "Button" & ai
        abbut.Size = buttonSize
        abbut.Text = "-"
        Me.Controls.Add(abbut)
    Next ai
Dim abbut2 As Button
Dim ak As Integer
    For ak = 1 To ListBox1.Items.Count
        abbut2 = New Button
        abbut2.Location = New Point(930, 150 + (ak * 25))
        abbut2.Name = "Button" & ak
        abbut2.Size = buttonSize
        abbut2.Text = "+"
        Me.Controls.Add(abbut2)
    Next ak
Dim abtxt As TextBox
Dim al As Integer
    For al = 1 To ListBox1.Items.Count
        abtxt = New TextBox
        abtxt.Location = New Point(885, 150 + (al * 25))
        abtxt.Name = "TextBox" & al
        abtxt.Size = textSize
        abtxt.Text = "TextBox" & al
        Me.Controls.Add(abtxt)
    Next al

Dim inbut As Button
Dim ii As Integer
    For ii = 1 To ListBox1.Items.Count
        inbut = New Button
        inbut.Location = New Point(1010, 150 + (ii * 25))
        inbut.Name = "Button" & ii
        inbut.Size = buttonSize
        inbut.Text = "-"
        Me.Controls.Add(inbut)
    Next ii

Dim inbut2 As Button
Dim ik As Integer
    For ik = 1 To ListBox1.Items.Count
        inbut2 = New Button
        inbut2.Location = New Point(1095, 150 + (ik * 25))
        inbut2.Name = "Button" & ik
        inbut2.Size = buttonSize
        inbut2.Text = "+"
        Me.Controls.Add(inbut2)
    Next ik

Dim intxt As TextBox
Dim il As Integer
    For il = 1 To ListBox1.Items.Count
        intxt = New TextBox
        intxt.Location = New Point(1055, 150 + (il * 25))
        intxt.Name = "TextBox" & il
        intxt.Size = textSize
        intxt.Text = "TextBox" & il
        Me.Controls.Add(intxt)
    Next il
End Sub

Private Sub but_click(sender As Object, e As EventArgs)

missing here what to do??
when i use msgbox on button it gives me message that button i click but how to add the value to appropriate text box? :( please help
End Sub
Posted
Updated 15-Apr-13 6:40am
v3
Comments
[no name] 15-Apr-13 12:39pm    
You would have to find the "appropriate" text box, whatever that means.
Member 9989624 15-Apr-13 12:43pm    
any advice how to get it?. i have set names for each textbox's, however only last textbox value is getting incremented.
[no name] 15-Apr-13 12:49pm    
Use a for loop, for each loop or a LINQ query to find whatever you think is the appropriate textbox.
Member 9989624 15-Apr-13 12:57pm    
Thanks.tried for loop. am struck in incremental with the control variable. still its pointing the last textbox only.
[no name] 15-Apr-13 13:30pm    
I have no idea what it is that you mean. You did not post any such code.

1 solution

First of all, your code is not optimized. And i can only ask you one question: How many for - next loops you want to use, if every time you iterate through the collection of LisBox1.Items?

Second, use With - End With block to set many properties for one control.

Third, use simple trick:
VB
Public Class Form1

    WithEvents oBtnAdd As Button
    WithEvents oBtnSubst As Button


    Private Sub CreateDynamicControls()
        Dim but As Button = Nothing
        Dim txt As TextBox = Nothing

        Dim i As Integer

        For i = 1 To 10
            but = New Button
            With but
                .Location = New Point(500, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonSubst" & i
                .Width = 24
                .Text = "-"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnSubst_Click
            End With

            txt = New TextBox
            With txt
                .Location = New Point(525, 150 + (i * 25))
                .Name = "TextBox" & i
                .Tag = i.ToString
                .Width = 45
                .Text = 0
                .Enabled = False
                .Parent = Me
            End With

            but = New Button
            With but
                .Location = New Point(571, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonAdd" & i
                .Width = 24
                .Text = "+"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnAdd_Click
            End With


        Next i
    End Sub

    Public Sub oBtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnAdd.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count + 1
    End Sub

    Public Sub oBtnSubst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnSubst.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count - 1
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        CreateDynamicControls()
    End Sub
End Class
 
Share this answer
 
v3
Comments
Member 9989624 15-Apr-13 13:31pm    
Thanks a lot Maciej Los. I will implement it to me code. Working Really great:)
Maciej Los 15-Apr-13 13:33pm    
You're welcome ;) Call again.
If it was helpful, please, vote ;)
Member 9989624 15-Apr-13 13:44pm    
one doubt here maciej. everything is fine.

i added this below

Dim btn As Button = sender
Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
count = count + 1
txt.Text = count

instead typing the button text to textbox. the value should keep incremental on clicking the button. but what happens when i add above is. if i press first button once the text vale is 1 and gets incremented with no issues. when i click the next button the textbox value gets continued from the previous text box. i need each text box should hold its own increment value when i press the button. please help
Maciej Los 15-Apr-13 14:00pm    
Where is a problem?
count = Int.Parse(txt.Text)
txt.Text = count +1
'or
'txt.Text = count -1
Maciej Los 15-Apr-13 14:02pm    
BTW, you need to define to handlers: one for Add and one for Substract action ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900