星期二, 8月 29, 2023

[C#] Form 傳值 - 事件回傳

在 Line 社群上看見討論,使用事件 (Event) 來收回傳值,以筆記 - [C#] Form 傳值 為範本,再進行延伸

frmMain C# Code
namespace WindowsFormsApplication1
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void btnCustSearch_Click(object sender, EventArgs e)
        {
            ClearControlValue();
            frmCustSearch CustSearch = new frmCustSearch();
            // 註冊 PassRetValue Event            
            CustSearch.PassRetValue += CustSearch_PassRetValue;
            CustSearch.ShowDialog();
        }

        private void CustSearch_PassRetValue(object sender, RetValueEventArgs e)
        {
            var customer = e.Customer;
            txtCustID.Text = customer.CustID.ToString();
            txtCustName.Text = customer.CustName;
            txtAddress.Text = customer.Address;
        }

        void ClearControlValue()
        {
            foreach (Control ctl in Controls.OfType<TextBox>())
            {
                if ((ctl is TextBox txt) == false)
                    continue;

                txt.Text = string.Empty;
            }
        }
    }
}
frmCustSearch C# Code
namespace WindowsFormsApplication1
{
    // 宣告 RetValueEventArgs
    public class RetValueEventArgs : EventArgs
    {
        public Customer Customer { get; private set; }

        public RetValueEventArgs(Customer customer)
        {
            Customer = customer;
        }
    }

    // 宣告 RetValueEventHandler delegate
    public delegate void RetValueEventHandler(object sender, RetValueEventArgs e);

    public partial class frmCustSearch : Form
    {
        public frmCustSearch()
        {
            InitializeComponent();
        }

        // 宣告 PassRetValue event 並透過它回傳
        public event RetValueEventHandler PassRetValue;

        private void frmCustSearch_Load(object sender, EventArgs e)
        {
            dgvCustomer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            btnOK.Click += btn_Click;
            btnCancel.Click += btn_Click;
            dgvCustomer.DataSource = DataGridViewSource();
            dgvCustomer.ClearSelection();
        }

        List<Customer> DataGridViewSource()
        {
            return
                new List<Customer>()
                    {
                        new Customer() { CustID = 1, CustName = "客戶1", Address = "台北" },
                        new Customer() { CustID = 2, CustName = "客戶2", Address = "台中" },
                        new Customer() { CustID = 3, CustName = "客戶3", Address = "高雄" }
                    };
        }

        void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn == null)
                return;

            DialogResult = DialogResult.OK;
            if (btn.Name == btnCancel.Name)
            {
                DialogResult = DialogResult.Cancel;
                return;
            }

            var customer = new Customer()
            {
                CustID = (int)dgvCustomer.CurrentRow.Cells[ColCustID.Name].Value,
                CustName = (string)dgvCustomer.CurrentRow.Cells[ColCustName.Name].Value,
                Address = (string)dgvCustomer.CurrentRow.Cells[ColAddress.Name].Value
            };

            var arg = new RetValueEventArgs(customer);
            // 觸發 PassRetValue event            
            PassRetValue?.Invoke(this, arg);

            Close();
        }
    }
}

沒有留言:

張貼留言