[問題] 請問如何在不同form裡控制變數???
抱歉
小弟弟算是程式新手
我寫2個表單Form1, Form2
打算在form2設定一個按鍵讓form1裡的一個布林變數打開
我的做法是這樣,請各位高手幫我看看是不是那裡錯了
先在Form1裡宣告一個公用變數開關
按Form1裡的button 會跳出form2:
public partial class Form1 : Form
{
public bool a = false;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Owner = this;
}
}
而form2裡也設定一個button, 按下去讓form1的a打開:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.a=true;
this.close();
}
照裡來說, 應該回到form1裡, a會是true才對啊???
為啥我在執行時會出現錯誤呢?
請各位高手幫幫忙...謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 119.14.168.8
推
04/28 23:02, , 1F
04/28 23:02, 1F
→
04/28 23:02, , 2F
04/28 23:02, 2F
推
04/28 23:04, , 3F
04/28 23:04, 3F
→
04/28 23:05, , 4F
04/28 23:05, 4F
→
04/28 23:07, , 5F
04/28 23:07, 5F
→
04/28 23:07, , 6F
04/28 23:07, 6F
→
04/28 23:12, , 7F
04/28 23:12, 7F
→
04/29 00:34, , 8F
04/29 00:34, 8F
→
04/29 00:38, , 9F
04/29 00:38, 9F
Compile沒問題, 但我按下form2 的button時就有錯誤了
錯誤訊息是An unhandled exception of type 'System.NullReferenceException' occurred in ADW_Map.exe
Additional information: Object reference not set to an instance of an object.
※ 編輯: chrishappy11 來自: 119.14.168.8 (04/29 08:03)
推
04/29 09:01, , 10F
04/29 09:01, 10F
→
04/29 10:02, , 11F
04/29 10:02, 11F
→
04/29 10:07, , 12F
04/29 10:07, 12F
推
04/29 17:34, , 13F
04/29 17:34, 13F
問題已解決...感謝
我自己key錯行...哈哈
※ 編輯: chrishappy11 來自: 119.14.168.8 (04/29 19:55)