Solved

Editing or addings to a Tlistbox at runtime

Posted on 2004-09-08
16
292 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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