|
■No102312 (Hongliang さん) に返信
■No102313 (魔界の仮面弁士 さん) に返信
解答ありがとうございます。
お二方のAddHandlerにします。
ありがとうございました。
----Form1.cs(dll)
using System.ComponentModel;
using System.Windows.Forms;
namespace ClassLibrary1
{
[DefaultEvent(nameof(ButtonClick))]
//[DefaultEvent(nameof(CheckboxCheckedChanged))] // CS50579
//[DefaultEvent(nameof(ButtonClick), nameof(CheckboxCheckedChanged))] // CS1729
public partial class Form1 : Form
{
// Form1.Designer.cs
// private to protected
protected System.Windows.Forms.Button button1;
public event EventHandler ButtonClick;
// Checkboxを追加したら? => CS50579/CS1729でダメ(Defaultじゃなくなるか...)
//public event EventHandler CheckboxCheckedChanged;
public Form1()
{
InitializeComponent();
// 案2=魔界の仮面弁士さん
//foreach (var b in new[] { button1, button2, button3 })
//{
// b.Click += delegate { ButtonClick?.Invoke(b, EventArgs.Empty); };
//};
//foreach( var c in new[] { checkBox1, checkBox2})
//{
// c.Click += delegate { CheckboxCheckedChanged?.Invoke(c, EventArgs.Empty); };
//}
// ↓Button, CheckBoxをDefaultEventデリゲートに...
//foreach (var c in new Control[] { button1, button2, button3, checkBox1, checkBox2 })
//{
// c.Click += delegate { ButtonClick?.Invoke(c, EventArgs.Empty); };
//};
}
private void button1_Click(object sender, EventArgs e)
{
this.Text = ((Control)sender).Name;
}
private void button2_Click(object sender, EventArgs e)
{
this.Text = ((Control)sender).Name;
}
private void button3_Click(object sender, EventArgs e)
{
this.Text = ((Control)sender).Name;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
var c = (CheckBox)sender;
this.Text = $"{c.Name}:{c.Checked}";
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
var c = (CheckBox)sender;
this.Text = $"{c.Name}:{c.Checked}";
}
}
}
----Form2.vb(exe)
Imports ClassLibrary1
Public Class Form2
Inherits Form1
' 案1=Hongliangさん/魔界の仮面弁士さん
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 必要なイベントだけAddHandler
AddHandler button1.Click, AddressOf button1_Click
AddHandler button2.Click, AddressOf button2_Click
'AddHandler button3.Click, AddressOf button3_Click
AddHandler checkBox1.CheckedChanged, AddressOf checkBox1_CheckedChanged
AddHandler checkBox2.CheckedChanged, AddressOf checkBox2_CheckedChanged
End Sub
' 案2
Private Sub button_Click(sender As Object, e As EventArgs) Handles MyBase.ButtonClick
'Select Case sender.Name
' Case button1.Name : button1_Click(sender, e)
' Case button2.Name : button2_Click(sender, e)
' Case button3.Name : button3_Click(sender, e)
' Case checkBox1.Name : checkBox1_CheckedChanged(sender, e)
' Case checkBox2.Name : checkBox2_CheckedChanged(sender, e)
'End Select
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, Control).Name)
End Sub
Private Sub button2_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, Control).Name)
End Sub
Private Sub button3_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, Control).Name)
End Sub
Private Sub checkBox1_CheckedChanged(sender As Object, e As EventArgs)
Dim c As CheckBox = DirectCast(sender, CheckBox)
MessageBox.Show($"{c.Name}:{c.Checked}")
End Sub
Private Sub checkBox2_CheckedChanged(sender As Object, e As EventArgs)
Dim c As CheckBox = DirectCast(sender, CheckBox)
MessageBox.Show($"{c.Name}:{c.Checked}")
End Sub
End Class
|