简体   繁体   中英

Only one textbox value entered appears in listbox

Simple beginners issue here, go easy. I've got a few text boxes that the user can put values into + pick a date, and I want them to appear in a list box. Unfortunately only the 2nd text box's value appears multiple times. This can be seen here: http://i.stack.imgur.com/kCqrz.png

Here is the full form code: http://pastebin.com/MDb1hSCA

Here's where the data is added to an array:

stockArray(nofDataDay, lowValue) = possibleLow
stockArray(nofDataDay, highValue) = possibleHigh
stockArray(nofDataDay, openValue) = possibleOpen
stockArray(nofDataDay, closeValue) = possibleClose
dateArray(nofDataDay) = Convert.ToDateTime(WeatherDateTimePicker.Text)
nofDataDay = nofDataDay + 1

And here's where it's displayed:

For day = 0 To nofDataDay - 1
    StockListBox.Items.Add(dateArray(day).ToShortDateString & _
        delimiter & stockArray(day, openValue).ToString & _
        delimiter & stockArray(day, closeValue).ToString & _
        delimiter & stockArray(day, highValue).ToString & _
        delimiter & stockArray(day, lowValue).ToString & _
        delimiter & AverageStock(stockArray(day, lowValue), stockArray(day, highValue)))
Next

For some reason, it's only adding the Close value.

You never set the value of your column index variables (viz. openValue , closeValue , highValue , lowValue ). They all default to zero, so you are in just adding the first column multiple times. You could set the value of them when you declare them, like this:

Dim lowValue As Integer = 0
Dim highValue As Integer = 1
Dim openValue As Integer = 2
Dim closeValue As Integer = 3

You'll need to declare your array larger too:

Dim stockArray(30, 3) As Integer

However, by default Dim declares fields as public, and since that's probably not what you really want, I would recommend changing them to private. Also, the column indexes really ought to be constants:

Private Const lowValue As Integer = 0
Private Const highValue As Integer = 1
Private Const openValue As Integer = 2
Private Const closeValue As Integer = 3
Private stockArray(30, 3) As Integer

However, this kind of bug would not be possible you designed your code better. Rather than using a two-dimensional array, I would recommend making a class that stores all the data for a single item. Then, rather than an array, use a List(T) object to store the list of items. For instance:

Public Class MyItem
    Public Date As Date
    Public LowValue As Integer
    Public HighValue As Integer
    Public OpenValue As Integer
    Public CloseValue As Integer
End Class

Private myItems As New List(Of MyItem)()

Then, you can add the items like this:

Dim item As New MyItem()
item.Date = Convert.ToDateTime(WeatherDateTimePicker.Text)
item.LowValue = possibleLow
item.HighValue = possibleHigh
' ...
myItems.Add(item)

And then you can read the items from the list like this:

For Each item As MyItem in myItems
    StockListBox.Items.Add(item.Date.ToShortDateString() & _
            delimiter & item.OpenValue.ToString() & _
            delimiter & item.CloseValue.ToString() & _
            delimiter & item.HighValue.ToString() & _
            delimiter & item.LowValue.ToString() & _
            delimiter & AverageStock(item.LowValue, item.HighValue))
Next

As you can see, doing it that way is much more self-documenting, less confusing, and less bug-prone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM