方法一:傳值
最先想到的,Form2構造函數中接收一個string類型參數,即Form1中選中行的文本,將Form2的TextBox控件的Text設置為該string,即完成了Form1向Form2的傳值。當Form2的AcceptChange按鈕按下,需要修改Form1中ListBox中相應列的值,因此可以考慮同時將Form1中的ListBox控件當參數也傳入Form2,所有修改工作都在Form2中完成,根據這個思路,Form2代碼如下:
-
<pre name=
"code"
class=
"csharp">
public
partial
class
Form2 :
Form
-
{
-
private
string text;
-
private ListBox lb;
-
private
int index;
-
-
//構造函數接收三個參數:選中行文本,ListBox控件,選中行索引
-
-
public Form2(string text,ListBox lb,int index)
-
{
-
this.text = text;
-
this.lb = lb;
-
this.index = index;
-
InitializeComponent();
-
this.textBox1.Text = text;
-
}
-
private void btnChange_Click(object sender, EventArgs e)
-
{
-
string text =
this.textBox1.Text;
-
this.lb.Items.RemoveAt(index);
-
this.lb.Items.Insert(index, text);
-
this.Close();
-
}
-
}
Form1中new窗體2時這么寫:
-
public
partial
class
Form1 :
Form
-
{
-
int index =
0;
-
string text =
null;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
if (
this.listBox1.SelectedItem !=
null)
-
{
-
text =
this.listBox1.SelectedItem.ToString();
-
index =
this.listBox1.SelectedIndex;
-
//構造Form2同時傳遞參數
-
Form2 form2 =
new Form2(text, listBox1, index);
-
form2.ShowDialog();
-
}
-
}
-
}
OK,方法一的解決方法就是這樣,好處是直觀,需要什么就傳什么,缺點也是顯而易見的,如果窗體1中需要修改的是一百個控件,難道構造的時候還傳100個參數進去?況且如果其他窗體仍然需要彈Form2,那Form2就廢了,只能供窗體1使用,除非寫重載的構造函數,不利於代碼的復用,繼續看下一個方法。
方法二:繼承
這個方法我試了很多次,繼承的確可以做,但是麻煩不說,還不方便,因此個人認為如果為了互相操作數據而使用繼承,是不合適的,但既然是個方法,就扔出來看看,實際作用≈0。
Form2:
-
//聲明Form2繼承於Form1
-
public
partial
class
Form2 :
Form1
-
{
-
public
int index;
-
public ListBox lb;
-
public Form2(string text)
-
{
-
//將繼承過來的listBox設置為不可見
-
this.listBox1.Visible=
false;
-
InitializeComponent();
-
this.textBox1.Text = text;
-
}
-
private void btnChange_Click(object sender, EventArgs e)
-
{
-
string text =
this.textBox1.Text;
-
this.lb.Items.RemoveAt(index);
-
this.lb.Items.Insert(index, text);
-
this.Close();
-
}
-
}
Form1:
-
public
partial
class
Form1 :
Form
-
{
-
public
int index =
0;
-
public
string text =
null;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
if (
this.listBox1.SelectedItem !=
null)
-
{
-
text =
this.listBox1.SelectedItem.ToString();
-
index =
this.listBox1.SelectedIndex;
-
Form2 form2 =
new Form2(text);
-
//構造完Form2后,為Form2中各參數賦值
-
form2.lb =
this.listBox1;
-
form2.index = index;
-
form2.Show();
-
}
-
}
-
}
這里有幾點問題需要注意,Form2中各屬性需要哪種賦值方法?從Java過渡來的都知道,Java繼承中在子類中使用關鍵字super可以訪問基類中公有的方法及參數,而C#中super換成了base,那是不是意味着我們可以在Form2中這么為參數賦值呢?
this.lb=base.listBox1;
this.index=base.index;
OK,第二種寫法沒問題,可以保存index值,但是對ListBox控件,這么賦值就會出問題,通過測試我發現,base.listBox1指向的是子類繼承過來的listBox1對象,並不是基類自己的listBox1對象。因此我們猜測,那base.index值是不是也是指向子類的index呢?測試一下發現的確是這樣,因此this.index=base.index等於沒寫,去掉照樣可以用,因為index一樣被Form2繼承過來了,因此我們可以了解到,C#中的窗體繼承通過base.控件是無法操作基類控件的。
方法三:事件回調
既然C#有事件這個東西,為啥不用呢,而且事件在窗體通信方面,有着更為方便的作用,我們知道事件實際上就是狀態的捕獲,在最后我會舉一個捕獲狀態的例子,先看數據互相操作的例子。
Form2:
-
//定義一個需要string類型參數的委托
-
public delegate void MyDelegate(string text);
-
public
partial
class
Form2 :
Form1
-
{
-
//定義該委托的事件
-
public
event MyDelegate MyEvent;
-
public Form2(string text)
-
{
-
InitializeComponent();
-
this.textBox1.Text = text;
-
}
-
private void btnChange_Click(object sender, EventArgs e)
-
{
-
//觸發事件,並將修改后的文本回傳
-
MyEvent(
this.textBox1.Text);
-
this.Close();
-
}
-
}
Form1:
-
public
partial
class
Form1 :
Form
-
{
-
public
int index =
0;
-
public
string text =
null;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
if (
this.listBox1.SelectedItem !=
null)
-
{
-
text =
this.listBox1.SelectedItem.ToString();
-
index =
this.listBox1.SelectedIndex;
-
Form2 form2 =
new Form2(text);
-
//注冊form2_MyEvent方法的MyEvent事件
-
form2.MyEvent +=
new MyDelegate(form2_MyEvent);
-
form2.Show();
-
}
-
}
-
//處理
-
void form2_MyEvent(string text)
-
{
-
this.listBox1.Items.RemoveAt(index);
-
this.listBox1.Items.Insert(index, text);
-
}
-
}
可以看出,使用事件做是很方便的,並且不需要傳遞那么多參數,不需要有繼承關系,且提高了代碼重用,因此在一般的需求下,建議這么使用。最后說一下事件的狀態捕獲:
當點Form1中的ShowForm2按鈕時,彈出Form2,且在TextBox里寫入"Show Form2",當點Form2中Form2Close按鈕時,關閉Form2,且將Form1的TextBox文本改為"Close Form2",這就是一個狀態的捕獲過程,常用在應用程序的狀態欄。
Form1:
-
public
partial
class
Form1 :
Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Form2 form2 =
new Form2();
-
this.textBox1.Text =
"Show Form2";
-
form2.MyEvent +=
new EventHandler(form2_MyEvent);
-
form2.Show();
-
}
-
void form2_MyEvent(object sender, EventArgs e)
-
{
-
this.textBox1.Text =
"Close Form2";
-
}
-
}
Form2:
-
public
partial
class
Form2 :
Form
-
{
-
public
event EventHandler MyEvent;
-
public Form2()
-
{
-
InitializeComponent();
-
}
-
private void button1_Click(object sender, EventArgs e)
-
{
-
MyEvent(sender,e);
-
this.Close();
-
}
-
}
From : http://www.cnblogs.com/Asa-Zhu/archive/2012/12/06/2805074.html
轉自https://blog.csdn.net/u011981242/article/details/48785639
