2012-07-12 4 views
0

희망 사람들이 도울 수 있습니다!텍스트 파일의 Combobox 값

구분 기호로 사용 된 구분 된 텍스트 파일의 정보로 채워진 콤보 상자를 만들려고합니다. IE

objecta, 대학원, N, 단어, 5, 정수/부울/문자열이 ... 그러나 나는이 작업을 수행하는 코드를 얻는 데 문제로 제대로 작동

내가, 사이의 다양한 분야를 필요로 moretext

Ive가 웹을 검색했지만이 코드가 있지만 제대로 작동하지 않습니다. 어쩌면 나는 바보이지만 희망을 갖고 누군가 나를 도울 수 있습니다. 인터넷의 오 전문가를 도와주세요!

Public Sub meritlist() 
    Dim looksups As New ArrayList 
    ' This call is required by the Windows Form Designer. 
    InitializeComponent() 

    Dim rdr As IO.StreamReader = IO.File.OpenText(c:merits.txt") 
    While Not rdr.EndOfStream 
     Dim line As String = rdr.ReadLine() 

     'Split the line by a comma: 
     Dim arrayList = line.Split(",") 

     'Get the right values: break down is as: Merit Name,Type,Shared,Carthan,Max,Pre-Req 
     Dim meritname As Integer = arrayList(0) 
     Dim merittype As String = arrayList(1) 
     Dim meritshared As Boolean = arrayList(2) 
     Dim carthan As String = arrayList(3) 
     Dim meritmax As Integer = arrayList(4) 
     Dim prereq As String = arrayList(5) 

     cmoMRDro.Items.Add(New lookups(arrayList(0), arrayList(1))) 

    End While 
    rdr.Close() 
End Sub 
+0

1.) 파일 경로가 잘못되었습니다. 그건 오타예요? 2)'objecta, grad, N'은'integer/boolean/string'에 속하지 않습니다. 예를 들어 'words, 5, moretext'는 문자열/정수/문자열' –

+0

1)이 페이지에 맞도록 변경되었지만 맞습니다. 2) YEah 나는 어떻게 보일지를 보여주기 위해 모범을 보였습니다. meritname, merittype, carthan 및 prereq는 모두 문자열입니다. 나는 실수로 잘못된 옵션이 아닌 N으로 공유를 넣는다는 것을 알게되었습니다. – AndrewMantis

답변

1

내가 제안 할 4 가지가 있습니다.

  1. 구분/고정 폭 텍스트 파일을 읽을 때는 TextFieldParser 클래스를 사용하십시오. 그들은이 일을하는 데 더욱 전문화되어 있으며, 이미 제대로 처리 할 수있는 코드를 가지고 있습니다.

  2. InitializeComponent에 대한 호출이 필요하지 않습니다.

  3. 원시 변수 대신 광고 항목을 저장할 구조/클래스를 만듭니다. 이 방법으로 당신은 그것을 더 조직적으로 할 수 있습니다. 대신에 몇 가지 수정을 보정 할 수있는 당신은 이미하고있다, 나는 모든 제안 I를 유지,이 방법을 수행 한 것이지만 ArrayList

의 구조/클래스의

  • 사용 List, 위에 줬다. 그러면 다음과 같이 쉽게 될 수 있습니다.

    Structure MeritListType 
        Dim meritname As Integer 
        Dim merittype As String 
        Dim meritshared As Boolean 
        Dim carthan As String 
        Dim meritmax As Integer 
        Dim prereq As String 
    
        Public Sub New(ByVal data() As String) 
         meritname = Integer.Parse(data(0)) 
         merittype = data(1) 
         meritshared = Boolean.Parse(data(2)) 
         carthan = data(3) 
         meritmax = Integer.Parse(data(4)) 
         prereq = data(5) 
        End Sub 
    End Structure 
    
    Public Sub MeritList() 
        Dim FileData As New List(Of MeritListType) 
        Dim tfp As New FileIO.TextFieldParser("c:\merits.txt") 
        tfp.TextFieldType = FileIO.FieldType.Delimited 
        tfp.SetDelimiters(",") 
        While Not tfp.EndOfData 
         FileData.Add(New MeritListType(tfp.ReadFields)) 
        End While 
        tfp.Close() 
    
        '' you have the records in FileData 
        '' do whaever you want to do with it here now 
    
    End Sub 
    
  • +0

    +1 TextFieldParser 제안 사항입니다. 나는 그것에 대해 절대적으로 동의합니다. –

    +0

    이 사용하려고 시도했지만 filedata.add에 오류가 있습니다 (새로운 meritlistype (ftp.readfields)) .. 생성자가 없습니다 .. – AndrewMantis

    +0

    'Public Sub New (ByVal 데이터 () As String)'구조를 사용합니다. 그걸 넣어 줘야 해. 또한 값을 올바른 데이터 유형으로 올바르게 변환 할 수 있도록이를 수정해야합니다. 예 : 필드에'F'가있을 수 있으며 부울로 직접 변환 할 수 없습니다. 따라서 해당 블록을 적절하게 수정해야합니다. :) –

    관련 문제