當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET ComboBox.DrawItem事件代碼示例

本文整理匯總了VB.NET中System.Windows.Forms.ComboBox.DrawItem事件的典型用法代碼示例。如果您正苦於以下問題:VB.NET ComboBox.DrawItem事件的具體用法?VB.NET ComboBox.DrawItem怎麽用?VB.NET ComboBox.DrawItem使用的例子?那麽, 這裏精選的事件代碼示例或許可以為您提供幫助。您也可以進一步了解該事件所在System.Windows.Forms.ComboBox的用法示例。


在下文中一共展示了ComboBox.DrawItem事件的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: InitializeComboBox

Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
  Private animals() As String

  ' This method initializes the owner-drawn combo box.
  ' The drop-down width is set much wider than the size of the combo box
  ' to accomodate the large items in the list.  The drop-down style is set to 
  ' ComboBox.DropDown, which requires the user to click on the arrow to 
  ' see the list.
  Private Sub InitializeComboBox()
      Me.ComboBox1 = New ComboBox
      Me.ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
      Me.ComboBox1.Location = New System.Drawing.Point(10, 20)
      Me.ComboBox1.Name = "ComboBox1"
      Me.ComboBox1.Size = New System.Drawing.Size(100, 120)
      Me.ComboBox1.DropDownWidth = 250
      Me.ComboBox1.TabIndex = 0
      Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
      animals = New String() {"Elephant", "c r o c o d i l e", "lion"}
      ComboBox1.DataSource = animals
      Me.Controls.Add(Me.ComboBox1)
  End Sub

  ' If you set the Draw property to DrawMode.OwnerDrawVariable, 
  ' you must handle the MeasureItem event. This event handler 
  ' will set the height and width of each item before it is drawn. 
   Private Sub ComboBox1_MeasureItem(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
          Handles ComboBox1.MeasureItem

      Select Case e.Index
          Case 0
              e.ItemHeight = 45
          Case 1
              e.ItemHeight = 20
          Case 2
              e.ItemHeight = 35
      End Select
      e.ItemWidth = 260

  End Sub

  ' You must handle the DrawItem event for owner-drawn combo boxes.  
  ' This event handler changes the color, size and font of an 
  ' item based on its position in the array.
  Private Sub ComboBox1_DrawItem(ByVal sender As Object,  _ 
      ByVal e As System.Windows.Forms.DrawItemEventArgs) _
      Handles ComboBox1.DrawItem

      Dim size As Single
      Dim myFont As System.Drawing.Font
      Dim family As FontFamily

      Dim animalColor As New System.Drawing.Color
      Select Case e.Index
          Case 0
              size = 30
              animalColor = System.Drawing.Color.Gray
              family = FontFamily.GenericSansSerif
          Case 1
              size = 10
              animalColor = System.Drawing.Color.LawnGreen
              family = FontFamily.GenericMonospace
          Case 2
              size = 15
              animalColor = System.Drawing.Color.Tan
              family = FontFamily.GenericSansSerif
      End Select

      ' Draw the background of the item.
      e.DrawBackground()

      ' Create a square filled with the animals color. Vary the size
      ' of the rectangle based on the length of the animals name.
      Dim rectangle As Rectangle = New Rectangle(2, e.Bounds.Top + 2, _
          e.Bounds.Height, e.Bounds.Height - 4)
      e.Graphics.FillRectangle(New SolidBrush(animalColor), rectangle)

      ' Draw each string in the array, using a different size, color,
      ' and font for each item.
      myFont = New Font(family, size, FontStyle.Bold)
      e.Graphics.DrawString(animals(e.Index), myFont, System.Drawing.Brushes.Black, _
          New RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, _ 
          e.Bounds.Width, e.Bounds.Height))

      ' Draw the focus rectangle if the mouse hovers over an item.
      e.DrawFocusRectangle()
  End Sub
開發者ID:VB.NET開發者,項目名稱:System.Windows.Forms,代碼行數:87,代碼來源:ComboBox.DrawItem

示例2: Form1

' 導入命名空間
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.XButton = New System.Windows.Forms.Button
        Me.ColorList = New System.Windows.Forms.ComboBox
        Me.SuspendLayout()
        '
        'XButton
        '
        Me.XButton.Location = New System.Drawing.Point(112, 24)
        Me.XButton.Name = "XButton"
        Me.XButton.Size = New System.Drawing.Size(75, 23)
        Me.XButton.TabIndex = 0
        Me.XButton.Text = "Button1"
        Me.XButton.UseVisualStyleBackColor = True
        '
        'ColorList
        '
        Me.ColorList.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        Me.ColorList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ColorList.FormattingEnabled = True
        Me.ColorList.Location = New System.Drawing.Point(88, 64)
        Me.ColorList.Name = "ColorList"
        Me.ColorList.Size = New System.Drawing.Size(121, 21)
        Me.ColorList.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 221)
        Me.Controls.Add(Me.ColorList)
        Me.Controls.Add(Me.XButton)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Control Drawing"
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents XButton As System.Windows.Forms.Button
    Friend WithEvents ColorList As System.Windows.Forms.ComboBox

End Class
Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ColorList.Items.Add("Red")
        ColorList.Items.Add("Orange")
        ColorList.Items.Add("Yellow")
        ColorList.Items.Add("Green")
        ColorList.Items.Add("Blue")
        ColorList.Items.Add("Indigo")
        ColorList.Items.Add("Violet")
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawEllipse(Pens.Black, 10, 10, Me.ClientRectangle.Width - 20, _
            Me.ClientRectangle.Height - 20)
    End Sub

    Private Sub XButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles XButton.Paint
        Dim usePen As Pen

        e.Graphics.Clear(SystemColors.Control)

        usePen = New Pen(SystemColors.ControlText, 3)
        e.Graphics.DrawRectangle(usePen, XButton.ClientRectangle)

        usePen.Dispose()
    End Sub

    Private Sub ColorList_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ColorList.DrawItem
        Dim useBrush As Brush

        If (e.Index = -1) Then Return

        e.DrawBackground()

        useBrush = New SolidBrush(Color.FromName(CStr(ColorList.Items(e.Index))))
        e.Graphics.FillRectangle(useBrush, _
            e.Bounds.Left + 2, e.Bounds.Top + 2, _
            e.Bounds.Width - 4, e.Bounds.Height - 4)
        useBrush.Dispose()

        e.Graphics.DrawRectangle(Pens.Black, _
            e.Bounds.Left + 2, e.Bounds.Top + 2, _
            e.Bounds.Width - 4, e.Bounds.Height - 4)
        e.DrawFocusRectangle()
    End Sub

    Private Sub XButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XButton.Click
        MsgBox("Button clicked.")
    End Sub
End Class

public class ComboBoxCellRenderer
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
開發者ID:VB程序員,項目名稱:System.Windows.Forms,代碼行數:123,代碼來源:ComboBox.DrawItem


注:本文中的System.Windows.Forms.ComboBox.DrawItem事件示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。