• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

500 Points for quick answer

I have a Sub (shown below) that handles the TextChanged event for multiple textboxes. How do test the value of the textbox that triggers the textchagned event. I think I need to use the WithEvents somehow but don't know how. My overall goal is to bold the text if the value is greater than zero. (not sure how to programatically bold a textbox either.)


Private Sub txtAdministration_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtCaseRelated.TextChanged, txtAdministration.TextChanged, txtMarketing.TextChanged, txtPrecert.TextChanged,          txtBetterBaby.TextChanged, txtDiseaseManagement.TextChanged

        If sender.Text > 0 Then
            ' Bold the textbox
        End If

    End Sub
0
bobbailey22
Asked:
bobbailey22
  • 6
  • 3
  • 3
  • +1
2 Solutions
 
S-TwilleyCommented:
You almost have it:

Private Sub txtAdministration_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtCaseRelated.TextChanged, txtAdministration.TextChanged, txtMarketing.TextChanged, txtPrecert.TextChanged,          txtBetterBaby.TextChanged, txtDiseaseManagement.TextChanged

        Dim thisTextBox as Textbox = DirectCast(sender, Textbox)

        If thisTextBox.TextLength > 0 Then  'I assume you meant textlength
            ' Bold the textbox
        End If

    End Sub
0
 
S-TwilleyCommented:
oops, sorry, just reread your question...


Private Sub txtAdministration_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtCaseRelated.TextChanged, txtAdministration.TextChanged, txtMarketing.TextChanged, txtPrecert.TextChanged,          txtBetterBaby.TextChanged, txtDiseaseManagement.TextChanged

        Dim thisTextBox as Textbox = DirectCast(sender, Textbox)
        Dim thistext As String = thisTextBox.Text

        If IsNumeric(thisText) Then
            If Csng(thisText) > 0 Then
                   thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Bold)
            Else
        Else

        End If
   
    End Sub
0
 
S-TwilleyCommented:
grrr... sorry.

       If IsNumeric(thisText) Then
            If Csng(thisText) > 0 Then
                   thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Bold)
            Else
                   thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Regular)

            End If
        Else
                   thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Regular)
        End If

============

Figure it should be changed to regular if its not greater than 0 or not numeric
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Joe_GriffithCommented:
A little cleaner:

      If IsNumeric(thisText) AndAlso Csng(thisText) > 0 Then
           thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Bold)
      Else
           thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Regular)
      End If

0
 
S-TwilleyCommented:
Thanks Joe... I've not actually used the AndAlso operator before,  you're right about it making things neater
0
 
bobbailey22Author Commented:
Very nice guys. Great job!
Would either of you mind explaining how the withevents works?
0
 
S-TwilleyCommented:
When you're declaring an object that you want to capture events for... you use the WithEvents...   that's probably the simplest way I could put it
0
 
Corey ScheichDeveloperCommented:
Correct me if I am wrong but don't you want to evaluate both

IsNumeric(thisText)

and

Csng(thisText) > 0

as this is written Csng(thisText) > 0 would only be evaluated if the value of thisText was not numeric.

      If IsNumeric(thisText) AndAlso Csng(thisText) > 0 Then
           thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Bold)
      Else
           thisTextBox.Font = new Font(thisTextBox.Font, FontStyle.Regular)
      End If
0
 
bobbailey22Author Commented:
how is that any different than "Handles" doen't capture the events that it "Handles"?
0
 
Corey ScheichDeveloperCommented:
forget it I was backward and accidently posted. I don't know what I hit.
0
 
S-TwilleyCommented:
what i mean is... say you have a timer class..

Dim WithEvents tmrTimer as New Timers.Timer(500)  '   the WithEVents lets VS know that we plan to capture events for it somewhere


Sub Timed(sender as object, e as timers.ElapsedEventArgs) Handles tmrTimer.Elapsed   '  we planned to capture the event, and now we have
       ' some code
End Sub

=========================
0
 
Joe_GriffithCommented:
For Corey2: "as this is written Csng(thisText) > 0 would only be evaluated if the value of thisText was not numeric."

No, it's the other way around.  If the text is not numeric then the test for >0 is not performed.  I'm assuming here that if the user types "Foobar" then he doesn't want it to be bold.

If you just used "And" instead of "AndAlso" then it would throw an exception for a non-numeric input.
0
 
Corey ScheichDeveloperCommented:
While I was typing i realized that AndAlso only interupted if the first expression evaluated to false, where I was origional thinking it would continue if it evaluated to true.  So I was going to abandon my post but it was too late when clicking back into this window I somehow caused it to post unintentionally.  I hate it when that happens.

Corey2
0
 
bobbailey22Author Commented:
They you for the explination. You are way ahead of me but thats why your the expert!

Thanks again,

Bob
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now