【问题标题】:Will not display in list box VB不会在列表框中显示 VB
【发布时间】:2013-05-04 21:46:07
【问题描述】:

所以,我正在尝试显示足球队得分的统计数据。 在执行此操作时,数组已经被填充,什么还没有。 当此表单打开时,我希望它显示最高、最低和平均分数.... 我希望它得到球员的名字和得分的最大值和最小值。例如:

    Maximum: John scored 9

    Minimum: Joe scored 2

例如,我会在 strPlayers(i) 中获得值作为名称,在 intScores(i) 中获得值。 我很确定我的功能是正确的,但是,无论出于何种原因,我无法让它在加载表单时在列表框中显示任何内容!

Public Class frmDisplayStatistics

Function FindMaximum() As String
    Dim max As Integer
    Dim i As Integer = 0

    ReDim intScores(intNumberOfPlayers)

    max = CInt(intScores(0))
    For i = 0 To intNumberOfPlayers
        If max < intScores(i) Then
            max = CInt(intScores(i))
        End If
    Next


    max = strPlayers(i) & " scored maximum points of " & intScores(i)


    Return max

End Function

Function FindMinimum() As Integer
    Dim min As Integer
    Dim i As Integer = 0

    ReDim intScores(intNumberOfPlayers)

    min = CInt(intScores(0))
    For i = 0 To intNumberOfPlayers

        If min > intScores(i) Then
            min = CInt(intScores(i))


        End If

    Next


    Return min

End Function

Function FindAverage() As Double
    Dim average As Double
    Dim i As Integer = 0


    average = total / intNumberOfPlayers

    Return average

End Function

Private Sub frmDisplayStatistics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim max As String
    max = FindMaximum()
    lstStatistics.Items.Add(max)
    lstStatistics.Items.Add("Minimum: " & FindMinimum())
    lstStatistics.Items.Add("Average: " & FindAverage())



End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    Me.Close()
End Sub
End Class

最大值返回一个字符串而最小值和平均值返回一个数字的原因是因为我尝试了一种不同的方法,但也没有用。 :/

【问题讨论】:

  • 表单加载事件中变量 max 的值是多少?
  • 我不知道,因为它不显示任何内容。
  • 我很确定函数返回正确的最大值和最小值,我只是不知道如何根据数组显示它们!
  • 好的,所以它返回你的数组。我认为你不能像这样添加数组。你需要循环它们

标签: arrays vb.net function vba listbox


【解决方案1】:

假设您在表单加载事件中获取变量 max 中的数组。然后你应该循环数组。如下所示

for i = 0 to max.count -1
     listbox.item.add(i)
 next

您还需要将变量 max 声明为数组。希望你明白我的意思

【讨论】:

  • 数组是 strPlayers(intNumberOfPlayers) 和 intScores(intNumberOfPlayers)。 max、min 和 average 是代表球员得分的最大值、最小值和平均值的整数值。
  • 您说它正在返回您的数组。我问你在变量 max 中得到什么值
猜你喜欢
  • 2021-11-23
  • 1970-01-01
  • 2014-02-09
  • 2021-07-20
  • 2016-09-18
  • 1970-01-01
  • 2015-09-08
  • 2014-04-28
  • 1970-01-01
相关资源
最近更新 更多