Link to home
Create AccountLog in
Avatar of PeterdeB
PeterdeBFlag for Netherlands

asked on

How to make sure an item in the listbox is always selected?

Hi Folks,

I want my listbox to always have the top most item selected, how can I get this done?

Kind regards,

Dweep
SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of PeterdeB

ASKER

HI DG and Mister LittleWood ;-)

I see I did not make myself clear enough. Ehm currently my code is causing me problems since I need the Listbox click to get the item clicked, into an editbox. At least, I think that is causing the problem. When I click on the Listbox, it sometimes jumps to another item, without selecting the one I clicked first. Therefore I thought to circumvent;-) this problem by forcing always one item in the Listbox to be selected.

Geez.....if reading this is as hard as writing it....then you prolly won't understand what I'm saying and as I mention this...I'm asking myself that same question....

Dweep ;-)
Try the OnChange event instead.
Sorry, thats for a TComboBox... Try OnMousedown.
ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
refresh before post, refresh before post. grrrr ;-)
Okay fair enough here goes. This is how I get the items from the hosts file in my editbox without the 127.0.0.1 + space part.
I get the idea this should be handled differently...so please help....

Kind regards,

Dweeep
procedure TfrmMain.lst1Click(Sender: TObject);
var p,s,d:string;
 begin
   p:= lst1.items[lst1.itemindex];
   s:= '127.0.0.1 ';
   d:= SearchAndReplace(p,s,'');
   edt1.Text := d;
 
end;  

Open in new window

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Okay Steve, than I will dive in deeper and return more code. Thanks for pointing this out, it was not obvious to me. Here's the onchange handler from the edit box.

Dweep
procedure TfrmMain.edt1Change(Sender: TObject);
 var
  S: array[0..255] of Char;
begin
 // UpDateState;
  StrPCopy(S, '127.0.0.1  '+edt1.Text);
  with lst1 do
    ItemIndex := Perform(LB_SELECTSTRING, 0, LongInt(@S));
end;

Open in new window

Perhaps you should disable the OnClick event of the ListBox while changing it's item index.
  with lst1 do
    begin 
    OnClick := nil;
    ItemIndex := Perform(LB_SELECTSTRING, 0, LongInt(@S));
    OnClick := lst1Click;
    end;

Open in new window

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Follks, thanks for your replies. Consider this question unasked ;-) Cause indeed it did not make complete sense, but I appreciate your efforts nonetheless.

Kind regards,

Dweep ;-)