我有點不清楚你正在嘗試做的,但是從你的標題聽起來像是你想從窗體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);
}
}
別的什麼喲你展示? –
以上代碼位於哪個表單? –