2013-07-17 67 views
0

我想刷新位於form1的listview1。我創建的代碼,引發公共無效刷新或使其listview1.visible = false;沒有工作,除了messagebox.show("test");我如何控制位於form1中的listview從form2刷新它

如何使它工作?

public void RefeshListView() 
    { 

     this.listView1.BeginUpdate(); 

     MessageBox.Show("s");//this shows! only:\ !?!?!? 
     listView1.Visible = false; 
     listView1.Height = 222; 
     listView1.EndUpdate(); 
     listView2.Clear(); 

    } 
+0

別的什麼喲你展示? –

+0

以上代碼位於哪個表單? –

回答

0

我有點不清楚你正在嘗試做的,但是從你的標題聽起來像是你想從窗體2 Form1的影響ListView。我假設Form1是從Form1創建的。在你的情況下,我有兩種方法可以這樣做,第一種方法是創建自定義構造函數並將表單實例傳遞給它,或者在顯示錶單時分配所有權。第二個是在Form2上創建一個自定義事件並訂閱Form1中的。

第一種方法:

在Form1中,當你表現出窗體2使用窗體2 frm2.Show(this);
當你要撥打的刷新方法使用((Form1)Parent).RefreshListView();

或者創建自定義構造函數的Form2

Form1

public partial class Form1 : Form 
{ 
    Form2 frm2; 
    public Form1() 
    { 
     InitializeComponent(); 
     frm2 = new Form2(this); 
     frm2.Show(); 
    } 

    void frm2_RefreshList(object sender, EventArgs e) 
    { 
     RefreshListView(); 
    } 

    public void RefreshListView() 
    { 

     this.listView1.BeginUpdate(); 

     MessageBox.Show("s");//this shows! only:\ !?!?!? 
     listView1.Visible = false; 
     listView1.Height = 222; 
     listView1.EndUpdate(); 
     listView1.Clear(); 

    } 
} 

窗體2

public partial class Form2 : Form 
{ 
    Form1 frm1; 
    public Form2() 
    { 
     InitializeComponent(); 
    } 
    public Form2(Form frm) 
    { 
     InitializeComponent(); 
     frm1 = (Form1)frm; 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     frm1.RefreshListView(); 
    } 
} 

第二種方法:

Form1中

public partial class Form1 : Form 
{ 
    Form2 frm2; 
    public Form1() 
    { 
     InitializeComponent(); 
     frm2 = new Form2(); 
     frm2.RefreshList += new EventHandler(frm2_RefreshList); 
     frm2.Show(); 
    } 

    void frm2_RefreshList(object sender, EventArgs e) 
    { 
     RefreshListView(); 
    } 

    public void RefreshListView() 
    { 

     this.listView1.BeginUpdate(); 

     MessageBox.Show("s");//this shows! only:\ !?!?!? 
     listView1.Visible = false; 
     listView1.Height = 222; 
     listView1.EndUpdate(); 
     listView1.Clear(); 

    } 
} 

窗體2

public partial class Form2 : Form 
{ 
    public event EventHandler RefreshList; 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     RefreshList(this, EventArgs.Empty); 
    } 
} 
+0

它的工作非常感謝:) – Karimchedid

+0

@Karimchedid不客氣 –

0

也許需要刷新。

public void RefeshListView() 
{ 

    this.listView1.BeginUpdate(); 

    MessageBox.Show("s");//this shows! only:\ !?!?!? 
    listView1.Visible = false; 
    listView1.Height = 222; 
    listView1.EndUpdate(); 
    listView2.Clear(); 
    listView2.refresh(); 
}