Solved

Editing or addings to a Tlistbox at runtime

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

Expert Comment

by:Ferruccio Accalai
ID: 12004416
wich color? Selection color or text color?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

623 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