//In Form's Button1, we use a Button as the client to add new item
private void button1_Click(object sender, System.EventArgs e)
{
this.listBox1.Items.Add("newitem");
}
//We implement a customized ListBoxListener for ListBox
private const int LB_ADDSTRING=0x0180;
public class ListBoxListener: NativeWindow
{
public ListBoxListener(ListBox lb)
{
lb.HandleDestroyed+= new EventHandler(this.OnHandleDestroyed);
}
internal void OnHandleDestroyed(object sender, EventArgs e)
{
// Window was destroyed, release hook.
ReleaseHandle();
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.Secur
ityAction.Demand, Name="FullTrust")]
protected override void WndProc(ref Message m)
{
// Listen for operating system messages
switch (m.Msg)
{
case LB_ADDSTRING:
MessageBox.Show("A new item is added in the listbox");
break;
}
base.WndProc(ref m);
}
}
//In Form.Button2, we register ListBoxListener with the ListBox1
private void button2_Click(object sender, System.EventArgs e)
{
ListBoxListener lbl=new ListBoxListener(this.listBox1);
lbl.AssignHandle(this.listBox1.Handle);
}