使用Dev第三方控件开发时,碰到在TextEdit控件EditValueChanged事件中为此控件赋值无效

开发时碰到此问题,举个例子,在winform中拖放一个textEdit控件,命名为txtEdit,绑定EditValueCahged事件。在事件中编写txtEdit.Editvalue=0;但是运行程序时,发现赋值无效。代码如下

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void txtEdit_EditValueChanged(object sender, EventArgs e)
    {
      txtEdit.EditValue = 0;
    }

  }
}

研究了很长时间,发现如果textEdit控件不失去焦点,在EditValueChanged事件赋值后成功不了。后来想了个办法,在界面上放一个隐藏的button按钮,名为button1.在赋值后,先把焦点移动到button上,然后再将光标移回text。这样就触发了事件,赋值成功。

代码如下:

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void txtEdit_EditValueChanged(object sender, EventArgs e)
    {
      txtEdit.EditValue = 0;
      button1.Focus();
      txtEdit.Focus();
      //此处将光标移动到输入文本的最后
      txtEdit.SelectionStart = txtEdit.Text.Length;
    }

  }
}

posted @ 2016-01-27 17:04  纪晓鱼  阅读(1728)  评论(0编辑  收藏  举报