お世話になります。
C#プログラミング初心者です
現在二つのフォームがありまして、
親フォームから
Form2 f = new Form2();
f.showDialog();
という具合に子フォームを表示させています。
その子フォームのLoadイベント内に
C#
1private void Form1_Load(object sender, EventArgs e) 2 { 3 4 this.button1.Text = "fuga"; 5 this.textBox1.Text = "hoge"; 6 7 }
とすると、fugaのほうは特に何の問題もないのですが、hogeの方で
「オブジェクト参照がオブジェクトインスタンスに設定されていません」
と例外が生じます。
同じソースを親フォームの方で書いたときは問題なく動作します。
最初ふと、LoadイベントはtextBoxなどがインスタンス化される前に呼ばれるのか?と思ったのですが、buttonの方はちゃんと動作するのが不思議です
何が間違っているのでしょうか、教えてください。
よろしくお願いいたします。
----以下追記です
InitializeComponent()内はVSが自動的に吐いてくれるソースですので、私は何もいじってないです。
一部名前やリテラルを隠させていただきましたが、それ以外は変えてないです。
親フォーム
C#
1namespace AR 2{ 3 public partial class Main : Form 4 { 5 public Main() 6 { 7 InitializeComponent(); 8 } 9 10 private void Main_Load(object sender, EventArgs e) 11 { 12 13 this.textBox1.Text = "changed"; 14 15 } 16 17 private void button2_Click(object sender, EventArgs e) 18 { 19 GYM f = new GYM(); 20 f.ShowDialog(); 21 Console.Write("フォームが表示されました。"); 22 23 f.Dispose(); 24 } 25 } 26}
子フォーム
C#
1namespace AR 2{ 3 public partial class GYM : Form 4 { 5 public String id; 6 public String ID 7 { 8 set { id = value; } 9 get { return id; } 10 } 11 12 private HtmlElementCollection html; 13 14 15 public GYM() 16 { 17 InitializeComponent(); 18 webBrowser1.Navigate("url"); 19 20 21 22 } 23 24 private void Form1_Load(object sender, EventArgs e) 25 { 26 27 this.button1.Text = "changed"; 28 this.textBox1.Text = "this"; 29 30 } 31 32 private void textBox1_TextChanged(object sender, EventArgs e) 33 { 34 HtmlElementCollection html = this.webBrowser1.Document.All; 35 HtmlElementCollection forms = html.GetElementsByName("hoge"); 36 forms[0].InnerText = textBox1.Text; 37 } 38 39 private void textBox2_TextChanged(object sender, EventArgs e) 40 { 41 HtmlElementCollection html = this.webBrowser1.Document.All; 42 HtmlElementCollection forms = html.GetElementsByName("fuga"); 43 forms[0].InnerText = textBox2.Text; 44 } 45 46 private void textBox3_TextChanged(object sender, EventArgs e) 47 { 48 HtmlElement pwd = webBrowser1.Document.GetElementById("hogefuga"); 49 pwd.InnerText = textBox3.Text; 50 51 52 } 53 } 54}
回答4件
あなたの回答
tips
プレビュー