ChrisLM
asked on
Change Listbox color with OnEnter and OnExit
Hi
I would like to change the Listbox color when you click on it and another color once another control has focus.
Listbox has focus, color = red.
Listbox has no focus, color = white.
I tryed:
procedure TForm1.ListBox1Enter(Sende r: TObject);
begin
ListBox1.Color := clBlue;
end;
procedure TForm1.ListBox1Exit(Sender : TObject);
begin
ListBox1.Color := clWindow;
end;
But once the listbox has a few lines the color doesn't changed properly.
Thanks
Chris.
I would like to change the Listbox color when you click on it and another color once another control has focus.
Listbox has focus, color = red.
Listbox has no focus, color = white.
I tryed:
procedure TForm1.ListBox1Enter(Sende
begin
ListBox1.Color := clBlue;
end;
procedure TForm1.ListBox1Exit(Sender
begin
ListBox1.Color := clWindow;
end;
But once the listbox has a few lines the color doesn't changed properly.
Thanks
Chris.
in addition to the above, you will have to draw the whole rectangle of the ListBox somewhere in OnDrawItem
with FullRect do
begin
Left := 0;
Top := 0;
Right := Width;
Bottom := Height;
end;
FillRect(FullRect);
and after that draw the items
with FullRect do
begin
Left := 0;
Top := 0;
Right := Width;
Bottom := Height;
end;
FillRect(FullRect);
and after that draw the items
ASKER
Thanks for your input.
Can you send the code for the whole form, with the additional piece of code added, as I tryed what your last two comments said, but I cannot get it too work,
Thanks
Chris
Can you send the code for the whole form, with the additional piece of code added, as I tryed what your last two comments said, but I cannot get it too work,
Thanks
Chris
Aaaa, yes, not I understand what I forgot (in order to draw the ListBox, you have to set it's style to lbOwnerDrawFixed). I set this in Design time, but it can be done in OnCreate
procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawFixed;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawFixed;
end;
ASKER
That helped a little, but 1 slight problem.
The items are not being displayed, so I must be doing something wrong with the OnDrawItem procedure. I have this:
var
FullRect: TRect;
begin
with ListBox1.Canvas do
begin
Brush.Color := ListBoxColor;
FillRect(Rect);
TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[index]);
end;
with FullRect do
begin
Left := 0;
Top := 0;
Right := Width;
Bottom := Height;
end;
Listbox1.Canvas.FillRect(F ullRect);
end;
The items are not being displayed, so I must be doing something wrong with the OnDrawItem procedure. I have this:
var
FullRect: TRect;
begin
with ListBox1.Canvas do
begin
Brush.Color := ListBoxColor;
FillRect(Rect);
TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[index]);
end;
with FullRect do
begin
Left := 0;
Top := 0;
Right := Width;
Bottom := Height;
end;
Listbox1.Canvas.FillRect(F
end;
There are 2 problems here:
1) You draw the item and the FullRect, which hides the items.
2) My suggestion with the FullRect was NOT good, because OnDrawItem is called for all items. And if you draw FullRect for the second item, it Hides the first one, which was already drawn.
Wait few minutes, I am trying something...
1) You draw the item and the FullRect, which hides the items.
2) My suggestion with the FullRect was NOT good, because OnDrawItem is called for all items. And if you draw FullRect for the second item, it Hides the first one, which was already drawn.
Wait few minutes, I am trying something...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Edit1: TEdit;
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure ListBox1Enter(Sender: TObject);
procedure ListBox1Exit(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
ListBoxColor : TColor;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ListBox1DrawItem(Co ntrol: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
with ListBox1.Canvas do
begin
Brush.Color := ListBoxColor;
FillRect(Rect);
Font.Color := clBlack;
TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[index]);
end;
end;
procedure TForm1.ListBox1Enter(Sende r: TObject);
begin
ListBoxColor := clRed;
ListBox1.Refresh;
end;
procedure TForm1.ListBox1Exit(Sender : TObject);
begin
ListBoxColor := clWhite;
ListBox1.Refresh;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawFixed;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Edit1: TEdit;
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure ListBox1Enter(Sender: TObject);
procedure ListBox1Exit(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
ListBoxColor : TColor;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ListBox1DrawItem(Co
Rect: TRect; State: TOwnerDrawState);
begin
with ListBox1.Canvas do
begin
Brush.Color := ListBoxColor;
FillRect(Rect);
Font.Color := clBlack;
TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[index]);
end;
end;
procedure TForm1.ListBox1Enter(Sende
begin
ListBoxColor := clRed;
ListBox1.Refresh;
end;
procedure TForm1.ListBox1Exit(Sender
begin
ListBoxColor := clWhite;
ListBox1.Refresh;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawFixed;
end;
end.
ASKER
Appreciate your help mate.
Your example paints the items properly, but doesn't paint the rest of the listbox?
Thanks.
Your example paints the items properly, but doesn't paint the rest of the listbox?
Thanks.
Yes, I notices that problem. But the ONLY solution I found here is to set the ListBox height in a proper way - to display exact number of items and you will not have additional space.
I couldn't solve this problems, because as I told you in problem 2) - if you draw the background for item 2 - you will erase item 1...
I couldn't solve this problems, because as I told you in problem 2) - if you draw the background for item 2 - you will erase item 1...
ASKER
Thanks for the help buddy.
I don't know what to do know since the question wasn't answered.
I would still like to give you a few points for your troubles though and then close the question.
Any ideas on how to give you some points??
I don't know what to do know since the question wasn't answered.
I would still like to give you a few points for your troubles though and then close the question.
Any ideas on how to give you some points??
You can if you open a new question "Points for Ivanov_G" and refer to this question.
But this is not the case here. The problems comes to be solved. But sometimes, due to some limitation we can not proceed in the way we won't, so we have to think for another alternative.
But this is not the case here. The problems comes to be solved. But sometimes, due to some limitation we can not proceed in the way we won't, so we have to think for another alternative.
ASKER
"due to some limitation we can not proceed in the way we won't, so we have to think for another alternative."
Are you saying to leave it open to wait for a solution or due to limitations this problem cannot be solved?
Are you saying to leave it open to wait for a solution or due to limitations this problem cannot be solved?
No, I don't say it can not be solved. Each problem have its solution.
But now because of not so good written VCL there is a bug. And we are trying to override it. I've done the same this you want, but with TEdit-s and there is works file. This is what I mean.
But now because of not so good written VCL there is a bug. And we are trying to override it. I've done the same this you want, but with TEdit-s and there is works file. This is what I mean.
ASKER
ok thanks.
I posted a comment "Points for Ivanov_G" for some points to you.
I will leave the solution open for a few days to see if anyone else can come up with something.
Thanks.
I posted a comment "Points for Ivanov_G" for some points to you.
I will leave the solution open for a few days to see if anyone else can come up with something.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, note to the above code sample. You don't need to set ownerdraw or anything. Just use lbStandard.
Tyrsis
Tyrsis
ASKER
Thanks Tyrsis.
That was exactly what I needed and worked perfectly. Points are yours.
Chris.
That was exactly what I needed and worked perfectly. Points are yours.
Chris.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Edit1: TEdit;
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure ListBox1Enter(Sender: TObject);
procedure ListBox1Exit(Sender: TObject);
private
ListBoxColor : TColor;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ListBox1DrawItem(Co
Rect: TRect; State: TOwnerDrawState);
begin
with ListBox1.Canvas do
begin
Brush.Color := ListBoxColor;
FillRect(Rect);
TextOut(Rect.Left + 2, Rect.Top + 2, ListBox1.Items[index]);
end;
end;
procedure TForm1.ListBox1Enter(Sende
begin
ListBoxColor := clRed;
ListBox1.Invalidate;
end;
procedure TForm1.ListBox1Exit(Sender
begin
ListBoxColor := clWhite;
ListBox1.Invalidate;
end;
end.