星期六, 7月 11, 2015

[C#] Form 傳值

論壇問題:要在 Form 之間傳值,拿來練習

Project 內容
[C#] Form 傳值-1

frmMain layout
[C#] Form 傳值-2

frmCustSearch layout
[C#] Form 傳值-3


Customer Class
namespace WindowsFormsApplication1
{
    public class Customer
    {
        public int CustID { get; set; }
        public string CustName { get; set; }
        public string Address { get; set; }
    }
}
frmMain C# Code
namespace WindowsFormsApplication1
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnCustSearch_Click(object sender, EventArgs e)
        {

            ClearControlValue();

            frmCustSearch CustSearch = new frmCustSearch();
            DialogResult result = CustSearch.ShowDialog();
            if (result != DialogResult.OK) return;

            txtCustID.Text = CustSearch.retValue.CustID.ToString();
            txtCustName.Text = CustSearch.retValue.CustName.ToString();
            txtAddress.Text = CustSearch.retValue.Address.ToString(); ;
        }

        void ClearControlValue()
        {
            foreach (Control ctl in this.Controls.OfType<Textbox>())
            {
                TextBox txt = ctl as TextBox;
                if (txt == null) continue;
                txt.Text = string.Empty;
            }
        }
    }
}
frmCustSearch C# Code
namespace WindowsFormsApplication1
{
    public partial class frmCustSearch : Form
    {
        public frmCustSearch()
        {
            InitializeComponent();
        }

        // 紀錄使用者點選的是哪一筆資料
        public Customer retValue;

        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 result = DialogResult.OK;
            if (this.retValue == null || btn.Name.Equals("btnCancel"))
            result = DialogResult.Cancel;
            this.DialogResult = result;

            this.Close();
        }

        private void dgvCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.retValue = new Customer()
                {
                    CustID = (int)dgvCustomer.CurrentRow.Cells["colCustID"].Value,
                    CustName = (string)dgvCustomer.CurrentRow.Cells["colCustName"].Value,
                    Address = (string)dgvCustomer.CurrentRow.Cells["colAddress"].Value
                };
        }
    }
}
結果
[C#] Form 傳值-4

沒有留言:

張貼留言