Custom Control
-
I want to create a TextBox Control that will change the back colour when it gets focus and then reset the back colour when it leaves focus
-
I want to create a TextBox Control that will change the back colour when it gets focus and then reset the back colour when it leaves focus
Use the GotFocus() and LostFocus() methods of the TextBox control to change the background color. ... Private Sub TextBox1_GotFocus(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.GotFocus TextBox1.BackColor = System.Drawing.Color.Blue End Sub Private Sub TextBox1_LostFocus(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.LostFocus TextBox1.BackColor = System.Drawing.Color.White End Sub ...
-
Use the GotFocus() and LostFocus() methods of the TextBox control to change the background color. ... Private Sub TextBox1_GotFocus(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.GotFocus TextBox1.BackColor = System.Drawing.Color.Blue End Sub Private Sub TextBox1_LostFocus(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.LostFocus TextBox1.BackColor = System.Drawing.Color.White End Sub ...
If you want this functionality in a custom control here are the steps you need to follow: (from the documentation on Inheriting from Existing Windows Form Controls) 1) Create a Windows Executable or a Windows Control Library project. (in the end you'll want to use a Windows Control Library, but for my test I just created the control in a Windows Executable project) 2) From the project menu select Add Inherited Control. 3) Select Custom Control in the Add New Item dialog box. 4) Go into the code for your new control and modify it so that you are inheriting from the specific control (in our case TextBox). change
Public Class MyColorTextBox Inherits System.Windows.Form.Control
toPublic Class MyColorTextBox Inherits System.Windows.Form.Textbox
... Now put in your code for the GotFocus() and LostFocus() events...as we did before. For my test I then put a TextBox control on the form of my test application. Then I went into the system generated code and changed the declaration of the Textbox to a MyColorTextBox. ie. changeMe.TextBox1 = New Systems.Windows.Forms.TextBox
toMe.TextBox1 = New WindowsApplication1.ColorTextBox
Now what you really want to do is wrap your new control into a Windows Control Library so that you can just draw a MyColorTextBox on any form. But I'm not going to go into all of that here. Hopefully this is a good start for you.