能否把TextBox的Text属性利用数据绑定来绑定到简单的字段上 比如绑定到类成员string name;没到name被改变是就自动反应到TextBox上

解决方案 »

  1.   

    bindsource1.add(object obj)
     textBox1.DataBindings.Add("Text", bindingSource1, "obj的成员名");有空自己看一下msdn上bindsource和textBox1.DataBindings的说明,都是很简单的东西,没啥好多解释的
      

  2.   

    是这样做么?可以没有效果 Name改变的时候TextBox的内容没有跟着变
    public partial class Form1 : Form
        {
            public string str{get;set;}
            fact _fact = new fact();
                    public Form1()
            {
                InitializeComponent();
                _fact.Name = "jiong";
                BindingSource bs = new BindingSource();
                bs.Add(_fact);
                textBox1.DataBindings.Add("Text", bs, "Name");
               
            }        private void button1_Click(object sender, EventArgs e)
            {
                _fact.Name += "Click";
            }        private void Form1_Load(object sender, EventArgs e)
            {        }
        }
        public class fact
        {
            public string Name { get; set; }    }
      

  3.   

    恩,的确是这样做
    不过少了一句
     bindingSource1.ResetCurrentItem();改成:
     private void button1_Click(object sender, EventArgs e)
            {
                _fact.Name += "Click";
                 bs.ResetCurrentItem();
            }就可以了ps:另外也可以 用bindingSource1.ResetBindings(true)重置所有项。
      

  4.   

    其实可以重新封装一下TEXTBOX控件,重写TEXT_CHANGED事件,把字段绑上,这样你就可以随时使用这个控件,比较方便