別フォームに値を渡す方法について調べていたのですが、
難しくてよく分からなかったので動けばいいや的な人向けです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using System; using System.Windows.Forms; namespace WindowsFormsApp4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); string SampleText = "Form2に出す用のテキスト"; form2.text = SampleText; } } } /////////////////////////////////// /// /// /// ここからForm2 /// /// /// /////////////////////////////////// using System; using System.Windows.Forms; namespace WindowsFormsApp4 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public string text { get; set; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(text); } } } |
まずForm1から
■15~16行目
Form2を表示させるだけ
■18~19行目
Form2の「text」に適当な文字列を入れます。
つぎにForm2
■44行目
「public string //// { get; set; }」の////部分はForm1で記述した
「Form2.////」←ここ
コメントを残す