Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Editing or addings to a Tlistbox at runtime

Posted on 2004-09-08
16
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 100 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 23

Expert Comment

by:Ferruccio Accalai
ID: 12004416
wich color? Selection color or text color?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:computerprogramer
ID: 12004420
Text Colour
0
 
LVL 23

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 23

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 23

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 23

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 100 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

688 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