VB.NET中如何删除listbox中的最大值和最小值?

如题所述

max是要保存最大值的,你每次改变ID = i的时候都没有更新最大值max,所以要改:
For i = 0 To ListBox1.Items.Count - 1
If Val(ListBox1.Items(i)) > max Then
max=CInt(ListBox1.Items(i))
ID = i
End If
Next i
还有你的ID更新也有问题,一开始的时候似乎应该是ID=0,而不是ID=1。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-19
ListBox1.Items.Remove(63)追问

是取随机数后,有个按钮可以删除最大最小值

第2个回答  2015-05-19
listbox中是什么?整数?实数?字符串?追问

是整数

追答    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim max As Integer = 0
        Dim min As Integer = 0
        For i = 0 To ListBox1.Items.Count - 1
            If Val(ListBox1.Items(max)) < Val(ListBox1.Items(i)) Then max = i
            If Val(ListBox1.Items(min)) > Val(ListBox1.Items(i)) Then min = i
        Next
        If min > max Then
            ListBox1.Items.RemoveAt(min)
            ListBox1.Items.RemoveAt(max)
        Else
            ListBox1.Items.RemoveAt(max)
            ListBox1.Items.RemoveAt(min)
        End If
    End Sub

本回答被提问者采纳
相似回答