概要
cscの作法、調べてみた。
練習問題、やってみた。
練習問題
サブフォームからメインフォームへデータを送れ。
写真
サンプルコード
using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace App
{
public partial class formSub: Form {
TextBox bo1;
public formMain fm;
public formSub() {
Text = "formSub";
ClientSize = new Size(300, 200);
bo1 = new TextBox();
bo1.Location = new Point(50, 20);
bo1.Text = "test desu";
bo1.Size = new Size(100, 20);
Controls.AddRange(new Control[] {
bo1
});
Button btn1 = new Button();
btn1.Location = new Point(50, 100);
btn1.Text = "test";
btn1.Click += btn1_Click;
Controls.AddRange(new Control[] {
btn1
});
}
void btn1_Click(object sender, System.EventArgs e) {
if (fm != null)
{
fm.ReceiveData = bo1.Text;
}
}
}
public partial class formMain: Form {
public string ReceiveData {
set {
receiveData = value;
bo1.Text = receiveData;
}
get {
return receiveData;
}
}
TextBox bo1;
private string receiveData = "";
public formMain() {
Text = "formMain";
ClientSize = new Size(300, 100);
bo1 = new TextBox();
bo1.Location = new Point(50, 20);
bo1.Size = new Size(100, 20);
Controls.AddRange(new Control[] {
bo1
});
formSub sub = new formSub();
sub.fm = this;
sub.Show();
}
[STAThread]
public static void Main() {
Application.Run(new formMain());
}
}
}
以上。