Solved

Editing or addings to a Tlistbox at runtime

Posted on 2004-09-08
16
291 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
Comment Utility
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items[ListBox1.itemindex] := ListBox1.Items[ListBox1.itemindex]+ ' HI';
end;

F68 ;-)
0
 

Author Comment

by:computerprogramer
Comment Utility
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
Comment Utility
wich color? Selection color or text color?
0
 

Author Comment

by:computerprogramer
Comment Utility
Text Colour
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:Ferruccio Accalai
Comment Utility
Have you tried the code above?
0
 
LVL 10

Expert Comment

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

Expert Comment

by:Ferruccio Accalai
Comment Utility
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
Comment Utility
All i want it on click the colour of the selected item changes
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

16 Experts available now in Live!

Get 1:1 Help Now