9 Replies - 414 Views - Last Post: 11 July 2012 - 10:48 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 559
  • Joined: 31-August 11

Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 09:36 AM

Tyring To Hook The windows procedure which works for everything else except what I want to try and detect listbox strings being added to my listboxes (like an event) here

   private const int LB_ADDSTRING = 0x180;
    private const int LB_INSERTSTRING = 0x181;
    private const int LB_DELETESTRING = 0x182;
    private const int LB_RESETCONTENT = 0x184;

protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
{
 case LB_ADDSTRING:
MessageBox.Show("Detected Listbox Event");
break;
}
        base.WndProc(ref m);
    }




I've tried all of these as a thought experiment to see if they work...they don't? I'm sure I'm doing something stupid any idea on what the problem is? I have a switch for each one of these they aren't working

Is This A Good Question/Topic? 0
  • +

Replies To: Hooking The Windows Procedure To Detect Listbox Events?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 09:40 AM

What is it you are trying to detect? The Listbox already has a long list of events you can subscribe to in a proper managed C# way.
Was This Post Helpful? 0
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 924
  • View blog
  • Posts: 926
  • Joined: 30-September 10

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 12:34 PM

The managed ListBox control doesn't expose any managed events specifically in response to an item being added/removed.

adn258, I'm guessing you've put your code in the Form class' WndProc(). You need to create your own class that derives from the ListBox class, and override its WndProc() instead.

Something like this should work:

Spoiler

Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 12:47 PM

I always like to assume that I'm not the first guy that needs something, then do a google.

In this case I figured it would be nice if there was an event for a listbox item added. And it would be really nice if the example found would be for C#. So I googled "C# Listbox item added event" and got several good targeted pages.


This one is very much along the lines of what you were trying but the approach is to NOT do it in your program, but to inherit from Listbox to make a new extended ListBox that has this event as a proper C# event. Now you use the ListBoxEx in place of the default ListBox, but your application doesn't try to micromanage every GUI control.
Was This Post Helpful? 1
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 559
  • Joined: 31-August 11

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 03:21 PM

View PostCodingSup3rnatur@l-360, on 11 July 2012 - 12:34 PM, said:

The managed ListBox control doesn't expose any managed events specifically in response to an item being added/removed.

adn258, I'm guessing you've put your code in the Form class' WndProc(). You need to create your own class that derives from the ListBox class, and override its WndProc() instead.

Something like this should work:

Spoiler



Thanks that's what I was after tlhIn`toq was right I googled that first and that's where I got the initial concept but he/she didn't explain how they did that very well. I really appreciate the help. I didn't know you could extend the wndprc procedure on a particular object like you just exposed; god I swear the amount you have to keep learning programming is crazy does the learning curve ever stop lol? Like I swear just when you think you know what you're doing a little even a tiny bit you realize you don't know anything.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,476
  • Joined: 08-June 10

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 03:42 PM

Quote

god I swear the amount you have to keep learning programming is crazy does the learning curve ever stop lol


It can, if you want to be typecast into a certain type of developer. We have mainframe programmers. They know pretty much everything they need to know about their environment, because it hasn't changed in any extreme way in 30 years.

Or you could be a VB6 programmer, using a language that has been abandoned by its creators, but not by companies with legacy applications written in it.

But if you want to stay current, it never stops. For instance, in a few years a lot of your Windows Forms knowledge will be almost useless, considering MS has been focusing all it's efforts on WPF and WinRT/Metro. Forms apps will still be supported, but there hasn't been a significant update to WinForms since .NET 2.0.
Was This Post Helpful? 1
  • +
  • -

#7 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 559
  • Joined: 31-August 11

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 04:10 PM

That said when I try creating this custom controls I added a class file to do it I haven't had to do this in awhile how do I can't access the controls main functions to test this? For instance

public Form1()
        {
            MyListBox ls = new MyListBox();
            ls.Name = "lsbx";
            ls.Size = new System.Drawing.Size(475, 238);
            ls.Location = new System.Drawing.Point(12, 12);
            ls.Enabled = true;
            Controls.Add(ls);
            InitializeComponent();
        }

       
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
          ls.Items.Add(
        }




You can't items.add stuff to it I just want to test this out of curiosity but it doesn't work? It's not available in this scope but why the control is added to the main form?

EDIT

Never mind I forgot to add public too it I believe that was the problem *hits head*/ I'm sick today with nausea etc. stomach bug so forgive me if I'm an extra idiot

This post has been edited by adn258: 11 July 2012 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 2035
  • View blog
  • Posts: 6,058
  • Joined: 05-May 12

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 04:19 PM

That's because ls is local to your constructor. Of course, the button click handler can't get to it.
Was This Post Helpful? 1
  • +
  • -

#9 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 559
  • Joined: 31-August 11

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 10:31 PM

View PostSkydiver, on 11 July 2012 - 04:19 PM, said:

That's because ls is local to your constructor. Of course, the button click handler can't get to it.


but if you put the MyListBox ls = new MyListBox();
as public outside the constructor it works fine; I know that was a really dumb question sorry dude...
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 2035
  • View blog
  • Posts: 6,058
  • Joined: 05-May 12

Re: Hooking The Windows Procedure To Detect Listbox Events?

Posted 11 July 2012 - 10:48 PM

View Postadn258, on 11 July 2012 - 10:31 PM, said:

View PostSkydiver, on 11 July 2012 - 04:19 PM, said:

That's because ls is local to your constructor. Of course, the button click handler can't get to it.


but if you put the MyListBox ls = new MyListBox();
as public outside the constructor it works fine; I know that was a really dumb question sorry dude...


Never make a member variable public unless you really have to. Just declare MyListBox ls as a private member variable and that should be good for most purposes.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1