2013-04-11 4 views
1

나는 이것을하는 방법에 대해 많은 질문을 보았지만 그 중 아무 것도 작동하지 않는 것으로 보인다. 내 기본보기에서 ListView 있어요 및 다른 폼에서 데이터를 추가하는 채우는 해요. 다른 양식에서는 텍스트 필드의 일부 문자열을 전달하고 싶습니다.다른 클래스에서 ListView에 항목 추가

namespace Booker 
{ 
    public partial class newBookingForm : Form 
{ 
    public newBookingForm() 
    { 
     InitializeComponent(); 
    } 

    private void NewBookingForm_Load(object sender, EventArgs e) 
    { 
     roomField.Focus(); 
    } 

    private void newBookingButton_Click(object sender, EventArgs e) 
    { 
     // Add to ListView 
     // Add to Parse 
     string room = this.roomField.Text; 
     string date = this.datePicker.Value.ToShortDateString(); 
     string time = this.timeField.Text; 
     string person = "<username>"; // Get current Parse user 

     Booker booker = new Booker(); 
     string[] array = new string[4] { room, date, time, person }; 
     booker.UpdatingListView(array); 

     this.Hide(); 
    } 
} 

}

// ListView에 양식 :

namespace Booker 
{ 
public partial class Booker : Form 
{ 
    delegate void MyDelegate(string[] array); 

    public Booker() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnFormClosing(FormClosingEventArgs e) 
    { 
     base.OnFormClosing(e); 
     Environment.Exit(0); 
    } 

    private void Booker_Load(object sender, EventArgs e) 
    { 
     listView.View = View.Details; 
     listView.AllowColumnReorder = false; 
    } 

    /* Methods for the Booker form */ 

    private void newBookingButton_Click(object sender, EventArgs e) 
    { 
     newBookingForm nbf = new newBookingForm(); 
     nbf.Show(); 
    } 

    public void UpdatingListView(string[] array) 
    { 
     if (this.listView.InvokeRequired) 
      this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array }); 
     else 
     { 
      ListViewItem lvi = new ListViewItem(array[0]); 
      lvi.SubItems.Add(array[1]); 
      this.listView.Items.Add(lvi); 
     } 
    } 

    private void exitButton_Click(object sender, EventArgs e) 
    { 
     // Error - no action being sent 
    } 

    private void helpButton_Click(object sender, EventArgs e) 
    { 
     // Display help panel 
    } 

    private void contactButton_Click(object sender, EventArgs e) 
    { 
     // Open panel/email 
    } 

    private void newBooking_Click(object sender, EventArgs e) 
    { 
     newBookingButton_Click(sender, e); 

     //string[] row = { "Meeting room", "April 11, 2013", "12:00PM-1:00PM", "Ryan" }; 
     //var listViewItem = new ListViewItem(row); 
     //listView.Items.Add(listViewItem); 
    } 
+0

질문을 잊어 버렸습니다. – tnw

+0

또한 배열에'UpdatingListView'를 전달한 다음'array [1]'을'lvi'에 넣는 것처럼 보입니다. 배열의 나머지 부분에 대해서는 아무 것도하지 않고,'listView'에 추가하십시오. 그게 문제 야? 배열의 모든 것이'listView'에 나타나지 않습니다. – tnw

+0

문제는 ListView에 아무 것도 입력되어 있지 않다는 것입니다. –

답변

0

다른 클래스의 항목을 추가하려면 다른 클래스의 메소드에 액세스하여 Program에서 추가하십시오. 이처럼 :

public void UpdatingListView(string[] array) 
    { 

     if (this.listView.InvokeRequired) 
     { 
      this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array }); 

     } else { 
      ListViewItem lvi = new ListViewItem(array[0]); 
      lvi.SubItems.Add(array[1]); 
      lvi.SubItems.Add(array[2]); 
      this.listView.Items.Add(lvi); 
     } 
    } 

문자열 배열이 ListViewSubItems로 추가됩니다 : 우리가 추가하는

string name = "Ryan"; 
string site = "imryan.net"; 

string[] array = new string[2] = { name, site }; 
Program.classWithListView.UpdatingListView(array); 

// 클래스 :

// 클래스 우리는에서 추가하는 .

1

UpdatingListView이 시도 :

ListViewItem lvi = new ListViewItem(array[0],0); 
lvi.SubItems.Add(array[1]); 
lvi.SubItems.Add(array[2]); 
lvi.SubItems.Add(array[3]); 
this.listView.Items.Add(lvi); 

// 새로운 예약 양식을 추가 : 여기에 내 코드입니다

이것은 creat됩니다. 귀하의 ListView의 첫 번째 위치에있는 열을 해당 열의 행으로 배열의 4 개 항목과 비교하십시오.

+0

아니, 작동하지 않습니다. : –

+0

@RyanCohen 코드를 단계적으로 수행하여 배열에 실제로 아무것도 포함되어 있지 않은지 확인 했습니까? – tnw

+0

예, MessageBoxes를 사용하여 배열의 데이터를 UpdatingListView 메서드에서 표시합니다. –