Loading ...
Sorry, an error occurred while loading the content.
 

Re: Select an item in a list box.

Expand Messages
  • brentje
    Never mind...after a good night s sleep, it s come to me. Here s the proper code. I had tried something like this before, but it wasn t working either. I
    Message 1 of 4 , Jan 12, 2006
      Never mind...after a good night's sleep, it's come to me. Here's the
      proper code. I had tried something like this before, but it wasn't
      working either. I thought it had something to do with the
      marshalling, which is why I started working with
      AllocateVirtualBuffer, but it was an issue with the pointer.

      As a warning, the variable names and most of the code in this example
      have no standards...


      my $FieldValue = "An Item";

      my $Index;

      my $SearchBuffer = $s_FieldValue . "\0";
      my $Searchbuf_ptr = pack( 'P', $SearchBuffer );
      my $Searchptr = unpack( 'L!', $Searchbuf_ptr );
      $Index = SendMessage( $H_ListBox, 0x018C, -1, $Searchptr );


      This will select the item and return the item's index, or LB_ERR if
      something went wrong. Unfortunately, it won't cause the selection
      change routine to fire. At the very least it makes the item visible
      in the list when it's selected. So I click on the item to get it to
      fire the selection change, which was a lot more difficult then I
      thought. There's a message you can send that will return the item's
      RECT values, LB_GETITEMRECT. It's hex code is 0x0198. But it
      requires a pointer to a RECT struct. Not a hard concept, unless
      you've never done it before. Here's the code I wrote to handle it.

      my $buffer = pack( "LLLL",
      0,
      0,
      0,
      0,
      );

      if(SendMessage( $H_ListBox, 0x0198 , $Index, unpack( 'L!', pack( 'P',
      $buffer ) ) ) > -1)
      {

      my ($i_LeftX, $i_TopY, $i_RightX, $i_BottomY) = unpack( 'L L L L',
      $buffer );

      my $i_X = $i_LeftX + (int(($i_RightX - $i_LeftX)/2));
      my $i_Y = $i_TopY + (int(($i_BottomY - $i_TopY)/2));

      my ($sx, $sy) = ClientToScreen( $H_ListBox, $i_X, $i_Y );

      MouseMoveAbsPix($sx, $sy);
      SendMouse("{LEFTCLICK}");
      }

      It seems the most important part to learn and understand are the
      unpack/pack statements. Although I'm still having trouble grasping
      unpack( 'L!', pack( 'P', $buffer ) ) ). But I know this code works.
      I'll let the understanding come to me on it's own. Good luck to
      anyone else working with listboxes or the SendMessage command, and I
      hope this helps.

      Brent


      --- In perlguitest@yahoogroups.com, "brentje" <brentje@y...> wrote:
      >
      > I'm going crazy. I can't look at this code anymore. Could someone
      > please tell me what I'm doing wrong with this SendMessage statement.
      >
      > For clarification, 0x018C is the hex code for the LB_SELECTSTRING
      message.
      >
      >
      >
      > my $FieldValue = "An Item\0";
      >
      > my $lvitem = AllocateVirtualBuffer( $H_ListBox, length($s_FieldValue) );
      >
      > WriteToVirtualBuffer( $lvitem, $FieldValue );
      > SendMessage( $H_ListBox, 0x018C , -1, $lvitem->{ 'ptr' } );
      >
      > Result comes back with 0. Which isn't right since the item's index is
      > actually 3, but isn't an error code either. It doesn't select anything.
      >
      >
      > Here's the link to the MSDN description of LB_SELECTSTRING.
      >
      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listboxes/listboxreference/listboxmessages/LB_SELECTSTRING.asp
      >
      > Any help would be appreciated, since I have to work with a lot of
      > listboxes.
      >
      > Thanks
      > Brent
      >
    • brentje
      Hey, thanks for the reply, even though I managed to muddle through it myself eventually. I m slowly getting my head around the SendMessage function. Now I m
      Message 2 of 4 , Jan 17, 2006
        Hey, thanks for the reply, even though I managed to muddle through it
        myself eventually. I'm slowly getting my head around the SendMessage
        function. Now I'm onto a different task that will probably involve
        SendMessage. I'm trying to read the records in a datagrid. Datagrids
        are tricky, since they are pure .Net controls and not standard Windows
        controls like Listbox and Combobox. They don't seem to have any
        messages associated with them on MSDN. Short of writing an extension
        to the datagrid class, are there any ways to interact with a datagrid?
        Any help would be appreciated.

        Thanks
        Brent

        --- In perlguitest@yahoogroups.com, "Piotr Kaluski" <pkaluski@p...> wrote:
        >
        > ListBox is a standard control so you don't have to bother with Virtual
        > Memory functions.
        > Last parameter of SendMessage should be a pointer to a string, which
        > is not a straightforward thing.
        >
        > Try this:
        > my $value = "Value";
        >
        > my $ptr = pack( 'P', $value );
        >
        > SendMessage( $H_ListBox, 0x018C , -1, unpack( 'L!', $ptr ) );
        >
        > Have a look at perlpacktut manual page.
        >
        > Note: I didn't try the code above.
        >
        > --Piotr
        >
        > --- In perlguitest@yahoogroups.com, "brentje" <brentje@y...> wrote:
        > >
        > > I'm going crazy. I can't look at this code anymore. Could someone
        > > please tell me what I'm doing wrong with this SendMessage statement.
        > >
        > > For clarification, 0x018C is the hex code for the LB_SELECTSTRING
        > message.
        > >
        > >
        > >
        > > my $FieldValue = "An Item\0";
        > >
        > > my $lvitem = AllocateVirtualBuffer( $H_ListBox,
        length($s_FieldValue) );
        > >
        > > WriteToVirtualBuffer( $lvitem, $FieldValue );
        > > SendMessage( $H_ListBox, 0x018C , -1, $lvitem->{ 'ptr' } );
        > >
        > > Result comes back with 0. Which isn't right since the item's index is
        > > actually 3, but isn't an error code either. It doesn't select
        anything.
        > >
        > >
        > > Here's the link to the MSDN description of LB_SELECTSTRING.
        > >
        >
        http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listboxes/listboxreference/listboxmessages/LB_SELECTSTRING.asp
        > >
        > > Any help would be appreciated, since I have to work with a lot of
        > > listboxes.
        > >
        > > Thanks
        > > Brent
        > >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.