تلوين ال ListBox
#1
السلام عليكم و رحمة الله و بركاته

تلوين ال ListBox

كود :
http://delphinews.files.wordpress.com/2012/10/colorlistbox.jpg

كود :
http://delphinews.files.wordpress.com/2012/10/colorlistbox2.jpg


أولا قم بتفعيل ال Owner Draw :

دلفي :

PHP كود :
self.ListBox1.Style := TListBoxStyle.lbOwnerDrawFixed ; 
دوت نت :


PHP كود :
listBox1.DrawMode = DrawMode.OwnerDrawFixed 
الآن كل العمل سيتم في الحدث DrawItem :

نقوم بفحص ترتيب ال item , اذا كان الترتيب زوجي (أي باقي القسمة على 2 يساوي 0) نقوم بتلوين ال Item باللون 1 , أما اذا كان الترتيب فردي (أي باقي القسمة لعى 2 لا يساوي صفر) نقوم بتلوين ال Item باللون الثاني ..

دلفي :
PHP كود :
procedure TMain.ListBox1DrawItem(Control: TWinControl; Index: Integer;
aRect: TRect; State: TOwnerDrawState);
var 
myListBox:TListBox;
begin
myListBox
:=TListBox(Control) ;
if 
index mod 2 = 0 then
// First Color (index is pair)
myListBox.Canvas.Brush.Color := clAqua else
// Second Color (index is impair)
myListBox.Canvas.Brush.Color:= clYellow;
// Draw Rectangle
myListBox.Canvas.FillRect(aRect);
// Set Font Color to Black
myListBox.Canvas.Font.Color :=clblack;
// Draw Items Text
myListBox.Canvas.TextOut(aRect.Left ,aRect.Top ,myListBox.Items [index]);

if 
odSelected in State then
myListBox
.Canvas.DrawFocusRect(aRect);

end; 
csharp :
PHP كود :
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox myListBox = (ListBox)sender;
SolidBrush sb = new SolidBrush (Color.White );
if (
e.Index % 2 == 0)
sb.Color = Color.Aqua;
else
sb.Color = Color.Yellow;
e.Graphics.FillRectangle(sb, e.Bounds);
e.Graphics.DrawString(myListBox.Items[e.Index].ToString (),e.Font,new SolidBrush (Color.Black ),e.Bounds );
if ((
e.State & DrawItemState.Selected) == DrawItemState.Selected)
e.DrawFocusRectangle();
} 
Vb.Net :
PHP كود :
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
Dim myListBox 
As ListBox = CType(sender, ListBox)
Dim sb As SolidBrush = New SolidBrush(Color.White)
If 
e.Index Mod 2 = 0 Then
sb
.Color = Color.Aqua
Else
sb.Color = Color.Yellow
End 
If
e.Graphics.FillRectangle(sb, e.Bounds)
e.Graphics.DrawString(myListBox.Items(e.Index).ToString, e.Font, New SolidBrush(Color.Black), e.Bounds)
If (
e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e
.DrawFocusRectangle()
End If
End Sub 
الرد


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم