SendMessage WM_SETTEXT到TextBox不会触发TextChanged事件 [英] SendMessage WM_SETTEXT to TextBox doesn't trigger TextChanged Event

查看:96
本文介绍了SendMessage WM_SETTEXT到TextBox不会触发TextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以获取文本框控件的句柄,并使用Windows API更改文本.文本更新时不会触发TextChanged事件.

I have code that gets the handle to a textbox control and uses the windows API to change the text. The TextChanged event doesn't fire when the text is updated.

是否可以使用Windows API触发TextBox.TextChanged事件?

Is there a way to fire the TextBox.TextChanged event using the Windows API?

[更新]
我认为事件不触发的原因是因为文本框句柄是通过DCOM接口发送的.该程序是用c#编写的National Instruments TestStand外壳,并使用NI TestStand COM对象实现核心功能.在TS序列文件(一种TS脚本语言)中,我为文本框句柄创建了一个对象引用,并在外壳窗体的load事件中使用TS api对其进行了设置.之后,我将句柄发送到我的c#DLL.我使用SendMessage更新文本框,效果很好.问题是不会触发TextChanged事件.

[Update]
I think the reason the event doesn't fire is because the textbox handle is sent though a DCOM interface. The program is a National Instruments TestStand shell written in c# and uses the NI TestStand COM object for the core functionality. In the TS sequence file (a sort of TS script language) I created an object reference for the textbox handle and set it using the TS api in the shell form's load event. After that I send the handle to my c# DLL. I use SendMessage to update the textbox and that works good. The problem is that the TextChanged event doesn't fire.

我尝试使用TS接口发送文本框和TextChanged委托,但无法正常工作.我认为存在通过TS COM对象执行此操作的AppDomain问题.

I tried using the TS interface to send the textbox and the TextChanged delegate and I couldn't get it to work. I think there is an AppDomain issue doing that through the TS COM object.

推荐答案

此程序证明,向控件发送 WM_SETTEXT 消息时,会触发 TextChanged 事件.

As this program proves, the TextChanged event does fire when the control is sent a WM_SETTEXT message.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const uint WM_SETTEXT = 0x000C;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, unit Msg, 
            IntPtr wParam, string lParam);

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show(textBox1.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendMessage(textBox1.Handle, WM_SETTEXT, IntPtr.Zero,
              textBox1.Text + ", " + textBox1.Text);
        }
    }
}


请注意,答案的原始版本过于复杂,并使用了如下的 SendMessage :

static extern IntPtr SendMessage(IntPtr hWnd, unit Msg, 
  IntPtr wParam, IntPtr lParam);

,因此不得不执行手动编组:

and consequently had to perform manual marshalling:

IntPtr text = Marshal.StringToCoTaskMemUni(textBox1.Text + ", "
  + textBox1.Text);
SendMessage(textBox1.Handle, WM_SETTEXT, IntPtr.Zero, text);
Marshal.FreeCoTaskMem(text);

对此问题的评论(

Comments at this question (Automatic casting for string DllImport arguments vs Marshal.StringToCoTaskMemUni) persuaded me to update.

这篇关于SendMessage WM_SETTEXT到TextBox不会触发TextChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆