简体   繁体   English

列表框中仅显示一个输入的文本框值

[英]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 可以在这里看到: http : //i.stack.imgur.com/kCqrz.png

Here is the full form code: http://pastebin.com/MDb1hSCA 这是完整的表单代码: 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. 由于某种原因,它仅添加Close值。

You never set the value of your column index variables (viz. openValue , closeValue , highValue , lowValue ). 您永远不会设置列索引变量的值(即openValuecloseValuehighValuelowValue )。 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. 但是,默认情况下, Dim会将字段声明为公共字段,由于这可能不是您真正想要的,我建议将其更改为私有字段。 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. 然后,使用List(T)对象而不是数组来存储项目列表。 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. 正如您所看到的那样,以这种方式进行操作可以使文档更加自我记录,减少混乱并减少错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对列表框中的文本框输入的值求和? - how to sum values entered by textbox in listbox? 当从列表框中选择“未完成”时,我想在文本框中输入0 - when “Not complete” is selected from a listbox i want 0 to be entered into a textbox VB:只允许数字(不是字母)输入文本框 - VB: Allowing Only Numbers (not Letters) To Be Entered Into Textbox 检查列表框中是否已存在文本框值 - check if a textbox value already exists in a listbox 仅更改用户在AJAX扩展文本框中输入的字符串的文本颜色 - change text color only of user entered string in AJAX extended textbox VB.net中的验证器或表达式,以检查是否在该文本框中输入了值 - Validator or Expression in VB.net to check if the value entered in that textbox 仅在一行上书写的文本框 - TextBox writing on one line only 逐行查看 TextBox1 中 RichTextBox 的每一行,如果第二行出现在 TextBox1 中,第一行就会消失 - view each line of RichTextBox in TextBox1 one by one if second line appears to TextBox1 the First line will disappear 使用一种方法在单个文本框中添加数字并将值返回到列表框 - Adding up numbers in single textbox and returning value to listbox using a method Visual Basic 2008-从列表框中获取特定值并显示在文本框中 - Visual Basic 2008 - Getting a certain value from a listbox and displaying in a textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM