日期:2014-05-17  浏览次数:21009 次

C#中如何避免手动给要显示的控件赋值?
因为要显示的页面可能会有很多控件,而且控件的种类也不确定,可有有TextBox,TextField等等,有什么方法可以实现他们具有统一的入口,从这个统一入口进行赋值。
C# Csharp textbox

------解决方案--------------------
                foreach(var c in this.Controls)
                {
                    if (c is TextBox)
                    {
                        TextBox tb = (TextBox)c;
                        tb.Text = "";
                    }
                    else if (c is Button)
                    {
                        Button btn = (Button)c;
                        btn.Text = "";
                    }
                }

------解决方案--------------------
ctrl是control
Type t = ctrl.GetType();
PropertyInfo pi = t.GetProperty("Text");
                if (pi != null)
                {
                    pi.SetValue(ctrl, "testauto", null);
                }
判断是不是为空。不为空就设置。为空那么就没有这个属性。就不用设了。
------解决方案--------------------
引用:
ctrl是control
Type t = ctrl.GetType();
PropertyInfo pi = t.GetProperty("Text");
                if (pi != null)
                {
                    pi.SetValue(ctrl, "testauto", nul……