How to Highlight a Number in a Textbox With Microsoft Visual Basic 2008

104 36
    • 1). Open Visual Basic 2008 and on the Start Page, click the "Project" link to the right of the "Create:" option.

    • 2). Change the default application name from "WindowsApplication1" to "HighlightTextbox," then double-click on the "Windows Forms Application" icon. This opens the Visual Basic Design window with a new, blank Windows form.

    • 3). Place the cursor on the "Toolbox" tab located at the top left of the Design window to open the Toolbox menu.

    • 4). Double-click the "TextBox" control and the "Button" control to add a TextBox and a Button to your form. Roll the cursor off the Toolbox and it will close automatically. Arrange the TextBox and the Button on the form for a pleasing appearance.

    • 5). Double-click on the "Button1" control to open the Code window. Visual Basic will place the cursor between the first and last lines of the button-click event handler. Copy and paste the following code so that the finished product looks like this:

      Public Class Form1

      Dim TextboxFlagHighlightBoolean As Boolean = False

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

      System.EventArgs) Handles Button1.Click

      If TextBox1.Text > "" Then

      If TextboxFlagHighlightBoolean Then

      TextBox1.ForeColor = Color.Black

      TextBox1.Font = New System.Drawing.Font("", 8,

      Drawing.FontStyle.Regular)

      TextboxFlagHighlightBoolean = False

      Else

      TextBox1.ForeColor = Color.Red

      TextBox1.Font = New System.Drawing.Font("", 8,

      Drawing.FontStyle.Bold)

      TextboxFlagHighlightBoolean = True

      End If

      Else

      MessageBox.Show("Please enter a number in the textbox")

      End If

      End Sub

      End Class

      This code declares a flag with the "Dim" statement that stores the state of the highlighting of the TextBox. If it is "False," clicking the button changes the text color of the TextBox to red and changes the font to bold. If the flag is "True" a button-click changes the text color back to black and the font back to regular.

    • 6). Click on the "Debug" menu item at the top of the screen to test your application. When it opens, enter a number in the TextBox and click "Button1." The background should change to red. Click it again and the background changes back to white.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.