Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox contents changed notification.

Status
Not open for further replies.

jv6886

Programmer
Dec 31, 2002
46
0
0
US
I am creating a user control containing a ListBox and a Button. I am exposing to the user the Items property along with most of the other functionality of the ListBox. I was hoping that there would be an event that would notify you when the contents of the ListBox had been changed, but did not see anything like that.

Any ideas on how I could set up some kind of notification for this?

Thanks,
wcprog
 
Code:
//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);
}

I found this code somewhere else, it does exactly what you want to do. I hope you can use it.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top