C# 小知识的积累 DataBinding使用

类型转换;

      显示转换,隐式转换 

显示转换:注意数据是否益处

checked(expression)

unchecked(expression);//可以改变益出检查的默认设置

      枚举转换

Ex:

   static void Main(string[] args)
        {
            string str = "noth";
       
        test t = (test)Enum.Parse(typeof(test), str);
        }
         enum test
    {
        noth,
        noth1
    }
View Code

 控件添加:

  

private void button1_Click(object sender, EventArgs e)
        {
           
                ((Button)sender).Text = "Clicked"; 

                 Button button = new Button();

                 button.Text = "按钮";

                 button.Size = new Size(100, 30);//控件大小

                 button.Location = new Point(0, 0); //位置

                 button.Click += delegate { ButtonClick(); }; //新控件增加执行的方法

                  this.Controls.Add(button);

    }

    void ButtonClick() //方法

    {

      MessageBox.Show("点击了click事件");

    }
  ((Button)sender).Text = "Clicked";
            //Button bt = new Button();
            Button bt = new Button();
            bt.Location = new Point(20, 60);
            bt.Text = "New Button";
            //t.Click += new EventHandler(bt_Click); 
            bt.Click += new EventHandler(bt_Click);
            Controls.Add(bt);
            

        }
        void bt_Click(object sender, EventArgs e)
        {
            //((Button)sender).Text = "Hello";
            MessageBox.Show("HelloWord");
        }

 DataBinding使用:

ex:案例:http://www.cnblogs.com/scy251147/archive/2011/10/23/2221991.html

        private void button1_Click(object sender, EventArgs e)
        {
            Person p = new Person();
            p.MyValue="Hello";            
              textBox1.DataBindings.Add("text", p, "MyValue", false, DataSourceUpdateMode.OnPropertyChanged);
        }
    }
    class Person
    {
        public string MyValue { get; set; }
    }
View Code

解释:

/************************************************
  * 第一个值:要绑定到TextBox的什么地方
  * 第二个值:数据源是什么
  * 第三个值:应该取数据源的什么属性
  * 第四个值:是否开启数据格式化
  * 第五个值:在什么时候启用数据源绑定
* *********************************************/

原文地址:https://www.cnblogs.com/haimingkaifa/p/5953158.html