2014-07-23 2 views
1

그런 어리석은 질문에 대해 용서해주십시오. 나는 많은 사람들이 이것을 쉽게 찾을 것이라고 확신합니다. 여기서 나는 이것을 거의 알아 내려고 독서의 하루 반 정도를 보냈습니다. 나는 FORM (Form1.cs를) 만든이다른 기능의 C# ListView

  1. : 여기

    문제입니다. 그 형식으로 나는 listview를 만들고 그것을 "ListView1"이라고 명명했다.
  2. Form1.cs 내에서 FileManager (this)라는 함수를 호출합니다.이 함수는 THIS 개체를 전달합니다.
  3. FileManager.cs에서 나는 listviewArray = originalForm.Controls.Find ("listView1", true)와 'listview'를 찾을 수있었습니다.
  4. listviewArray [0] < 할 때 목록을 추가 할 수 없습니다. 컬렉션이 비어 있기 때문에

FileManager.cs

FileManager(object sender) 
    { 
     if (sender != null) 
     { 
      originalForm = (Form)sender; 
     } 


    } 
    public void getFiles() 
    { 
     filePaths = Directory.GetFiles(hsocDir); 
     if(filePaths != null) 
     { 
      listviewArray= originalForm.Controls.Find("listView1", true); 
      if(listviewArray != null) 
      { 
       ListViewItem lvi = new ListViewItem("text"); 

       // My Array is listViewArray 
       // How to add things to Lvi to it. 

      } 
     } 


== Form1.cs 
public Form1() 
     { 
      InitializeComponent(`enter code here`); 
      mysql = new MySQLCheck(this); 
      fileManager = new FileManager(this); 
      fileManager.getFiles(); 
     } 

답변

0

당신은 컬렉션의 요소 0에 액세스 할 수 없습니다. 항목을 추가하려면, 사용

listViewArray.Items.Add(lvi); 

당신은 ListView 콜렉션 (그것의 제어)이 아니므로,이 작업을 수행하려면 대신 ListView 자체의 Items 수집을 수정해야합니다.

귀하의 목록보기에서 또한
+0

질문 1 : 나는 listView1을 찾기 위해 사용하는 방법이 물건을 추가하려고 올바른 경로를 반대인가? – FlyingV

+0

@ user3443386 그게 아주 합리적인 방법입니다. 심지어 직접 UI 조작을 버리고 MVVM 구현 (WPF 가능성이 있음)을 사용하는 것이 좋습니다. 즉, 프로젝트를 다시 작성할 준비가되지 않았으므로 괜찮습니다. – BradleyDotNET

0
listViewArray.Items.Add(lvi); 

,이 특성이 도움이 될 것입니다 설정 :

 // Set the view to show details. 
     listViewArray.View = View.Details; 
     // Select the item and subitems when selection is made. 
     listViewArray.FullRowSelect = true; 
     // Display grid lines. 
     listViewArray.GridLines = true;