How to use function to change textbox color

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesnkk
    New Member
    • Nov 2006
    • 134

    How to use function to change textbox color

    Hi, I am new to visual basic 6, In a form I have many 20 textboxes such as

    If (texbox1.Text < 2.85) Or (textbox1.Text > 3.15) Then
    textbox1.Text = ""
    Else
    textbox1.BackCo lor = &H80000011

    end if

    If (texbox2.Text < 2.85) Or (textbox2.Text > 3.15) Then
    textbox2.Text = ""
    Else
    textbox2.BackCo lor = &H80000011

    end if

    Is there a way that i can use function to change the backcolor without repeating
    for the rest of the 20 textboxes. By the way please dont ask me to create
    control array for the 20 text boxes as due to some reasons.
  • CyberSoftHari
    Recognized Expert Contributor
    • Sep 2007
    • 488

    #2
    You can prepare your own method.
    • Create a method with textbox control as parameter.
    • Program what you done here inside that method.
    • Pass all the textbox control to that method.

    Comment

    • jamesnkk
      New Member
      • Nov 2006
      • 134

      #3
      Originally posted by CyberSoftHari
      You can prepare your own method.
      • Create a method with textbox control as parameter.
      • Program what you done here inside that method.
      • Pass all the textbox control to that method.
      I don;t really understand, Isn't the same as I still need to code for the 20 text boxes ?

      Comment

      • jamesd0142
        Contributor
        • Sep 2007
        • 471

        #4
        [code=vb]
        Private Sub changecolour(By Val NoOfBoxs As Integer)
        For i As Integer = 1 To NoOfBoxs
        If Me.Controls("te xtbox" & i).Text < "3" Then
        Me.Controls("te xtbox" & i).BackColor = Color.Red
        Else
        Me.Controls("te xtbox" & i).BackColor = Color.Green
        End If
        Next

        End Sub
        [/code]

        This is vb2005 code but im sure it will give you an idea of how it could be done...

        Depending on the number of textbox's you say are present, it checks the value and changes the backcolor accrodingly.

        James

        Comment

        • CyberSoftHari
          Recognized Expert Contributor
          • Sep 2007
          • 488

          #5
          [CODE=vb]Private sub ChangeColor(txt Control As Textbox)
          'Your Code here
          End Sub[/CODE]

          Then Call this method where ever you need.
          (You have to try)

          Comment

          • CyberSoftHari
            Recognized Expert Contributor
            • Sep 2007
            • 488

            #6
            Originally posted by jamesd0142
            [code=vb]
            Private Sub changecolour(By Val NoOfBoxs As Integer)
            For i As Integer = 1 To NoOfBoxs
            ............... ......
            ..............
            ...........
            [/code]
            This is not good practice, because there may be some other textboxes available!

            Comment

            • jamesd0142
              Contributor
              • Sep 2007
              • 471

              #7
              Originally posted by CyberSoftHari
              This is not good practice, because there may be some other textboxes available!
              simple solution to that is the ones you want the function to work on will have a different name, ie textbox1 textbox2 etc.

              and call all other textboxes by more usefull names ie txtlogin, txtpassword.

              its simply an example, im not saying its the best way of doin this...

              James

              Comment

              • jamesnkk
                New Member
                • Nov 2006
                • 134

                #8
                Originally posted by CyberSoftHari
                [CODE=vb]Private sub ChangeColor(txt Control As Textbox)
                'Your Code here
                End Sub[/CODE]

                Then Call this method where ever you need.
                (You have to try)
                Thank you so much it work !

                Comment

                • jamesnkk
                  New Member
                  • Nov 2006
                  • 134

                  #9
                  Originally posted by jamesd0142
                  [code=vb]
                  Private Sub changecolour(By Val NoOfBoxs As Integer)
                  For i As Integer = 1 To NoOfBoxs
                  If Me.Controls("te xtbox" & i).Text < "3" Then
                  Me.Controls("te xtbox" & i).BackColor = Color.Red
                  Else
                  Me.Controls("te xtbox" & i).BackColor = Color.Green
                  End If
                  Next

                  End Sub
                  [/code]

                  This is vb2005 code but im sure it will give you an idea of how it could be done...

                  Depending on the number of textbox's you say are present, it checks the value and changes the backcolor accrodingly.

                  James
                  Thank you so much for the beautiful coding, i will try it out

                  Comment

                  • smartchap
                    New Member
                    • Dec 2007
                    • 236

                    #10
                    Place your controls (like textboxes, labels, timer, picturebox, etc.) on the form.
                    The name of all the textboxes for which you want to change color must start with 'Text' or any other initials, i.e. must have starting letters same. Then use the code given below:

                    Private Sub Command1_Click( )
                    End
                    End Sub

                    Private Sub Form_Load()
                    Timer1.Enabled = True
                    End Sub

                    Private Sub Timer1_Timer()
                    For Each TextBox In Me
                    If Left$(TextBox.N ame, 4) = "Text" Then
                    If Val(TextBox.Tex t) < 2.85 Or Val(TextBox.Tex t) > 3.15 Then
                    TextBox.BackCol or = vbRed
                    Else
                    TextBox.BackCol or = vbYellow
                    End If
                    End If
                    Next
                    End Sub

                    In the code you can change TextBox.BackCol or = vbYellow with TextBox.Text = "" as you required.

                    Hope it will certainly solve your problem. If yes then give a post.

                    Comment

                    • jamesnkk
                      New Member
                      • Nov 2006
                      • 134

                      #11
                      Originally posted by smartchap
                      Place your controls (like textboxes, labels, timer, picturebox, etc.) on the form.
                      The name of all the textboxes for which you want to change color must start with 'Text' or any other initials, i.e. must have starting letters same. Then use the code given below:

                      Private Sub Command1_Click( )
                      End
                      End Sub

                      Private Sub Form_Load()
                      Timer1.Enabled = True
                      End Sub

                      Private Sub Timer1_Timer()
                      For Each TextBox In Me
                      If Left$(TextBox.N ame, 4) = "Text" Then
                      If Val(TextBox.Tex t) < 2.85 Or Val(TextBox.Tex t) > 3.15 Then
                      TextBox.BackCol or = vbRed
                      Else
                      TextBox.BackCol or = vbYellow
                      End If
                      End If
                      Next
                      End Sub

                      In the code you can change TextBox.BackCol or = vbYellow with TextBox.Text = "" as you required.

                      Hope it will certainly solve your problem. If yes then give a post.
                      Thanks You so much for that wonderful code

                      Comment

                      • smartchap
                        New Member
                        • Dec 2007
                        • 236

                        #12
                        Dear Jamesnkk

                        We are always here to help u out. With this code u can check any type of control on any form and will give u wonderful results.

                        Comment

                        • smartchap
                          New Member
                          • Dec 2007
                          • 236

                          #13
                          Also with my code you can check any number of textboxes. Even if u change the no. of textboxes (or other controls) it will take care of and no need to change the code.

                          Comment

                          Working...