Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
 
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
 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 17-Apr-13 23:00pm
Comments
Member 9989624 at 18-Apr-13 4:02am
   
Maciej Los need your help badly
Maciej Los at 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 at 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 at 18-Apr-13 8:44am
   
Maciej Los any help pls
pdoxtader at 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?
Member 9989624 at 18-Apr-13 9:42am
   
i have created textboxes and buttons dynamically based on the number of items in listbox in form load.
 
Upon clicking the Button the value of textbox will get incremented ( 1,2,3,4,5,6....... so on..)
 
ther is no issue till now.
 
Now i have created Button manually in the form. what i need is when i click that button the current value in the dynamically created text box should get updated to listbox.
 
there are totally 2 set like below
 
This set of textbox is names as txt(i) This set of textbox is names as suptxt(i)
Button Text Button Button Text Button
Button Text Button Button Text Button
 
Please help me.
Kschuler at 18-Apr-13 10:50am
   
Your code is very complicated and confusing. You say what you need is "when i click that button the current value in teh dynamically created text box should get updated to listbox." But what specifically are you stuck on? Are you having trouble getting the event to fire? Or getting items into the list box? I see that you have added event handlers, but you haven't given us the code for those events. Please supply that code and tell us why it isn't working. What it IS doing, what you WANT it to do. Any error messages.
Sergey Alexandrovich Kryukov at 13-Jun-13 0:25am
   
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Ok, So your event handler for a button click will look something like this:
 
   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:
 
   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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I got it resolved. Thanks for your time.
  Permalink  

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100