Click here to Skip to main content
15,895,011 members

Populating the Text values to Listbox

Member 9989624 asked:

Open original thread
Hi Team,


I need the values of the textbox to be moved to Listbutton on button click.( Buton and Listbox are created manually).

For your information i have created textboxes dynamically on form run. I need those values to be sent to listbox.

My code is below

VB
Dim but As Button = Nothing
        Dim but2 As Button = Nothing
        Dim but3 As Button = Nothing
        'Dim txt As TextBox = Nothing
        Dim supbut As Button = Nothing
        Dim supbut2 As Button = Nothing
        Dim suptxt As TextBox = Nothing
        Dim abbut As Button = Nothing
        Dim abbut2 As Button = Nothing
        Dim abtxt As TextBox = Nothing
        Dim inbut As Button = Nothing
        Dim inbut2 As Button = Nothing
        Dim intxt As TextBox = Nothing
        Dim lbl As Label = Nothing



        For Me.i = 1 To ListBox1.Items.Count

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

            txt = New TextBox
            With txt
                .Location = New Point(545, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "TextBox" & i
                .Width = 24
                .Parent = Me
            End With

            but2 = New Button
            With but2
                .Location = New Point(590, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "Button" & i
                .Width = 24
                .Text = "-"
                .Parent = Me
                AddHandler but2.Click, AddressOf oBtn2_Click
            End With
        Next i
        For Me.j = 1 To ListBox1.Items.Count
            supbut = New Button
            With supbut
                .Location = New Point(670, 150 + (j * 25))
                .Tag = j.ToString
                .Name = "Button" & j
                .Width = 24
                .Text = "+"
                .Parent = Me
                AddHandler supbut.Click, AddressOf osupBtn_Click
            End With

            suptxt = New TextBox
            With suptxt
                .Location = New Point(715, 150 + (j * 25))
                .Tag = j.ToString
                .Name = "SupTextBox" & j
                .Width = 24
                .Parent = Me
            End With

            supbut2 = New Button
            With supbut2
                .Location = New Point(760, 150 + (j * 25))
                .Tag = j.ToString
                .Name = "Button" & j
                .Width = 24
                .Text = "-"
                .Parent = Me
                AddHandler supbut2.Click, AddressOf osupBtn2_Click
            End With
        Next j

        For Me.k = 1 To ListBox1.Items.Count
            abbut = New Button
            With abbut
                .Location = New Point(840, 150 + (k * 25))
                .Tag = k.ToString
                .Name = "Button" & k
                .Width = 24
                .Text = "+"
                .Parent = Me
                '  AddHandler abbut.Click, AddressOf oabBtn_Click
            End With

            abtxt = New TextBox
            With abtxt
                .Location = New Point(885, 150 + (k * 25))
                .Name = "TextBox" & k
                .Width = 24
                .Tag = k.ToString
                .Parent = Me
            End With

            abbut2 = New Button
            With abbut2
                .Location = New Point(930, 150 + (k * 25))
                .Tag = k.ToString
                .Name = "Button" & k
                .Width = 24
                .Text = "-"
                .Parent = Me
                'AddHandler abbut2.Click, AddressOf oabBtn2_Click
            End With
        Next k

        For l = 1 To ListBox1.Items.Count
            inbut = New Button
            With inbut
                .Location = New Point(1010, 150 + (l * 25))
                .Tag = l.ToString
                .Name = "Button" & l
                .Width = 24
                .Text = "+"
                .Parent = Me
                '  AddHandler inbut.Click, AddressOf oinBtn_Click
            End With

            intxt = New TextBox
            With intxt
                .Location = New Point(1055, 150 + (l * 25))
                .Name = "TextBox" & l
                .Width = 24
                .Tag = l.ToString
                .Parent = Me
            End With

            inbut2 = New Button
            With inbut2
                .Location = New Point(1095, 150 + (l * 25))
                .Tag = l.ToString
                .Name = "Button" & l
                .Width = 24
                .Text = "-"
                .Parent = Me
                '  AddHandler inbut2.Click, AddressOf oinBtn2_Click
            End With
        Next l

    End Sub


listbox display snippet
VB
Dim ctl As Control = Me.GetNextControl(Me, True)
       Do
           If ctl Is txt Then
               ListBox2.Items.Add(ctl.Text)
           End If
           ctl = Me.GetNextControl(ctl, True)
       Loop Until ctl Is txt
       ListBox2.Items.Add(txt.Text)


the listbox snippet works, but ts getting all text box values to listbox. i need to add only the particular set of textbox's value to one listbox and others in separate listbox.

Listbox2 Listbox3

but text but but text but
but text but but text but
but text but but text but
but text but but text but
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