- PR -

他のFormを参照

1
投稿者投稿内容
hideaki200x
会議室デビュー日: 2003/07/30
投稿数: 8
投稿日時: 2003-07-30 15:53
こんにちは。
VisualC#.NETでの質問です。
たとえば親Form(Form1)と子フォーム(Form2)があるとします。
で、Form1にはlabel1が、Form2にはtextBox1とbutton1があるとします。
そのForm2のtextBox1に"a"と入力してbutton1を押すと、Form1のlabel1にそのtextBox1に入力した"a"が表示されるようにしたいのですが、方法が分かりません。
教えてください。お願い致します。
ocean
ベテラン
会議室デビュー日: 2003/07/06
投稿数: 65
投稿日時: 2003-07-30 17:33
下のリンクが参考になります。
C#でつくるWindowsアプリ
WisdomSoft
Microsoft(いまいちわかりにくい)

コード:


using System;
using System.Drawing;
using System.Windows.Forms;

class MeApp
{
public static void Main(String[] args)
{
Application.Run(new MeForm1());
}
}

class MeForm1 : Form
{
private TextBox FTextBox = new TextBox();
private Button FButton = new Button();

public MeForm1()
{
FTextBox.Location = new Point(0, 0);
FTextBox.Width = 140;
FTextBox.Parent = this;

FButton.Text = "Open";
FButton.Location = new Point(150, 0);
FButton.Parent = this;
FButton.Click += new EventHandler(Button_OnClick);
}

private void Button_OnClick(object sender, EventArgs e)
{
using (MeForm2 form = new MeForm2())
{
if (DialogResult.OK == form.ShowDialog())
{
FTextBox.Text = form.InputedText;
}
}
}
}

class MeForm2 : Form
{
private TextBox FTextBox = new TextBox();
private Button FButton = new Button();

public string InputedText
{
get
{
return FTextBox.Text;
}
}

public MeForm2()
{
FTextBox.Location = new Point(0, 0);
FTextBox.Width = 140;
FTextBox.Parent = this;

FButton.Text = "OK";
FButton.Location = new Point(150, 0);
FButton.DialogResult = DialogResult.OK;
FButton.Parent = this;
}
}




[ メッセージ編集済み 編集者: ocean 編集日時 2003-07-30 17:35 ]
なな
ぬし
会議室デビュー日: 2003/06/22
投稿数: 659
お住まい・勤務地: 愛知県
投稿日時: 2003-07-30 18:01
親フォームから、子フォームを表示する際に、thisを渡すと、

dlg.ShowDialog(this);

子フォームのOwnerをキャストすることで、親フォームにアクセスできます。

(FormMain)Owner
hideaki200x
会議室デビュー日: 2003/07/30
投稿数: 8
投稿日時: 2003-07-31 07:58
できました!!
ありがとうございました。
1

スキルアップ/キャリアアップ(JOB@IT)