C#編寫第三方控制項,實現表單控制項的一鍵取值

來源:互聯網
上載者:User

標籤:控制項取值   c#   第三方控制項   textbox   

    上篇部落格中講到了利用編寫第三方控制項的方法,實現給表單控制項的Text屬性賦值,比如說:TextBox,Combox等。有賦值,當然也有取值操作。從表單的控制項中取值,然後存入變數或者實體屬性當中,傳入到資料訪問層進行添加,更新等操作也是我們經常使用的。如何?一鍵取值呢?

使用的方法在上篇部落格中已經做了詳細說明,這裡就不再累述了。

代碼寫在這裡:

 

該方法的作用是,遍曆傳入表單中的控制項,並且擷取其值賦給實體類的相應屬性。

        /// <summary>        /// 從表單控制項取值,填充到學生實體類中        /// </summary>        /// <param name="enStudent">學生實體</param>        public static void FillStuEntityByForm(StuEntity enStudent, Form thisfrm)        {            //遍曆表單上的控制項            foreach (Control ctrl in thisfrm.Controls)            {                //控制項是否繼承了介面                if (ctrl is Interface1)                {                    //是否屬於文字框                    if (ctrl is TextBoxEx)                    {                        //給實體屬性賦值                        SetStuValue(enStudent, ((TextBoxEx)ctrl).DataField, ctrl.Text);                    }//如果屬於下拉框                    else if (ctrl is ComboxEx)                    {                        SetStuValue(enStudent, ((ComboxEx)ctrl).DataField, ((ComboxEx)ctrl).Text);                    }                }            }        }


利用反射給實體每個屬性賦值:

        /// <summary>        /// 給實體相應的屬性賦值        /// </summary>        /// <param name="enStudent">學生實體類</param>        /// <param name="DataFiled">匹配字串</param>        /// <param name="Value">表單控制項的值</param>        public static void SetStuValue(StuEntity enStudent, string DataFiled, string Value)        {            PropertyInfo field = enStudent.GetType().GetProperty(DataFiled);            field.SetValue(enStudent, Value, null);        }


 

將返回實體的屬性取出,拼成一個字串。

        /// <summary>        /// 將實體的屬性群組合成字串返回        /// </summary>        /// <param name="enStudent"></param>        /// <returns></returns>        public static string GetStr(StuEntity enStudent)        {            PropertyInfo[] fields = enStudent.GetType().GetProperties();            StringBuilder sb = new StringBuilder();            foreach (PropertyInfo pi in fields)            {                sb.Append(pi.Name).Append(":").Append(pi.GetValue(enStudent, null)).Append("\r\n");            }            return sb.ToString();        }

 

用戶端調用代碼:

        private void btnGetValue_Click(object sender, EventArgs e)        {            StuEntity enStudent = new StuEntity();            //調用填充實體屬性的函數            FrmHelper.FillStuEntityByForm(enStudent, this);            //將實體的屬性顯示出來            string str = FrmHelper.GetStr(enStudent);            MessageBox.Show(str);        }


顯示效果:
            

 

      實現過程就是這樣了,有什麼不當的地方歡迎指出!

 

 

 

 

 

                                           oノo════════════════════════════╲

                                            │ヽ.編程學習中。歡迎交流。                                 │

                                            │   http://blog.csdn.net/u010028869                  .ヽ│

                                             ╲═════════════════════════════ヾ

 

 

C#編寫第三方控制項,實現表單控制項的一鍵取值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.