home *** CD-ROM | disk | FTP | other *** search
- Public Class ListBoxForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
- Friend WithEvents lblRGB As System.Windows.Forms.Label
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- '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.
- Friend WithEvents btnShowColors As System.Windows.Forms.Button
- Friend WithEvents btnObjects As System.Windows.Forms.Button
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.lblRGB = New System.Windows.Forms.Label()
- Me.ListBox1 = New System.Windows.Forms.ListBox()
- Me.btnShowColors = New System.Windows.Forms.Button()
- Me.btnObjects = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'lblRGB
- '
- Me.lblRGB.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.lblRGB.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
- Me.lblRGB.Location = New System.Drawing.Point(136, 184)
- Me.lblRGB.Name = "lblRGB"
- Me.lblRGB.Size = New System.Drawing.Size(184, 24)
- Me.lblRGB.TabIndex = 2
- '
- 'ListBox1
- '
- Me.ListBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.ListBox1.ItemHeight = 18
- Me.ListBox1.Location = New System.Drawing.Point(136, 16)
- Me.ListBox1.Name = "ListBox1"
- Me.ListBox1.Size = New System.Drawing.Size(184, 166)
- Me.ListBox1.TabIndex = 0
- '
- 'btnShowColors
- '
- Me.btnShowColors.Location = New System.Drawing.Point(16, 64)
- Me.btnShowColors.Name = "btnShowColors"
- Me.btnShowColors.Size = New System.Drawing.Size(104, 40)
- Me.btnShowColors.TabIndex = 1
- Me.btnShowColors.Text = "Show Color List"
- '
- 'btnObjects
- '
- Me.btnObjects.Location = New System.Drawing.Point(16, 16)
- Me.btnObjects.Name = "btnObjects"
- Me.btnObjects.Size = New System.Drawing.Size(104, 40)
- Me.btnObjects.TabIndex = 3
- Me.btnObjects.Text = "Load list of Persons"
- '
- 'ListBoxForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(336, 213)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnObjects, Me.lblRGB, Me.btnShowColors, Me.ListBox1})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.MinimumSize = New System.Drawing.Size(344, 240)
- Me.Name = "ListBoxForm"
- Me.Text = "ListBoxForm"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' these routines show that you can load actual object references
- ' into a ListBox control, and the ToString property is implicitly
- ' invoked for each one
-
- Private Sub btnObjects_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnObjects.Click
- FillListboxWithPersons()
- End Sub
-
- Sub FillListboxWithPersons()
- ' we need this in case the button is clicked after showing
- ' a list of colors in the ListBox
- ListBox1.DrawMode = DrawMode.Normal
-
- ' comment out next line if you want to see that the ToString
- ' method is used by default to display an object in a ListBox
- ListBox1.ValueMember = "ReverseName"
-
- ListBox1.Items.Add(New Person("Joe", "Doe", 2))
- ListBox1.Items.Add(New Person("Robert", "Smith", 1))
- ListBox1.Items.Add(New Person("Ann", "Doe", 3))
- End Sub
-
- ' these routines demonstrate owner-drawn ListBox controls
-
- Private Sub btnShowColors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowColors.Click
- FillListBoxWithColors()
- ListBox1.ValueMember = "Id"
- End Sub
-
- ' prepare the ListBox to contain colors
-
- Private Sub FillListBoxWithColors()
- ' Make the listbox owner-draw.
- ListBox1.DrawMode = DrawMode.OwnerDrawFixed
- ListBox1.ItemHeight = 24
-
- ' Avoid flickering.
- ListBox1.BeginUpdate()
- ListBox1.Items.Clear()
-
- ' Create a list of all the properties in the Color class.
- Dim pi As Reflection.PropertyInfo
- For Each pi In GetType(Color).GetProperties(Reflection.BindingFlags.Static Or Reflection.BindingFlags.Public)
- ' Add the name of the property (that is, the color) to the ListBox.
- ListBox1.Items.Add(pi.Name)
- Next
- ' Now display the result.
- ListBox1.EndUpdate()
- End Sub
-
- ' this event handler is called for each element about to be drawn
-
- Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
- ' Get the rectangle to be drawn.
- Dim rect As Rectangle = e.Bounds
-
- ' If this is the selected item, draw the background.
- If (e.State And DrawItemState.Selected) Then
- ' Fill the rectable with the highlight system color.
- e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
- Else
- ' else, fill the rectangle with the Window system color.
- e.Graphics.FillRectangle(SystemBrushes.Window, rect)
- End If
-
- ' Get the color of the item to be drawn.
- Dim colorName As String = ListBox1.Items(e.Index)
- ' Build a brush of that color
- Dim b As New SolidBrush(Color.FromName(colorName))
-
- ' Shrink the rectangle by some pixels.
- rect.Inflate(-16, -2)
- ' Draw the rectangle interior.
- e.Graphics.FillRectangle(b, rect)
- ' Draw the rectable outline.
- e.Graphics.DrawRectangle(Pens.Black, rect)
-
- ' select an appropriate color for the brush
- Dim b2 As Brush
- If CInt(b.Color.R) + CInt(b.Color.G) + CInt(b.Color.B) > 128 * 3 Then
- b2 = Brushes.Black
- Else
- b2 = Brushes.White
- End If
-
- ' draw the name of the color using the default font.
- e.Graphics.DrawString(colorName, e.Font, b2, rect.X + 4, rect.Y + 2)
-
- ' destroy the custom brush
- ' (don't dispose b2, because it is a system brush)
- b.Dispose()
- End Sub
-
- ' this is necessary, otherwise items don't redraw correctly when the form resizes.
- Private Sub ListBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Resize
- ListBox1.Refresh()
- End Sub
-
- ' display the RGB value of the color under the mouse cursor
-
- Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
- ' don't do anything if we aren't showing colors
- If ListBox1.DrawMode = DrawMode.Normal Then Exit Sub
-
- ' get the index of the element under the mouse cursor.
- Dim index As Integer = ListBox1.IndexFromPoint(e.X, e.Y)
- ' exit if no valid entry.
- If index = ListBox.NoMatches Then Exit Sub
-
- ' get the corresponding color.
- Dim c As Color = Color.FromName(ListBox1.Items(index))
- ' Display the RGB components.
- lblRGB.Text = String.Format("R={0} G={1} B={2}", c.R, c.G, c.B)
- End Sub
-
- End Class
-
- ' a sample class used to demonstrate that you can load
- ' object references in a ListBox
-
- Class Person
- Public ID As Integer
- Public FirstName As String
- Public LastName As String
-
- Sub New(ByVal firstName As String, ByVal lastName As String, ByVal id As Integer)
- Me.FirstName = firstName
- Me.LastName = lastName
- Me.ID = id
- End Sub
-
- ' this is the value that is displayed by default
- Overrides Function ToString() As String
- Return FirstName & " " & LastName
- End Function
-
- ' this value can be shown
- ReadOnly Property ReverseName() As String
- Get
- Return LastName & ", " & FirstName
- End Get
- End Property
-
-
-
- End Class