# VB is driving me mad



## jayhusky (Oct 17, 2009)

Does anyone know to make VB take an entry from a textbox and load it into Label1 and then re-use the same textbox and load the new value into Label2 and re-use again the same textbox and load into Label3

Every resource I've looked at hasn't helped me.

Any help is really appreciated

Thanks
Jay


----------



## net-cat (Oct 17, 2009)

Been years since I used VB. (Lol VB4.)

Why not use a List control?


----------



## jayhusky (Oct 17, 2009)

*facepalm*

didn't think about that..
hmm will give it a shot..


----------



## Stratelier (Oct 17, 2009)

I used to program in VB4 and 6.  I'm not sure exactly what you're trying to do, but...

Sounds so far like you basically need to transfer your string from (Textbox).Text to (Label).Caption .  But without a clear picture of what exactly you're trying to do and why, I can't be of much help here.  net-cat is right, you could also use something along the lines of a Listbox with code to the tune of "(Listbox).AddItem (Textbox).Text", then the Listbox's contents can be accessed via its List property methods.


----------



## jayhusky (Oct 18, 2009)

Basic idea of it is an array calculator that uses 2 buttons 1 TextBox and 7 labels

Enter a number in Textbox and click button "enter" (repeat process 5 times)
Once label 6 has been filled with a value greater than "" (NULL) then click on button "Total"

On clicking the button it adds all the values of the Labels 1 to 6 and provides the answer in Label7

Example:

[TEXTBOX]
              [Enter] - [Total]

[Label 1][Label 2][Label 3]
[Label 4][Label 5][Label 6]

            [Label 7]


Hopes this helps a little.


----------



## Rostam The Grey (Oct 18, 2009)

Depends on what version you're using. But you're probably looking for something like:


```
Public Function AddText(ByVal String As Text)
 If lbl1.Text = "" Then
  lbl1.Text = Text
 ElseIf lbl2.Text = "" Then
  lbl2.Text = Text
 .....
 Else
  lbl7.Text = CStr(CInt(lbl1.Text) + .......)
 End If
End Function
```

Sorry if I'm lazy but you should get the picture. It would be easier to do with a ListBox though. 


```
Public Function AddText(ByVal String As Text)
 If lbMine.Items.Count < 6 Then
  lbMine.Items.Add(Text)
 Else
  Dim Total As Integer
  Total = 0
  For I = 0 To 6
   Total = Total + lbMine.Items(I)
  Next
  lbl7.Text = Total
End Function
```

Of course you could allow them to put any number of values if it's a list box and have 2 buttons, one to add a value and one to total. Or just total every time a value is added.


----------



## Stratelier (Oct 19, 2009)

jayhusky said:


> Basic idea of it is an array calculator that uses 2 buttons 1 TextBox and 7 labels...
> Example:
> 
> [TEXTBOX]
> ...



Okay then, some tips:

1 - Give Label #1 an Index of zero, and Labels 2 thru 6 the same Name as the firs label.  This makes them a _control array_ which you can access using a *For Each* type of loop.  This means you don't have to daisy-chain If ... ElseIf statements together for each and every label on the form, you can just use a single loop to iterate through them all (and regardless of how many -- one, two, six, or sixty does not matter)

2 - Pseudocode with #1 in mind.  I'll use double-parentheses to identify placeholders for your actual control names:

```
Public Function ((Button))_Click()

Dim sumValues As Integer

' Loop through our six (or however many) labels.
' The moment we find a blank one, fill it in from our Textbox.
' If there's another blank one left over after that, exit for now
' so the user can input their next number.
For Each item In ((Label1))

  ' Check that the textbox has a number in it (note that blank string = not a number)
  If Not IsNumeric( ((Textbox)).Text) Then Exit Function

  ' If a label is currently empty, fill it in from our textbox.
  If item.Caption = "" Then
    item.Caption = ((Textbox)).Text
    ((Textbox)).Text = ""  ' Clear the textbox for the next number entry
  End If

  ' And since we're already inside a loop, let's start calculating the sum as we go.
  sumValues = sumValues + CInt( item.Caption)

Next item

' If we reach this point, all six labels were filled and we have our sum.
' If there were still empty labels remaining, the Exit Function would've fired instead.

((Label7)).Caption = sumValues

End Function
```


----------



## Rostam The Grey (Oct 21, 2009)

LOL, I forgot about control arrays. I never see them anymore so I wonder if you can still do them in .Net?

Apparently not...


----------



## jayhusky (Oct 31, 2009)

Sorry for the long wait for a reply back but i've been a busy person with things at the moment.

I put this code together and it works (enter a number in text box, hit enter it puts it into label 1 and moves on etc etc, the total label calculates as you go along, but for some reason takes the number of the label into account 

for example

Label 1 = 6
 (Total Label = 6)
Label 2 = 7
(Total Label = 6 + 7 x 2)

Its taking whatever number you enter into the box, adding it to the right label and then when its putting it into the total label it takes the label number and multiplies it by it.

any way heres the code if you want to try it out for yourself
(I use VB 2008 Express edition)


```
Public Class Form1
    Dim NumEntry(19) As Double
    Dim EntryCount As Integer
    Dim Total As Double = 0
    Dim Valid As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then MessageBox.Show("Entry Not Supplied") : Valid = 0

        If TextBox1.Text > "" Then NumEntry(EntryCount) = TextBox1.Text : EntryCount = EntryCount + 1 : Valid = 1

        Label1.Text = NumEntry(0) : Label2.Text = NumEntry(1) : Label3.Text = NumEntry(2) : Label4.Text = NumEntry(3) : Label5.Text = NumEntry(4) : Label6.Text = NumEntry(5) : Label7.Text = NumEntry(6) : Label8.Text = NumEntry(7) : Label9.Text = NumEntry(8) : Label10.Text = NumEntry(9) : Label11.Text = NumEntry(10) : Label12.Text = NumEntry(11) : Label13.Text = NumEntry(12) : Label14.Text = NumEntry(13) : Label15.Text = NumEntry(14) : Label16.Text = NumEntry(15) : Label17.Text = NumEntry(16) : Label18.Text = NumEntry(17) : Label19.Text = NumEntry(18) : Label20.Text = NumEntry(19)

        For x = 0 To 19
            Total = NumEntry(x) + Total
        Next
        Label21.Text = Total

    End Sub
End Class
```


----------



## LizardKing (Oct 31, 2009)

Oh god what is this

You obviously know how to do a for loop, yet you have that huge long line

Why god why


----------



## jayhusky (Oct 31, 2009)

I'm not actually sure why i used the long line there.
Once I figure out the issue behind the total number i'm going to tweak it up to make it better.


----------



## LizardKing (Oct 31, 2009)

Oh I just noticed those were globals

You're updating the total after every press for each one, including all the previous updates

I'm assuming you're pressing that like 20 times or something

So on the 20th press, it's added NumEntry(0) to the total 20 times, NumEntry(1) 19 times, NumEntry(2) 18 times, etc.


----------



## Stratelier (Oct 31, 2009)

Rostam The Grey said:


> LOL, I forgot about control arrays. I never see them anymore so I wonder if you can still do them in .Net?
> 
> Apparently not...



That sucks.  My last few VB6 projects used control arrays _up the wazoo_, the code was simply too complicated and tedious without them.


----------



## jayhusky (Oct 31, 2009)

LizardKing said:


> Oh I just noticed those were globals
> 
> You're updating the total after every press for each one, including all the previous updates
> 
> ...




I see, any idea to combat this?


----------



## LizardKing (Oct 31, 2009)

Set Total to 0 first.

Outside the for loop, obviously.


----------



## Stratelier (Oct 31, 2009)

If your Total variable isn't used outside of the loop, make it a local variable instead of global, e.g. put the "Dim Total as Integer" statement in your Button1_Click event instead of your Form declarations.  That way it is initialized to zero every time Button1 is clicked.


----------



## LizardKing (Oct 31, 2009)

Or that


----------



## jayhusky (Oct 31, 2009)

Stratadrake said:


> If your Total variable isn't used outside of the loop, make it a local variable instead of global, e.g. put the "Dim Total as Integer" statement in your Button1_Click event instead of your Form declarations.  That way it is initialized to zero every time Button1 is clicked.



*facepalm*
why oh why did i not think of that.. 
just gonna try it..
will report back in a few.


*EDIT:* Stratadrake, Lizardking, you are ace. thats fixed the problem. Can't believe it was something so simple and i missed it.

Thank you so much xD


----------



## Stratelier (Nov 1, 2009)

For that matter, your "Valid" variable should be local to the Click event too.  Local variables are easier to manage than globals.

Also, if you're expecting numeric input from a textbox make sure you check it against IsNumeric() to be sure its text content can be converted to a number.  An extra sanity check on your input never hurts....


----------



## jayhusky (Nov 1, 2009)

Ok thanks stratadrake, I've put it all together and it works like a dream

Thanks again


----------



## Xaevo (Nov 1, 2009)

jayhusky said:


> Does anyone know to make VB take an entry from a textbox and load it into Label1 and then re-use the same textbox and load the new value into Label2 and re-use again the same textbox and load into Label3
> 
> Every resource I've looked at hasn't helped me.
> 
> ...


use C#


----------

