Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 列表框中只显示一个输入的文本框值_Vb.net_Visual Studio_Textbox_Listbox - Fatal编程技术网

Vb.net 列表框中只显示一个输入的文本框值

Vb.net 列表框中只显示一个输入的文本框值,vb.net,visual-studio,textbox,listbox,Vb.net,Visual Studio,Textbox,Listbox,简单的初学者问题在这里,去轻松。我有几个文本框,用户可以在其中输入值并选择日期,我希望它们显示在列表框中。不幸的是,只有第二个文本框的值出现多次。这可以在这里看到: 以下是完整的表单代码: 以下是将数据添加到阵列的位置: stockArray(nofDataDay, lowValue) = possibleLow stockArray(nofDataDay, highValue) = possibleHigh stockArray(nofDataDay, openValue) = possibl

简单的初学者问题在这里,去轻松。我有几个文本框,用户可以在其中输入值并选择日期,我希望它们显示在列表框中。不幸的是,只有第二个文本框的值出现多次。这可以在这里看到:

以下是完整的表单代码:

以下是将数据添加到阵列的位置:

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
下面是它的展示位置:

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

出于某种原因,它只添加了关闭值。

您从未设置列索引变量的值(即
openValue
closeValue
highValue
lowValue
)。它们都默认为零,因此您只需多次添加第一列。您可以在声明它们时设置它们的值,如下所示:

Dim lowValue As Integer = 0
Dim highValue As Integer = 1
Dim openValue As Integer = 2
Dim closeValue As Integer = 3
Dim item As New MyItem()
item.Date = Convert.ToDateTime(WeatherDateTimePicker.Text)
item.LowValue = possibleLow
item.HighValue = possibleHigh
' ...
myItems.Add(item)
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
您还需要将阵列声明为更大:

Dim stockArray(30, 3) As Integer
但是,默认情况下,
Dim
将字段声明为公共字段,并且由于这可能不是您真正想要的,因此我建议将它们更改为私有字段。此外,列索引实际上应该是常量:

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
然而,如果您能更好地设计代码,这种bug是不可能的。我建议创建一个类来存储单个项目的所有数据,而不是使用二维数组。然后,使用
List(T)
对象而不是数组来存储项目列表。例如:

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)()
然后,您可以添加如下项目:

Dim lowValue As Integer = 0
Dim highValue As Integer = 1
Dim openValue As Integer = 2
Dim closeValue As Integer = 3
Dim item As New MyItem()
item.Date = Convert.ToDateTime(WeatherDateTimePicker.Text)
item.LowValue = possibleLow
item.HighValue = possibleHigh
' ...
myItems.Add(item)
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
然后您可以从列表中读取如下项目:

Dim lowValue As Integer = 0
Dim highValue As Integer = 1
Dim openValue As Integer = 2
Dim closeValue As Integer = 3
Dim item As New MyItem()
item.Date = Convert.ToDateTime(WeatherDateTimePicker.Text)
item.LowValue = possibleLow
item.HighValue = possibleHigh
' ...
myItems.Add(item)
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
正如您所看到的,这样做更容易自我记录,更少混乱,更不容易出现错误