Solved

Highlight list box when not focused in Delphi

Posted on 2009-04-14
1
580 Views
Last Modified: 2013-11-23
In my Delphi 7 app I have a listbox and buttons to enable the user to move items up or down in the listbox. However, as soon as either button is clicked the highlight in the list box disappears. I would like to keep the highlight and have tried using SendMessge with the LB_SETCARETINDEX message but this does not work. The code is below and I would like to know how to get this to work.

procedure TfrmMain.cmdMoveUpClick(Sender: TObject);

var CurrIndex: integer;

begin

  with lstSelectedImages do

    if ItemIndex > 0 then begin

      CurrIndex:=ItemIndex;

      Items.Move(ItemIndex, (CurrIndex - 1));

      ItemIndex:=CurrIndex-1;

      SendMessage(lstSelectedImages.Handle, LB_SETCARETINDEX, ItemIndex, 0);

    end;

end;

Open in new window

0
Comment
Question by:galcott1
1 Comment
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
Comment Utility
Try this. It works well for me.
procedure TForm1.btnMoveUpClick(Sender: TObject);

var Index : Integer;

begin

     if lst1.ItemIndex > 1 then

          begin

          Index := lst1.ItemIndex  -1;

          lst1.Items.Move(lst1.ItemIndex, Index);

          lst1.ItemIndex := Index;

          lst1.Selected[Index] := True;

          lst1.SetFocus

          end;
 

end;
 

procedure TForm1.btnMoveDnClick(Sender: TObject);

var Index : Integer;

begin

     if (lst1.ItemIndex > 0) and (lst1.ItemIndex < lst1.Items.Count -1) then

          begin

          Index := lst1.ItemIndex  +1;

          lst1.Items.Move(lst1.ItemIndex, Index);

          lst1.ItemIndex := Index;

          lst1.Selected[Index] := True;

          lst1.SetFocus

          end;

end;

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now