在 Visual Basic 中选择列表框项时切换 CVS 文本文件项出错 [英] Error switch CVS text file item when selecting listbox item in Visual Basic

查看:24
本文介绍了在 Visual Basic 中选择列表框项时切换 CVS 文本文件项出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将M"切换为Mr.".和F"到女士".在列表框中选择姓氏时.当我单击第一个名称时,它起作用了,但是当我单击任何其他名称时,我收到了以下错误消息:

I'm trying to switch "M" to "Mr." and "F" to "Ms." when the last name is selected in the listbox. When I clicked on the first name it worked, but when I clicked on anyother name, I got this error message:

--附加信息:索引超出了数组的范围.--

--Additional information: Index was outside the bounds of the array.--

文本文件中的信息是这样的:

The information in the text file is like this:

       Ball,Krystal,F,1981
       Banks,Robin,F,1988
       Burgher,Hamilton,M,1980
       Early,Brighton,M,1989
       Hedd,MT,M,1960
       Hogg,Ima,F,1953
       Knapp,Anita,F,1970
       Overnout,Roger,M,1968
       Psito,Arnie,M,1962
       Teak,Anne,F,1939

而我的代码如下:

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
    Dim names As IO.StreamReader = IO.File.OpenText("Info.txt")
    Dim lName As String = lstNames.Text
    Dim line As String
    Dim gender As String
    Dim foundFlag As Boolean = False
    Do Until foundFlag Or names.EndOfStream
        line = names.ReadLine
        If line.Split(","c)(2) = "M" Then
            gender = "Mr. "
        ElseIf line.Split(","c)(2) = "F" Then
            gender = "Ms. "
        End If
        If line.Split(","c)(0) = lName Then
            txtOutput.Text = gender & line.Split(","c)(1) & " " & line.Split(","c)(0) & " is " & 2012 - line.Split(","c)(3)
            foundFlag = True
        End If
    Loop

End Sub

谁能告诉我有什么问题.提前致谢.

Can someone please let me know what's wrong. Thanks in advance.

推荐答案

我重新创建了您的应用程序,完全按照您在此处的方式创建,并且运行良好.

I re-created your application exactly as you have it here and it worked jsut fine.

这让我相信两件事之一正在发生:

That leads me to believe one of 2 things is happening:

  1. 您的 Info.txt 文件中有错误字符
  2. 您尚未使用与 Info.txt 中相同的名称填充您的 lstNames 组合框.
  1. You have bad characters in your Info.txt file
  2. You haven't populated your lstNames combobox with the identical names to what you have in Info.txt.

PS - 您可能还想查看您的循环以提高效率:

PS - You might also want to look into your loop to make it more efficient:

    Dim line() As String
    Dim gender As String
    Dim foundFlag As Boolean = False
    Do Until foundFlag Or names.EndOfStream
        line = names.ReadLine.Split(","c)

        If line(0) = lName Then
            If line(2) = "M" Then
                gender = "Mr. "
            ElseIf line(2) = "F" Then
                gender = "Ms. "
            End If
            txtOutput.Text = gender & line(1) & " " & line(0) & " is " & 2012 - cint(line(3))
            foundFlag = True
        End If
    Loop

这篇关于在 Visual Basic 中选择列表框项时切换 CVS 文本文件项出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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