質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

16594閲覧

UserControl内のButtonClickイベントがFormクラスで認識されない

mercredi

総合スコア26

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2017/04/09 19:21

###前提・実現したいこと
C#のUserControlでButtonClickイベントを作成し、Formでイベントをハンドルしたいのですが、Form内でエラーが出てしまいます。('The name UserControl1.ButtonClick does not exist in the current context')

###UserControl:

public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public event EventHandler ButtonClick; private void btnUpdate_Click(object sender, EventArgs e) { if (this.ButtonClick != null) this.ButtonClick(this, e); }

###Form:

UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick); //ここで'The name UserControl1.ButtonClick does not exist in the current context'というエラーメッセージが出てきます。 protected void UserControl_ButtonClick(object sender, EventArgs e) { //handle the event }

###試したこと
UserControl1クラスは念のため何度もBuildしました。
FormとUserControl1クラスは同じプロジェクト内にあります。
他にもUserControl内でテキストボックスをいくつか作っているのですが、それらのプロパティはすべて認識されております。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Form.cs:

1 public PatientNumber() 2 { 3 InitializeComponent(); 4 userControl11.updateButton.Click += button_Click; 5 } 6 7 protected void button_Click(object sender, EventArgs e) 8 { 9 //handle the event 10 }

UserControl.cs内でボタンのプロパティ(updateButton)を作り、フォームのコンストラクタの内ででイベントハンドラーを追加するとできました。

投稿2017/04/09 23:49

mercredi

総合スコア26

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問