home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic .NET - Read Less - Learn More / Visual_Basic.NET_Read_Less_Learn_More_Richard_Bowman_Visual_2002.iso / Resources / Code / Ch12-CreateAControlFromScratch / UserControl1.vb < prev   
Text File  |  2001-10-02  |  3KB  |  77 lines

  1. Public Class UserControl1
  2.     Inherits System.Windows.Forms.UserControl
  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.     'UserControl1 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.  
  26.     'Required by the Windows Form Designer
  27.     Private components As System.ComponentModel.Container
  28.  
  29.     'NOTE: The following procedure is required by the Windows Form Designer
  30.     'It can be modified using the Windows Form Designer.  
  31.     'Do not modify it using the code editor.
  32.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  33.         '
  34.         'UserControl1
  35.         '
  36.         Me.Name = "UserControl1"
  37.  
  38.     End Sub
  39.  
  40. #End Region
  41.  
  42.     Private pressed As Boolean
  43.     Private dispText As String = "Standard text"
  44.  
  45.     Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  46.  
  47.     End Sub
  48.  
  49.     Private Sub UserControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
  50.         pressed = True
  51.         Me.Invalidate()
  52.     End Sub
  53.  
  54.     Private Sub UserControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
  55.         pressed = False
  56.         Me.Invalidate()
  57.     End Sub
  58.  
  59.     Public Property DisplayText() As String
  60.         Get
  61.             Return dispText
  62.         End Get
  63.         Set(ByVal Value As String)
  64.             dispText = Value
  65.             Me.Invalidate()
  66.         End Set
  67.     End Property
  68.     Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  69.         Dim f As New Font("Arial", 20, FontStyle.Bold), c As Color
  70.         Dim p As SizeF = e.Graphics.MeasureString(dispText, f)
  71.         Dim x As Integer = e.ClipRectangle.Width / 2 - p.Width / 2
  72.         Dim y As Integer = e.ClipRectangle.Height / 2 - p.Height / 2
  73.         If pressed Then c = Color.Red Else c = ForeColor
  74.         e.Graphics.DrawString(dispText, f, New SolidBrush(c), x, y)
  75.     End Sub
  76. End Class
  77.