さまざまな状況で「再利用可能」にしたいフォームがあります。主に情報を表示および印刷します。フォームには 2 つのボタンとリストボックスがあります
ボタンが押されたときに何をするかをフォームに伝えるオブジェクトをフォームに渡すことができるようにしたい(たとえば、メッセージボックスを表示したり、リストボックスの内容を印刷したり、フォームを閉じたりする)
ボタンにどのイベントを割り当てるかを if ステートメントを使用して判断しています...これを行うためのより良い方法はありますか?
理想的には、「アクション」と呼ばれる列挙型を使用して、代わりに最初の呼び出しコードからイベントを設定したいと思います
==========calling code=================
var information = new Information();
information.Action = Action.Print;
var frmInformation = new frmInformation(information);
frmInformation.Show(this);
====================information class======================
public class Information
{
public delegate void OkButtonDelegate();
public IList<string> information{ get; set; }
public Information()
{
information = new BindingList<string>();
}
===============information form======================
public partial class frmInformation : Form
{
private readonly Information _information;
public Information.OkButtonDelegate _delegate;
public frmInformation(Information information)
{
_information = information;
InitializeComponent();
SetupForm();
}
private void SetupForm()
{
if (_information.Action== Action.Print)
_delegate = new Information.OkButtonDelegate(Print);
else if (_information.Action == Action.Close)
_delegate = new Information.OkButtonDelegate(Close);
}
private void ShowMessageBox()
{
MessageBox.Show("lalalalalala");
}
public static void Print()
{
//take the contente out of listbox and send it to the printer
}
private void btnSend_Click(object sender, EventArgs e)
{
_delegate();
}