Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Member 9989624 18-Apr-13 4:02am    
Maciej Los need your help badly
Maciej Los 18-Apr-13 4:20am    
I don't have no idea what you want to achieve. Please, be more specific adn provide more details.
Do you want do add value from textbox on "Add" and "Substract" event (on buttonX_Click)?
Member 9989624 18-Apr-13 4:48am    
we the result in textbox when we add or subtract, i need that value to be displayed in listbox on clicking button.( this button is not dynamic. its manuall button created in form design).
Member 9989624 18-Apr-13 8:44am    
Maciej Los any help pls
pdoxtader 18-Apr-13 9:18am    
Yeah... You really need to be more specific. I can't tell what your trying to do either. What does "only the particular set" mean?

I got it resolved. Thanks for your time.
 
Share this answer
 
Ok, So your event handler for a button click will look something like this:

VB
Private Sub oBtn_Click(sender As System.Object, e As System.EventArgs)

End Sub


You can use this event handler for all your dynamically created buttons by testing for each button, like so:

VB
If typeof(sender) Is Button Then
   Dim thisButton As Button = DirectCast(sender, Button)

   If thisButton.Name = "the name your looking for" then
      ' Your code here
   End If

End If


While your enumerating all the controls on your form, you can look for your text boxes the same way. Test for a text box the way I did for a button above, and then check it's name once you cast it to a local textbox control. When you find the name your looking for, copy it's text to the listbox your want. If you are creating multiple text boxes, name them something like abtxt1, abtxt2, ect - and then test for substrings of the names. When you find an "abtxt" text box, then you know you've found one who's value you want to write to the list box...

Hope this helps.

- Pete

Oh, And I HAVE to say this - you should be using descriptive names and camel case. Readability is everything. Don't be afraid to use long names for your objects... instead of abtxt, you should be using something like aboutThisUserTextBox. This way when you come back to this code next year, or even longer you will be able to read it easily and understand what you were doing. If you ever find yourself working with a team, they will thank you for it.
 
Share this answer
 
v3

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