dポイントプレゼントキャンペーン実施中!

C# コントロール取得

選択しているコントロールを取得する方法とかありますでしょうか?
VC#2008でアプリケーションを作っています。

例えばテキストボックスのLeave関数などで

MessageBox.Show(this.textbox1.Text) みたいにしたいのですが

テキストボックスが20個ぐらい必要だとLeave関数を20個作らなくてはいけないので
上記の方法がわかれば1つで済むので是非知りたいです。
自分でも調べてActiveControlを使用してみたのですが反応しません。

MessageBox.Show(this.ActiveControl.Text); としましたが・・
教えてください

A 回答 (1件)

using System;





namespace Q6036418

{

class MainClass:System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox textbox1;

private System.Windows.Forms.TextBox textbox2;



public MainClass(){

this.Width = 800;

this.Height = 600;



textbox1 = new System.Windows.Forms.TextBox();

textbox1.Left = 0;

textbox1.Top = 0;

textbox1.Width = 100;



textbox2 = new System.Windows.Forms.TextBox();

textbox2.Left = 400;

textbox2.Top = 0;

textbox2.Width = 100;

/* 分かりやすいようわざとあとに持ってきた。 */

textbox1.Leave += textboxes_leave;

textbox2.Leave += textboxes_leave;



this.Controls.Add(textbox1);

this.Controls.Add(textbox2);



}



private void textboxes_leave(object sender, System.EventArgs e){

System.Windows.Forms.MessageBox.Show(((System.Windows.Forms.TextBox)sender).Text);

}



public static void Main(String[] args){

MainClass form1 = new MainClass();

form1.ShowDialog();

}

}

}
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!