列表视图的问题 [英] Problems with listview

查看:96
本文介绍了列表视图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好((>





如何在列表视图中使用selectedIndex?



之前我在Visual Basic中创建了一个列表框的播放列表,但最终决定将其更改为列表视图,但在最后一行中无法做到这一点



我如何在列表视图中更改或使用selectedIndex?



我尝试过:



Hello everyone (:


how I can use a selectedIndex in a listview?

previously I create a playlist a listbox in visual basic but eventually decided to change it for a listview but have trouble doing this in the last line

how I can change or use the selectedIndex in a listview?

What I have tried:

Private Sub ListaDeReproduccionToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ListaDeReproduccionToolStripMenuItem.Click
        On Error Resume Next
        ListView1.Items.Clear()
        OpenFileDialog1.Filter = "Lista de Reproducción (*.mpl4)|*.mpl4"
        OpenFileDialog1.ShowDialog()
        Dim rutaFichero As String
        rutaFichero = OpenFileDialog1.FileName
        If IO.File.Exists(rutaFichero) = True Then
            Dim fichero As New IO.StreamReader(rutaFichero)
            While (fichero.Peek() > -1)
                ListView1.Items.Add(fichero.ReadLine)
            End While
            fichero.Close()
        End If
        Me.ListView1.SelectedIndex = Me.ListView1.SelectedIndex + 1 'line where the error appears me'
    End Sub

推荐答案

首先要做的事情:摆脱 On Error Resume Next - 这是一种堕胎,它会隐藏问题直到它们变得太大而不能再忽略了。如果你没有及早发现错误,它们就会增长和增长,直到你不知道它究竟是什么原因造成的。

其次,摆脱流读取器和循环,并使用File.ReadAllLines和ListView.Items.AddRange代替在一个操作中执行。

但是尝试设置ListView的SelectedIndex,你只是根据当前值清空并重新填充(这个阶段可能是负面的) )是一个奇怪的想法 - 我会说你应该只检查ListView包含的项目,如果是这样,将它设置为零。
First things first: get rid of On Error Resume Next - it's an abortion which hides problems until they become too big to ignore any more. If you don't detect the errors early, they grow and grow, until you have no idea what actually caused it.
Second, get rid of the stream reader and loop, and use File.ReadAllLines and ListView.Items.AddRange instead to do it in one operation.
But trying to set the SelectedIndex of a ListView you just emptied and refilled based on the "current value" (which is probably negative by this stage) is an odd idea - I'd say you should just check the ListView contains items, and if so set it to zero.


这篇关于列表视图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆