Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Focus Apply Background Color:
- Steps:
- 1) Create a WinBase form that all other forms will inherit.
- 2) On the form to display, change the the disigner's code to inherit WinBase form.
- Example CustomerForm with input background changed - https://imgur.com/JiRvvZR
- Code for WinBase at: https://pastebin.com/BKh0VyBR
- WinBase.vb code behind:
- ''' <summary>
- ''' Provides standard functionality for all data entry forms
- ''' </summary>
- ''' <remarks>Handles the following tasks:
- ''' - Changing the color of the control when the user enters the control
- ''' </remarks>
- Public Class WinBase
- #Region " Form Events"
- Private Sub BaseWin_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
- Handles Me.Load
- ' Add the event handlers
- AddEventHandlers(Me)
- End Sub
- #End Region
- #Region " Private Methods"
- #Region " AddEventHandlers"
- ''' <summary>
- ''' Assigns the Enter and Leave event handlers for appropriate controls
- ''' </summary>
- ''' <param name="ctrlContainer">Control containing the controls</param>
- ''' <remarks>The first time this is called the form is normally passed in.
- ''' This method is then called recursively for any control that has child controls.</remarks>
- Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
- For Each ctrl As Control In ctrlContainer.Controls
- If TypeOf ctrl Is TextBox OrElse
- TypeOf ctrl Is ComboBox OrElse
- TypeOf ctrl Is MaskedTextBox Then
- AddHandler ctrl.Enter, AddressOf ProcessEnter
- AddHandler ctrl.Leave, AddressOf ProcessLeave
- End If
- ' If the control has children,
- ' recursively call this function
- If ctrl.HasChildren Then
- AddEventHandlers(ctrl)
- End If
- Next
- End Sub
- #End Region
- #Region " ProcessEnter"
- ''' <summary>
- ''' Sets the background color when the user enters the control
- ''' </summary>
- ''' <param name="sender">Control generating the event</param>
- ''' <param name="e">Event arguments</param>
- ''' <remarks></remarks>
- Private Sub ProcessEnter(ByVal sender As Object, ByVal e As System.EventArgs)
- ' Highlight backcolor to Bisque.
- DirectCast(sender, Control).BackColor = Color.Bisque
- End Sub
- #End Region
- #Region " ProcessLeave"
- ''' <summary>
- ''' Sets the background color back to the Windows color when
- ''' the user leaves the control
- ''' </summary>
- ''' <param name="sender"></param>
- ''' <param name="e"></param>
- ''' <remarks></remarks>
- Private Sub ProcessLeave(ByVal sender As Object, ByVal e As System.EventArgs)
- DirectCast(sender, Control).BackColor = Color.FromKnownColor(KnownColor.Window)
- End Sub
- #End Region
- #End Region
- End Class
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- CustomerForm.Designer.vb (partial) code:
- <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
- Partial Class CustomerForm
- 'Inherits System.Windows.Forms.Form
- Inherits WinBase '<---
- 'Form overrides dispose to clean up the component list.
- <System.Diagnostics.DebuggerNonUserCode()> _
- Protected Overrides Sub Dispose(ByVal disposing As Boolean)
- Try
- If disposing AndAlso components IsNot Nothing Then
- components.Dispose()
- End If
- Finally
- MyBase.Dispose(disposing)
- End Try
- End Sub
- '...
Advertisement
Add Comment
Please, Sign In to add comment