此屬性用于設(shè)置返回列表框中被選擇的項(xiàng)的序號。如果MultiSelect 屬性設(shè)為True,并且用戶選擇了多個項(xiàng),此屬性返回其中有輸入焦點(diǎn)的項(xiàng)的序號。程序示例代碼如下:
[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事件裡,根據(jù)item的值,改變Canvas屬性即可。
例如:
[delphi] view plain copyprocedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin //字體用原來默認(rèn)的顏色 if Odd(index) then //當(dāng)items的index為奇數(shù)時的顏色 begin listbox1.Canvas.Brush.Color:=clwindow; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end else //當(dāng)items的index為偶數(shù)時的顏色 begin listbox1.Canvas.Brush.Color:=clinactivecaptiontext; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end; if odSelected in state then //當(dāng)選定時的顏色 begin listbox1.Canvas.Brush.Color:=clhighlight; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end; end;新聞熱點(diǎn)
疑難解答
圖片精選