Link to home
Start Free TrialLog in
Avatar of bobbailey22
bobbailey22

asked on

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
Avatar of S-Twilley
S-Twilley

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
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
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Joe... I've not actually used the AndAlso operator before,  you're right about it making things neater
Avatar of bobbailey22

ASKER

Very nice guys. Great job!
Would either of you mind explaining how the withevents works?
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
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
how is that any different than "Handles" doen't capture the events that it "Handles"?
forget it I was backward and accidently posted. I don't know what I hit.
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

=========================
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.
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
They you for the explination. You are way ahead of me but thats why your the expert!

Thanks again,

Bob