Solved

Editing or addings to a Tlistbox at runtime

Posted on 2004-09-08
16
293 Views
Last Modified: 2012-05-05
Hey,

How would I add to a Tlistbox at runtime so if I clicked a Button it would add a word to the highlited item for example ...

List box item before clicked on...

Hello

Listbox Item highlighted

Button Clicked

Listbox Item added hi to ...

Hello Hi



Get my drift?

Cheers,

Computer Programer
0
Comment
Question by:computerprogramer
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 25 total points
ID: 12004384
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items[ListBox1.itemindex] := ListBox1.Items[ListBox1.itemindex]+ ' HI';
end;

F68 ;-)
0
 

Author Comment

by:computerprogramer
ID: 12004407
Hey,

Also for 10 more points can you tell me how to change the colour with that?

Cheers
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12004416
wich color? Selection color or text color?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:computerprogramer
ID: 12004420
Text Colour
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12004512
I'm not sure about your needs, btw to customize the items color you have to set the Listbox1.Style to lbOwnerDrawFixed and write an OnDrawItem event...

Something like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items[ListBox1.itemindex] := ListBox1.Items[ListBox1.itemindex]+ ' HI';
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
if Pos(' HI',Listbox1.items[index]) > 0 then
Listbox1.Canvas.Font.Color := clred
else
ListBox1.canvas.Font.Color := clblack;
ListBox1.Canvas.TextRect(Rect, Rect.Left, Rect.Top,
     ListBox1.Items[Index]);
end;
0
 

Author Comment

by:computerprogramer
ID: 12004548
Ok im rasing it to 50 because im not sure if you can do this or not,

I just want it so when you click on the button the text colour changes to red

cheers thanks for your help
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12004568
Well, the code posted above do that. Clicking on the button HI is added and the font changes to red. Do you mean this?
0
 

Author Comment

by:computerprogramer
ID: 12004602
This sounds kinda stupid but can you make it so just on button click it changes the highlighted item to red ,

Sorry for the hassle
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12004677
Have you tried the code above?
0
 
LVL 10

Expert Comment

by:Jacco
ID: 12010001
Ferruccio68: You could store the color in Items.Objects[...] ;-)
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12010206
I know it Jacco, but the fact is that until now i've not exactly understood what he need: if just a color change in onclick and then let say so the color or just the color of the selected....
Well, give me a hand with your suggestion :))
0
 

Author Comment

by:computerprogramer
ID: 12010292
All i want it on click the colour of the selected item changes
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12010382
Make this change:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  // Only change to red if selected and containing the word 'HI'
  // If you want all selected to be red,
  // remove the first condition in the if statement.
  if (Pos(' HI',Listbox1.items[index]) > 0)  and
     (gdselected in State) then
    Listbox1.Canvas.Font.Color := clRed
  else
    ListBox1.Canvas.Font.Color := clWindowText;

  ListBox1.Canvas.TextRect(Rect, Rect.Left, Rect.Top,
     ListBox1.Items[Index]);
end;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12010386
OH, do you mean you want the BACKGROUND of the item to be red?
0
 
LVL 10

Accepted Solution

by:
Jacco earned 25 total points
ID: 12010444
Okay, here is my 50 ct:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Add('test');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if ListBox1.ItemIndex >= 0 then
  begin
    ListBox1.Items.Objects[ListBox1.ItemIndex] := Pointer(clRed);
    ListBox1.Invalidate;
  end;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
  with TListBox(Control) do
  begin
    Canvas.FillRect(Rect);
    Canvas.Font.Color := TColor(Items.Objects[Index]);
    Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top,  Items[Index]);
  end;
end;

Attach the DrawItem to the ListBox.OnDrawItem and make sure you set ListBox.Style to lbOwnerDrawFixed

Regards Jacco
0
 

Author Comment

by:computerprogramer
ID: 12012929
Thankyou both Jacco and Ferruccio68  you both got what answers I wanted.
I split the points in half,

Cheers,

And thanks everyone else,

Computer Programer
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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