Author Topic: BasicLabel and Substring  (Read 965 times)

GraemeTownsend

  • Newbie
  • *
  • Posts: 29
    • View Profile
BasicLabel and Substring
« on: October 21, 2015, 08:36:17 PM »
Hi All,

This is most likely a very basic question, but I'm struggling.
Using TextChanged event on a basicLabel I want to manipulate the returned data, (which is just a long number, 10 digits).
I want to get a substring as indicated below, but I cannot.
If I use Substring I get System.ArgumentOutOfRangeException
If I use Mid the label goes blank.

If I use a bogus string, (TestStr3 below), it works fine.


Code below, (I've ballooned it out with un-needed variables for clarity and fault finding). Any advice welcomed.
Cheers,
GT.



    Private Sub Time_TK01_TextChanged(sender As Object, e As EventArgs) Handles Time_TK01.TextChanged
        Dim TestStr1 As String = Time_TK01.Text.ToString()
        Dim TestStr2 As String = "1234567890"
        Dim TestStr3 As String

        TestStr3 = TestStr1.Substring(2, 2)
        'TestStr3 = Mid(TestStr1, 2, 2)

        Time_TK01.Text = TestStr3
    End Sub

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
Re: BasicLabel and Substring
« Reply #1 on: October 21, 2015, 08:55:54 PM »
This is what is happening. When you change the value and push it back into the same label, the event fires again. So let's say you have "1234567890". Your TextChanged fires and you take the substring(3,2), which will return "45". You now put that back into the same Label which causes your Text to change, then you get a TextChanged event, except now the Text is "45" and if you try to substring "45" starting at 3, you get an index error.

A better solution may be to add a DataSubcriber and a Label from the All Windows Forms group., set the PLCAddressValue property of the DataSubscriber, then double click the DataSubscriber to get to the ValueChanged event.

In this event handler try your substring and put the result in Label1.Text

GraemeTownsend

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: BasicLabel and Substring
« Reply #2 on: October 21, 2015, 09:39:28 PM »
Thanks. Worked perfect.