Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

One Two Three

In visual basic, design the interface shown below. When one of the three text boxes receives the focus, its text becomes red. When it loses the focus, the text returns to black. The buttons set the alignment in the text boxes to left or right.

Note: Rely on Intellisense to provide you with the proper settings for the TextAlign property.

One Two Three

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
frm123 One, Two, Three Form Holds Controls
txtOne One Text Box Red text when active, black when not.
txtTwo Two Text Box Red text when active, black when not.
txtThree Three Text Box Red text when active, black when not.
btnLeft Left Text Box Makes text left justified.
btnRight Right Text Box Makes text right justified.

Write the Code:

' Project: One, Two, Three
' Description: Creates three text boxes and two buttons, Left and Right. Clicking the right
' button moves the text alignment to the right. Clicking the left button moves the text
' alignment to the left. Clicking on a text box causes the text to turn red. Text returns to
' black when the focus changes to another box.

Public Class frmOneTwoThree
   Private Sub txtOne_GotFocus(sender As Object, e As EventArgs) Handles txtOne.GotFocus
      txtOne.ForeColor = Color.Red
      txtOne.BackColor = Color.White
   End Sub
   Private Sub txtTwo_GotFocus(sender As Object, e As EventArgs) Handles txtTwo.GotFocus
      txtTwo.ForeColor = Color.Red
      txtTwo.BackColor = Color.White
   End Sub
   Private Sub txtThree_GotFocus(sender As Object, e As EventArgs) Handles txtThree.GotFocus
      txtThree.ForeColor = Color.Red
      txtThree.BackColor = Color.White
   End Sub
   Private Sub btnRight_Click(sender As Object, e As EventArgs) Handles btnRight.Click
      ' Moves text in the 3 text boxes to the right
      txtOne.TextAlign = HorizontalAlignment.Right
      txtTwo.TextAlign = HorizontalAlignment.Right
      txtThree.TextAlign = HorizontalAlignment.Right
   End Sub
   Private Sub btnLeft_Click(sender As Object, e As EventArgs) Handles btnLeft.Click
      ' Moves text in the 3 text boxes to the left
      txtOne.TextAlign = HorizontalAlignment.Left
      txtTwo.TextAlign = HorizontalAlignment.Left
      txtThree.TextAlign = HorizontalAlignment.Left
   End Sub
   Private Sub txtOne_Leave(sender As Object, e As EventArgs) Handles txtOne.Leave
      txtOne.ForeColor = Color.Black
   End Sub
   Private Sub txtTwo_Leave(sender As Object, e As EventArgs) Handles txtTwo.Leave
      txtTwo.ForeColor = Color.Black
   End Sub
   Private Sub txtThree_Leave(sender As Object, e As EventArgs) Handles txtThree.Leave
      txtThree.ForeColor = Color.Black
   End Sub
End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2025 · Metro Pro Theme on Genesis Framework · WordPress · Log in