Click here to Skip to main content
15,878,871 members

Handling Dynamic Button and Textbox in VB.Net

Member 9989624 asked:

Open original thread
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
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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