2017-01-11 1 views
0

그래서이 목록보기가 있고 openfiledialog를 통해 항목을 추가 할 수 있습니다. 그런 다음 File.ReadLine을 수행하고 텍스트 파일의 모든 줄을 읽습니다. 선택된.내 목록을 반복 할 때 레이블을 카운트 업하려면 어떻게해야합니까?

그럼 3 줄의 텍스트 파일을 선택했다고합시다.

밥 고양이 인간은

는 그것이 무엇을하면리스트 뷰에 항목을 추가합니다.

이제 모든 항목에 대해 레이블을 증가시키고 싶습니다 (0> 3에서 추가).

private void btnAddItems_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Names|*.txt"; 
      if(ofd.ShowDialog() == DialogResult.OK) 
      { 

       string[] recipients = File.ReadAllLines(ofd.FileName); 

       foreach(string name in recipients) 
       { 

        lvRecipient.Items.Add(name); 
        //increment the number of items in the list 
        foreach(int item in lvRecipient.Items) 
        { 
         int i = 0; 
         i++; 
         lbCount.Text = i.ToString(); 
        } 
       } 
      } 

는 그 시도하지만 난 그것을 실행하자마자 오류가있어, 내가 0> 3에서 내 라벨 증분을 어떻게 뒤에 실제 locig가 없기 때문에 작동을 wouldnt 꽤 확신했다 (또는 얼마나 많은 항목이 텍스트 파일에 있습니까?)

+1

'int i = 0;'선언을 해당 루프 외부로 이동하십시오. – LarsTech

답변

1

당신이 묻는 문제를 해결하려면, 그것은 다음과 같이 수행 할 수 있습니다

private void btnAddItems_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.Filter = "Names|*.txt"; 

    if(ofd.ShowDialog() == DialogResult.OK) 
    { 
     string[] recipients = File.ReadAllLines(ofd.FileName); 

     foreach(var name in recipients) 
     { 
      lvRecipient.Items.Add(name); 

      lbCount.Text = lvRecipient.Items.Count.ToString(); 
     } 
    } 
} 

그냥 모든 항목이 추가 된 후 카운트 라벨을 설정하는 것이 좋을 것이다, 오히려 그때마다 A를 설정 추가 작업이 매우 빨라야 사람이 변화하는 숫자를 감지하지 못할 수도 있기 때문에 새로운 것이 추가됩니다. 이것은과 같이 할 수있다 :

private void btnAddItems_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.Filter = "Names|*.txt"; 

    if(ofd.ShowDialog() == DialogResult.OK) 
    { 
     string[] recipients = File.ReadAllLines(ofd.FileName); 

     foreach(var name in recipients) 
     { 
      lvRecipient.Items.Add(name); 
     } 

     lbCount.Text = lvRecipient.Items.Count.ToString(); 
    } 
} 
+0

그거야! 하지만 실제로는 세지 않습니다. 0> 1000에서 점프합니다. – JonnyKhanas

+0

@JonnyKhanas 정확한 질문을 해결하기위한 답변을 업데이트했으며, 더 나은 해결책이라고 생각되는 내용도 포함 시켰습니다. – JacobVoller

+0

심지어 foreach 루프 안에 넣어도 여전히 점프합니다. – JonnyKhanas

0
using System; 
using System.IO; 
using System.Windows.Forms; 

namespace Jonny 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnAddItems_Click(object sender, EventArgs e) 
     { 
      { 
       OpenFileDialog ofd = new OpenFileDialog(); 
       ofd.Filter = "Names|*.txt"; 
       if (ofd.ShowDialog() == DialogResult.OK) 
       { 
        string[] recipients = File.ReadAllLines(ofd.FileName); 

        foreach (string name in recipients) 
        { 
         lvRecipient.Items.Add(name); 
         //increment the number of items in the list 
         foreach (var item in lvRecipient.Items) 
         { 
          int i = 0; 
          i++; 
          lbCount.Text = i.ToString(); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

이가하는 일을 - 나는 그것을 시도했다. "lvRecipient.Items"가 foreach 루프에서 정수로 처리 될 수 없기 때문에 "int"를 "var"로 변경하면됩니다.

관련 문제