此屬性用于設置返回列表框中被選擇的項的序號。如果MultiSelect 屬性設為True,并且用戶選擇了多個項,此屬性返回其中有輸入焦點的項的序號。程序示例代碼如下:
[delphi] view plain copyPRocedure TForm1.FormCreate(Sender: TObject); var I: Integer; begin ListBox1.MultiSelect: = False; Button.Caption = ’Move to Top’; for I: = 1 to 10 do ListBox1.Items.Add(’Item’ + IntToStr(I)); end; procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.Items.Move(ListBox1.ItemIndex, 0); end;1、ListBox1 的 Style 屬性改為 lbOwnerDrawVariable2、在ListBox的OnDrawItem事件裡,根據item的值,改變Canvas屬性即可。
例如:
[delphi] view plain copyprocedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin //字體用原來默認的顏色 if Odd(index) then //當items的index為奇數時的顏色 begin listbox1.Canvas.Brush.Color:=clwindow; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end else //當items的index為偶數時的顏色 begin listbox1.Canvas.Brush.Color:=clinactivecaptiontext; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end; if odSelected in state then //當選定時的顏色 begin listbox1.Canvas.Brush.Color:=clhighlight; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end; end;新聞熱點
疑難解答
圖片精選