JayBeeOH

FocusApplyBackgroundColor

Jan 26th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.52 KB | None | 0 0
  1. Focus Apply Background Color:
  2.  
  3. Steps:
  4. 1) Create a WinBase form that all other forms will inherit.
  5. 2) On the form to display, change the the disigner's code to inherit WinBase form.
  6.  
  7. Example CustomerForm with input background changed - https://imgur.com/JiRvvZR
  8. Code for WinBase at: https://pastebin.com/BKh0VyBR
  9.  
  10. WinBase.vb code behind:
  11.  
  12. ''' <summary>
  13. ''' Provides standard functionality for all data entry forms
  14. ''' </summary>
  15. ''' <remarks>Handles the following tasks:
  16. ''' - Changing the color of the control when the user enters the control
  17. ''' </remarks>
  18. Public Class WinBase
  19.  
  20. #Region " Form Events"
  21.  
  22.     Private Sub BaseWin_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
  23.             Handles Me.Load
  24.  
  25.         ' Add the event handlers
  26.         AddEventHandlers(Me)
  27.     End Sub
  28.  
  29. #End Region
  30.  
  31. #Region " Private Methods"
  32.  
  33. #Region " AddEventHandlers"
  34.  
  35.     ''' <summary>
  36.     ''' Assigns the Enter and Leave event handlers for appropriate controls
  37.     ''' </summary>
  38.     ''' <param name="ctrlContainer">Control containing the controls</param>
  39.     ''' <remarks>The first time this is called the form is normally passed in.
  40.     ''' This method is then called recursively for any control that has child controls.</remarks>
  41.     Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
  42.         For Each ctrl As Control In ctrlContainer.Controls
  43.             If TypeOf ctrl Is TextBox OrElse
  44.                 TypeOf ctrl Is ComboBox OrElse
  45.                 TypeOf ctrl Is MaskedTextBox Then
  46.                 AddHandler ctrl.Enter, AddressOf ProcessEnter
  47.                 AddHandler ctrl.Leave, AddressOf ProcessLeave
  48.             End If
  49.             ' If the control has children,
  50.             ' recursively call this function
  51.             If ctrl.HasChildren Then
  52.                 AddEventHandlers(ctrl)
  53.             End If
  54.         Next
  55.     End Sub
  56.  
  57. #End Region
  58.  
  59. #Region " ProcessEnter"
  60.  
  61.     ''' <summary>
  62.     ''' Sets the background color when the user enters the control
  63.     ''' </summary>
  64.     ''' <param name="sender">Control generating the event</param>
  65.     ''' <param name="e">Event arguments</param>
  66.     ''' <remarks></remarks>
  67.     Private Sub ProcessEnter(ByVal sender As Object, ByVal e As System.EventArgs)
  68.         ' Highlight backcolor to Bisque.
  69.         DirectCast(sender, Control).BackColor = Color.Bisque
  70.     End Sub
  71.  
  72. #End Region
  73.  
  74. #Region " ProcessLeave"
  75.  
  76.     ''' <summary>
  77.     ''' Sets the background color back to the Windows color when
  78.     ''' the user leaves the control
  79.     ''' </summary>
  80.     ''' <param name="sender"></param>
  81.     ''' <param name="e"></param>
  82.     ''' <remarks></remarks>
  83.     Private Sub ProcessLeave(ByVal sender As Object, ByVal e As System.EventArgs)
  84.         DirectCast(sender, Control).BackColor = Color.FromKnownColor(KnownColor.Window)
  85.     End Sub
  86.  
  87. #End Region
  88.  
  89. #End Region
  90.  
  91. End Class
  92.  
  93. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  94. CustomerForm.Designer.vb (partial) code:
  95.  
  96. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  97. Partial Class CustomerForm
  98.     'Inherits System.Windows.Forms.Form
  99.     Inherits WinBase '<---
  100.  
  101.     'Form overrides dispose to clean up the component list.
  102.     <System.Diagnostics.DebuggerNonUserCode()> _
  103.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  104.         Try
  105.             If disposing AndAlso components IsNot Nothing Then
  106.                 components.Dispose()
  107.             End If
  108.         Finally
  109.             MyBase.Dispose(disposing)
  110.         End Try
  111.     End Sub
  112. '...
Advertisement
Add Comment
Please, Sign In to add comment