home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / controlsdemo / listboxform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  9.0 KB  |  240 lines

  1. Public Class ListBoxForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
  26.     Friend WithEvents lblRGB As System.Windows.Forms.Label
  27.  
  28.     'Required by the Windows Form Designer
  29.     Private components As System.ComponentModel.Container
  30.  
  31.     'NOTE: The following procedure is required by the Windows Form Designer
  32.     'It can be modified using the Windows Form Designer.  
  33.     'Do not modify it using the code editor.
  34.     Friend WithEvents btnShowColors As System.Windows.Forms.Button
  35.     Friend WithEvents btnObjects As System.Windows.Forms.Button
  36.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  37.         Me.lblRGB = New System.Windows.Forms.Label()
  38.         Me.ListBox1 = New System.Windows.Forms.ListBox()
  39.         Me.btnShowColors = New System.Windows.Forms.Button()
  40.         Me.btnObjects = New System.Windows.Forms.Button()
  41.         Me.SuspendLayout()
  42.         '
  43.         'lblRGB
  44.         '
  45.         Me.lblRGB.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
  46.                     Or System.Windows.Forms.AnchorStyles.Right)
  47.         Me.lblRGB.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  48.         Me.lblRGB.Location = New System.Drawing.Point(136, 184)
  49.         Me.lblRGB.Name = "lblRGB"
  50.         Me.lblRGB.Size = New System.Drawing.Size(184, 24)
  51.         Me.lblRGB.TabIndex = 2
  52.         '
  53.         'ListBox1
  54.         '
  55.         Me.ListBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  56.                     Or System.Windows.Forms.AnchorStyles.Left) _
  57.                     Or System.Windows.Forms.AnchorStyles.Right)
  58.         Me.ListBox1.ItemHeight = 18
  59.         Me.ListBox1.Location = New System.Drawing.Point(136, 16)
  60.         Me.ListBox1.Name = "ListBox1"
  61.         Me.ListBox1.Size = New System.Drawing.Size(184, 166)
  62.         Me.ListBox1.TabIndex = 0
  63.         '
  64.         'btnShowColors
  65.         '
  66.         Me.btnShowColors.Location = New System.Drawing.Point(16, 64)
  67.         Me.btnShowColors.Name = "btnShowColors"
  68.         Me.btnShowColors.Size = New System.Drawing.Size(104, 40)
  69.         Me.btnShowColors.TabIndex = 1
  70.         Me.btnShowColors.Text = "Show Color List"
  71.         '
  72.         'btnObjects
  73.         '
  74.         Me.btnObjects.Location = New System.Drawing.Point(16, 16)
  75.         Me.btnObjects.Name = "btnObjects"
  76.         Me.btnObjects.Size = New System.Drawing.Size(104, 40)
  77.         Me.btnObjects.TabIndex = 3
  78.         Me.btnObjects.Text = "Load list of Persons"
  79.         '
  80.         'ListBoxForm
  81.         '
  82.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  83.         Me.ClientSize = New System.Drawing.Size(336, 213)
  84.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnObjects, Me.lblRGB, Me.btnShowColors, Me.ListBox1})
  85.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  86.         Me.MinimumSize = New System.Drawing.Size(344, 240)
  87.         Me.Name = "ListBoxForm"
  88.         Me.Text = "ListBoxForm"
  89.         Me.ResumeLayout(False)
  90.  
  91.     End Sub
  92.  
  93. #End Region
  94.  
  95.     ' these routines show that you can load actual object references
  96.     ' into a ListBox control, and the ToString property is implicitly
  97.     ' invoked for each one
  98.  
  99.     Private Sub btnObjects_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnObjects.Click
  100.         FillListboxWithPersons()
  101.     End Sub
  102.  
  103.     Sub FillListboxWithPersons()
  104.         ' we need this in case the button is clicked after showing
  105.         ' a list of colors in the ListBox
  106.         ListBox1.DrawMode = DrawMode.Normal
  107.  
  108.         ' comment out next line if you want to see that the ToString
  109.         ' method is used by default to display an object in a ListBox
  110.         ListBox1.ValueMember = "ReverseName"
  111.  
  112.         ListBox1.Items.Add(New Person("Joe", "Doe", 2))
  113.         ListBox1.Items.Add(New Person("Robert", "Smith", 1))
  114.         ListBox1.Items.Add(New Person("Ann", "Doe", 3))
  115.     End Sub
  116.  
  117.     ' these routines demonstrate owner-drawn ListBox controls
  118.  
  119.     Private Sub btnShowColors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowColors.Click
  120.         FillListBoxWithColors()
  121.         ListBox1.ValueMember = "Id"
  122.     End Sub
  123.  
  124.     ' prepare the ListBox to contain colors
  125.  
  126.     Private Sub FillListBoxWithColors()
  127.         ' Make the listbox owner-draw.
  128.         ListBox1.DrawMode = DrawMode.OwnerDrawFixed
  129.         ListBox1.ItemHeight = 24
  130.  
  131.         ' Avoid flickering.
  132.         ListBox1.BeginUpdate()
  133.         ListBox1.Items.Clear()
  134.  
  135.         ' Create a list of all the properties in the Color class.
  136.         Dim pi As Reflection.PropertyInfo
  137.         For Each pi In GetType(Color).GetProperties(Reflection.BindingFlags.Static Or Reflection.BindingFlags.Public)
  138.             ' Add the name of the property (that is, the color) to the ListBox.
  139.             ListBox1.Items.Add(pi.Name)
  140.         Next
  141.         ' Now display the result.
  142.         ListBox1.EndUpdate()
  143.     End Sub
  144.  
  145.     ' this event handler is called for each element about to be drawn
  146.  
  147.     Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
  148.         ' Get the rectangle to be drawn.
  149.         Dim rect As Rectangle = e.Bounds
  150.  
  151.         ' If this is the selected item, draw the background.
  152.         If (e.State And DrawItemState.Selected) Then
  153.             ' Fill the rectable with the highlight system color.
  154.             e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
  155.         Else
  156.             ' else, fill the rectangle with the Window system color.
  157.             e.Graphics.FillRectangle(SystemBrushes.Window, rect)
  158.         End If
  159.  
  160.         ' Get the color of the item to be drawn.
  161.         Dim colorName As String = ListBox1.Items(e.Index)
  162.         ' Build a brush of that color
  163.         Dim b As New SolidBrush(Color.FromName(colorName))
  164.  
  165.         ' Shrink the rectangle by some pixels.
  166.         rect.Inflate(-16, -2)
  167.         ' Draw the rectangle interior.
  168.         e.Graphics.FillRectangle(b, rect)
  169.         ' Draw the rectable outline.
  170.         e.Graphics.DrawRectangle(Pens.Black, rect)
  171.  
  172.         ' select an appropriate color for the brush
  173.         Dim b2 As Brush
  174.         If CInt(b.Color.R) + CInt(b.Color.G) + CInt(b.Color.B) > 128 * 3 Then
  175.             b2 = Brushes.Black
  176.         Else
  177.             b2 = Brushes.White
  178.         End If
  179.  
  180.         ' draw the name of the color using the default font.
  181.         e.Graphics.DrawString(colorName, e.Font, b2, rect.X + 4, rect.Y + 2)
  182.  
  183.         ' destroy the custom brush
  184.         ' (don't dispose b2, because it is a system brush)
  185.         b.Dispose()
  186.     End Sub
  187.  
  188.     ' this is necessary, otherwise items don't redraw correctly when the form resizes.
  189.     Private Sub ListBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Resize
  190.         ListBox1.Refresh()
  191.     End Sub
  192.  
  193.     ' display the RGB value of the color under the mouse cursor
  194.  
  195.     Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
  196.         ' don't do anything if we aren't showing colors
  197.         If ListBox1.DrawMode = DrawMode.Normal Then Exit Sub
  198.  
  199.         ' get the index of the element under the mouse cursor.
  200.         Dim index As Integer = ListBox1.IndexFromPoint(e.X, e.Y)
  201.         ' exit if no valid entry.
  202.         If index = ListBox.NoMatches Then Exit Sub
  203.  
  204.         ' get the corresponding color.
  205.         Dim c As Color = Color.FromName(ListBox1.Items(index))
  206.         ' Display the RGB components.
  207.         lblRGB.Text = String.Format("R={0}  G={1}  B={2}", c.R, c.G, c.B)
  208.     End Sub
  209.  
  210. End Class
  211.  
  212. ' a sample class used to demonstrate that you can load
  213. ' object references in a ListBox
  214.  
  215. Class Person
  216.     Public ID As Integer
  217.     Public FirstName As String
  218.     Public LastName As String
  219.  
  220.     Sub New(ByVal firstName As String, ByVal lastName As String, ByVal id As Integer)
  221.         Me.FirstName = firstName
  222.         Me.LastName = lastName
  223.         Me.ID = id
  224.     End Sub
  225.  
  226.     ' this is the value that is displayed by default
  227.     Overrides Function ToString() As String
  228.         Return FirstName & " " & LastName
  229.     End Function
  230.  
  231.     ' this value can be shown 
  232.     ReadOnly Property ReverseName() As String
  233.         Get
  234.             Return LastName & ", " & FirstName
  235.         End Get
  236.     End Property
  237.  
  238.  
  239.  
  240. End Class