Click here to Skip to main content
12,395,645 members (81,186 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
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.

  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 = "-"
        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)
            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 = "+"
    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 = ""
        'AddHandler txt.Click, AddressOf txt_click
    Next l
Dim supbut As Button
    For = 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 = "-"
    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 = "+"
    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
    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 = "-"
    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 = "+"
    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
    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 = "-"
    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 = "+"
    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
    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? Frown | :( please help
End Sub
Posted 15-Apr-13 6:34am
Updated 15-Apr-13 6:40am
ThePhantomUpvoter 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.
ThePhantomUpvoter 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.
ThePhantomUpvoter 15-Apr-13 13:30pm
I have no idea what it is that you mean. You did not post any such code.
Sergey Alexandrovich Kryukov 17-Apr-13 15:40pm
Please, you should not re-post the same of similar question over and over. If you don't like the solution, you should add posts (comments, replies, "Improve question") on the page of your original post.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:
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.
        ' Add any initialization after the InitializeComponent() call.
    End Sub
End Class
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
'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 ;)
Maciej Los 15-Apr-13 14:11pm
Check my answer after update.
Member 9989624 15-Apr-13 14:14pm
Yea i had done those handlers. thanks a lot for your support. i got it done by adding this
txt.Text = Val(txt.Text) + 1.

That was really great to meet you. Am fully satisfied:)
Maciej Los 15-Apr-13 14:32pm
Use Int.Parse() or Int.TryParse() instead of Val() function.
Member 9989624 15-Apr-13 14:15pm
5 Star for you. You deserve it:)
Maciej Los 15-Apr-13 14:30pm
Thank you ;
Member 9989624 16-Apr-13 1:17am
Yea i used it. I have another set as same setup. ie)

but txt but but txt but
but txt but but txt but
but txt but but txt but.

how to handle the second set? i tried with the same however value not getting incremental at second set. its getting incremented at first set only.
Maciej Los 16-Apr-13 1:56am
Probably, the second set of buttons and textboxes has the same name as first set. Think, what you need to change to solve the problem.
Member 9989624 16-Apr-13 1:59am
i had checked, its unique name. could you please test and help me?
Maciej Los 16-Apr-13 2:11am
How can i help you, if i can't see your code after changes? I suggest you to debug "add" and "substract" events. If it won't help, please, post another question with current code.
Member 9989624 16-Apr-13 2:38am
Am sorry:).. posted a new question with code. Please assist
Sergey Alexandrovich Kryukov 16-Apr-13 23:53pm
My 4; I can see some minor problems:

1) use string.Format instead of string concatenation, for better performance and readability;

2) named event handlers in this case are ugly, use anonymous delegates which are well supported by VB.NET;

3) to me, this is a kind of offense: the names like oBtnAdd_Click violate (good) Microsoft naming conventions ('_' should not be used); it would be not bad, but the problem is: you mimic the naming of auto-generated code; you should understand that it's impossible to impose the same requirements to auto-generated code and the code written with understanding of semantic; (by the way: in fact, all auto-generated names should always be renamed at the end of development).

Maciej Los 17-Apr-13 3:21am
I knew it... i felt it... ;)
Thanks a lot for 4 stars, i think it's fair enough ;) Thank you for pointing me that out. I agree with all your suggestions and i'll promise to improve my answer. At that moment i can only "say" that was a few instructions addressed to OP how to correct His/Her code.

BTW, in my opinion it would be the best to write custom component/control to achieve that functionality.

Maciek (a diminutive of "Maciej") ;)
Sergey Alexandrovich Kryukov 17-Apr-13 11:48am
Me pleasure. I knew you are quite cooperative. :-)

Thank you for information on your name forms. You know, I'm familiar with some Polish name generation from early childhood, when I had a huge book of Polish folklore tales, by the way, very funny (or scary, in the chapter of scary fair tales), wonderful reading I remember very well so far. You name was always hard to spell, by the way :-) I just checked pronunciation in Wikipedia and was surprised that [ˈmatɕɛj]; I thought 'c' is pronounced as Polish 'c' or 'tʂ' (transliterated as English 'ts' or 'ch', Cyrillic 'ц' or 'ч'), but it says 'ɕ' ('sh', 'ш'). It it really so? Is the same in "Maciek"? However, from the very beginning I supposed it was derived from biblical Matthew, but Maciej is associated with later (Latinised?) form "Matthias". Interestingly, in Russian names, these (and other) different forms are considered different names. "Matthew" is "Матвей" but we also have old name "Макей". (Do you understand Cyrillic?)

Sorry for off-topic; I'm very interested in etymology and relationships between languages. (Tired to explain CodeProject members from India how their languages use their scripts and how to use Unicode. :-)

Maciej Los 17-Apr-13 12:50pm
Yes, i do really understand Cyrillic and yes, Maciej is derived from biblical Matthew. Later forms of my name -as much as i know - are: Mathias, Mathi, Matti and so on. I've heard that my name was said by "Russian-speaking" (?) /Ukrainian, Estonian/ people like: Матвей and Макей.
In Poland "ci" is similar (in speech) to "ć" (soft form of "ts" where the middle part of tongue is going up and - in the same time - the front of tongue is hiding behind the bottom teeth).
Note from the dictionary: In Polish orthography, the softness of the consonant is indicated by the diacritic "'" only at the end of a word or before a consonant. Before vowels it is marked by the letter "i".

Cheers, Brother ;)
Sergey Alexandrovich Kryukov 17-Apr-13 12:58pm
This is what I though ('ć', pretty much like 'ц'), so this Wikipedia article confused me (also check with Polish IPA for 'ɕ'):

I would say, you should register at Wikipedia and fix this mistake on this page. It's not good to fool honest polite people who want to address other people by name correctly. How about that?

Maciej Los 17-Apr-13 13:23pm
As you know, i'm not so familiar with English as i wish to be ;( I'll promise to study English and that... who knows ;)
Here is pretty good example how sounds "ć":[^]
Sergey Alexandrovich Kryukov 17-Apr-13 14:21pm
Are you saying that 'c' in your name is actually 't͡ɕ'? Thank you very much for the link.

Then the transcription in the 'Maciej' Wikipedia article is not accurate: 't͡ɕ' is not the same as 'ɕ'... Something should be fixed.
My knowledge of IPA is very limited (we used different transcription system in learning of "British" English), but I feel frustrated. On the main site, I can see that it is at least incomplete. I know some Turkic and even Cyrillic sounds which are not represented there (not even our 'h' which is very different from English 'h'); at the same time, many Indian consonants slightly different from English, are preserved (people from India typically use then when speaking English, instead of real English, which is quite natural). Maybe I'm missing something, but I have an impression of certain lameness in IPA...
Member 9989624 17-Apr-13 21:04pm
maciej los. need your help on this question "Adding the Dynamic textbox values to Listbox"
Maciej Los 18-Apr-13 2:01am
In which part?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2016
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