前からやれるかなとやっていなかったが、今回どうしてもということでここを参考にやってみた。
やはり追い詰められないとやらないな。書を捨てて街に出よ!
Form1 f1; <– ここポイント
(Label)でキャストもポイント
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this); // 自フォームへの参照を渡す
f2.Show(); // サブ・フォームを表示
}
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 f1;
// コンストラクタ
public Form2(Form1 f)
{
f1 = f; // メイン・フォームへの参照を保存
InitializeComponent();
}
// ボタンのClickイベント・ハンドラ
private void button1_Click(object sender, EventArgs e)
{
Label f1_label1 = (Label)f1.Controls["label1"];
f1_label1.Text = this.textBox1.Text;
// メイン・フォームのラベルのModifiersプロパティを
// internalにした場合は次の1行でOK
//
// f1.label1.Text = this.textBox1.Text;
}
}
}
No tags for this post.

