2010-06-09 6 views
1

txt 파일에 들어있는 데이터의 다른 부분을 다른 목록 상자 (양식의 다른 탭에 있음)에 표시하여 사용자가 특정 데이터 블록을 볼 수있게해야합니다. . 관심있는 txt 파일에 포함 된텍스트 파일을 목록 상자 모음으로 읽어들입니다.

데이터는 다음과 같습니다

G30:39:03:31 JG06 
G32:56:36:10 JG04 
G31:54:69:52 JG04 
G36:32:53:11 JG05 
G33:50:05:11 JG06 
G39:28:81:21 JG01 
G39:22:74:11 JG06 
G39:51:44:21 JG03 
G39:51:52:22 JG01 
G39:51:73:21 JG01 
G35:76:24:20 JG06 
G35:76:55:11 JG01 
G36:31:96:11 JG02 
G36:31:96:23 JG02 
G36:31:96:41 JG03 

그것을 :) 훨씬 더하지만

별도의리스트 박스는 첫 번째 정수 쌍 경기의 유일한 선을 포함합니다 그 목록 상자의 이름. 예를 들어, "G32"를 시작하는 모든 행은 G32 목록 상자에 추가됩니다.

나는 코드가 같은 시작할 것이라고 생각한다

private void ReadToBox() 
    { 
     FileInfo file = new FileInfo("Jumpgate List.JG"); 
     StreamReader objRead = file.OpenText(); 
     while (!objRead.EndOfStream) 

을하지만 난 아직 분류지고의 측면에서 어디서부터 시작 모르겠어요.

어떤 도움이 필요합니까? 당신을 위해 몇 가지 대표 있어요 : D

편집 :

private void ViewForm_Load(object sender, EventArgs e) 
    { 
     this.PopulateListBox(lstG30, "G30", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG31, "G31", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG32, "G32", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG33, "G33", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG34, "G34", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG35, "G35", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG36, "G36", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG37, "G37", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG38, "G38", ("Jumpgate List.JG")); 
     this.PopulateListBox(lstG39, "G39", ("Jumpgate List.JG")); 
    } 

    void PopulateListBox(ListBox lb, string prefix, string textfile) 
    { 
     string[] filestrings = textfile.Split(Environment.NewLine.ToCharArray()); 
     foreach(string line in filestrings) 
     { 
      if (line.StartsWith(prefix)) 
       lb.Items.Add(line); 
     } 
    } 
+1

Arcadian에서는 메소드의 'textfile' 인수가 파일 이름 자체가 아닌 파일로 간주됩니다. 목록 상자를 채우기 전에 파일을 문자열로 읽습니다 (예 :'string s = String.Empty; using (StreamReader sr = File.OpenText (filename)) {s = sr.ReadToEnd(); } ' – JYelton

+0

그랬습니다. 모든 도움에 감사드립니다. – Arcadian

+0

당신을 진심으로 환영합니다. 행운을 빕니다! – JYelton

답변

2

일부 반 의사 ... 각 목록 상자를 채우는 부를 것이다 방법. 목록 상자 컨트롤, 당신은 분리 할 접두사 및 입력 파일을 지정합니다

void PopulateListBox(ListBox lb, string prefix, string[] textfile) 
{ 
    foreach(string line in textfile) 
    { 
     if (line.StartsWith(prefix)) 
     lb.Add(line); 
    } 
} 

편집 :이 방법은 하나의 문자열 (보다는 문자열 배열을 기대)로 파일을 처리

:

void PopulateListBox(ListBox lb, string prefix, string textfile) 
{ 
    string[] filestrings = textfile.Split(Environment.NewLine.ToCharArray()); 
    foreach(string line in filestrings) 
    { 
     if (line.StartsWith(prefix)) 
     lb.Add(line); 
    } 
} 
+0

나는 그것이 그렇게 될 것이라고 생각했다. 각 목록 상자에 대해 새로운 메서드를 가져야한다는 의미는 아닙니다. 더 빠른 방법이 있기를 바랬습니다. – Arcadian

+1

아니요, 메서드 호출에서 채울 목록 상자의 이름을 제공합니다. 따라서 목록 상자의 이름을 'lstbG32'로 지정한 경우 다음과 같이하십시오.'PopulateListBox (lstbG32, "G32", txt); ' – JYelton

+0

미안, 금발의 순간 거기에서 나는 그것을 지금 얻는다. foreach 부분에서 길드 오류가 발생하지만 문자열을 char로 변환 할 수 없습니다. – Arcadian