Results 1 to 23 of 23

Thread: [RESOLVED] How to separate content of a TextBox?!

  1. #1

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Resolved [RESOLVED] How to separate content of a TextBox?!

    Howdy guys ...

    How can I separate content of a textbox ?! For e.g. I have a textbox which contains both alphabetical and numeric text in it , something like this :

    TextBox.Text="vbfourms2014"

    I want to learn how to separate any part of the text. For e.g. I want to separate vb and forums and 2014.

    Thanks in advance

  2. #2
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    397

    Re: How to separate content of a TextBox?!

    Using the '&' operator ? TextBox.Text="vb " & "fourms " & "2014"

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to separate content of a TextBox?!

    Doesn't want to know how to CREATE a textbox with "vbfourms2014" (sic)
    But rather if he HAS that, he wants to separate into (three) sections.
    Glowing....what you are asking is not what you are asking. :-)
    if the ONLY thing in textbox.text is "vbfourms2014", then you can simply say:
    Code:
    Dim firstSection as string
    Dim secSection as string
    Dim thirdSection as string
    firstSection = Mid(textbox.text,1,2)
    secSection = Mid(textbox.text,3,6)
    thirdSection = Mid(textbox.text,7,4)
    but what I suspect is that you want to split SOME text in the textbox. YOu are going to have to be a LOT more specific what you want separated. How would the computer know if you want the first 2 characters (like 'vb'), or maybe the first 3 characters (like 'vb6' in a string of 'vb6forums2014').
    See what I mean....?

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to separate content of a TextBox?!

    Check in the codebase M2000 interpreter. It is a list processing monster. You can found functions: islabel isnumber isstring and more advance isexp isstrexp for aritmetic, logic and string operations. Code is free and open.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to separate content of a TextBox?!

    Well the first thing you need to do is come up with a rule as to how it should be split up.

    Your example has no separators and you want to not only split out the number but also part of the text.
    This would be simple if you always want the first 2 characters, the last 4 and those in between but if this is not the case then you must do something different.
    So again first step is to determine the rule that must be used to split the data only then can you come up with code that will work on different strings.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to separate content of a TextBox?!

    Btwis. :-)

  7. #7
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to separate content of a TextBox?!

    GlowingVB,
    Suppose you know that "forums" is inside the string to examine. An Instr function give you the position 0 if "forums" not exist or a positive number for position. So if a rule says that if a forums exist in string then split that string to a string before and to another string after "forums". Can you make the code for that problem?

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: How to separate content of a TextBox?!

    Quote Originally Posted by SamOscarBrown View Post
    but what I suspect is that you want to split SOME text in the textbox. YOu are going to have to be a LOT more specific what you want separated. How would the computer know if you want the first 2 characters (like 'vb'), or maybe the first 3 characters (like 'vb6' in a string of 'vb6forums2014').
    See what I mean....?
    How about something such as

    Code:
    Dim firstSection As String
    Dim secSection As String
    Dim thirdSection As String
    Dim pos As Integer
    pos = Len(TextBox.Text)
    firstSection = Mid(TextBox.Text, 1, pos)
    pos = Len(TextBox.Text)
    secSection = Mid(TextBox.Text, pos + 1, pos)
    pos = Len(TextBox.Text)
    thirdSection = Mid(TextBox.Text, pos + 1, pos)
    Text2.Text = firstSection & " " & secSection & " " & thirdSection
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How to separate content of a TextBox?!

    Thanks for your answers ...

    It's better to say that the content of textbox might be anything! For example , You have strange code like : adfjkq2389rfjaiosdjg883 !!!!

    Now you want to use any part of the code for different purposes (for e.g.) .

    I want the program to get , for example , letter k to number 8 (Like kq2389rfjaiosdjg8) and separate it from other parts of content.

  10. #10

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How to separate content of a TextBox?!

    Quote Originally Posted by SamOscarBrown View Post
    Doesn't want to know how to CREATE a textbox with "vbfourms2014" (sic)
    But rather if he HAS that, he wants to separate into (three) sections.
    Glowing....what you are asking is not what you are asking. :-)
    if the ONLY thing in textbox.text is "vbfourms2014", then you can simply say:
    Code:
    Dim firstSection as string
    Dim secSection as string
    Dim thirdSection as string
    firstSection = Mid(textbox.text,1,2)
    secSection = Mid(textbox.text,3,6)
    thirdSection = Mid(textbox.text,7,4)
    but what I suspect is that you want to split SOME text in the textbox. YOu are going to have to be a LOT more specific what you want separated. How would the computer know if you want the first 2 characters (like 'vb'), or maybe the first 3 characters (like 'vb6' in a string of 'vb6forums2014').
    See what I mean....?
    This seems to work ... . I try it ASAP

  11. #11
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to separate content of a TextBox?!

    Think about a function that you put a string and return a pattern string like "NSSN" number string string number. This function give the idea of what your input string is and then you can do what you want.

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: How to separate content of a TextBox?!

    One could tackle such things with a generic Split-Function, which separates the
    numeric parts and the TextParts.

    The Form-Code below shows a simple approach (which currently will parse out only Integer-snippets,
    but could be enhanced, to understand also Fractional numbers using the same principle...


    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim Part
      For Each Part In NumericSplit("abc123xyz456???")
        Debug.Print Part, TypeName(Part)
      Next
    End Sub
    
    Function NumericSplit(S As String) 'state-machine like parsing of numeric and txt-parts
    Dim i As Long, j As Long, B() As Byte, WC As Long, IsNum As Boolean, IsTxt As Boolean, V()
      B = S
      For i = 0 To UBound(B) Step 2
        WC = B(i) + B(i + 1) * 256
        Select Case WC
          Case 48 To 57: If Not IsNum Then IsNum = True: IsTxt = False: ReDim Preserve V(j): j = j + 1
          Case Else:     If Not IsTxt Then IsTxt = True: IsNum = False: ReDim Preserve V(j): j = j + 1
        End Select
        If IsNum Then V(j - 1) = V(j - 1) * 10 + WC - 48 Else V(j - 1) = V(j - 1) & ChrW$(WC)
      Next i
      NumericSplit = V
    End Function
    Olaf

  13. #13
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to separate content of a TextBox?!

    Nice. But a pattern export first is more usefull. So maybe you can alter your function to give actual data or qualification pattern.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: How to separate content of a TextBox?!

    Quote Originally Posted by georgekar View Post
    Nice. But a pattern export first is more usefull. So maybe you can alter your function to give actual data or qualification pattern.
    As it is currently, one wouldn't really need to think about giving any patterns (or their lenght),
    since the numeric and string-parts will be parsed out with either length in alternating order
    (wichever part-type - numeric or text comes first).

    Relatively easy from there, to write ones own "special pattern-match-routines" directly -
    since one can test, which part comes first in the returned Variant-Array, by simply using
    the VarType Function.

    A simple generic pattern-match function is easily possible - without changing the
    NumericSplit-Function (which can remain "as is", usable on its own).

    Example into a Form:

    Code:
    Option Explicit
     
    Private Sub Form_Load()
    Dim Parts(): Parts = NumericSplit("abc123xyz")
    
      'this will not work, because we have only 3 parts, and the pattern wants 4
      If CheckPatternMatchFor(Parts, "TNTN") Then EnumerateParts Parts
      
      'this will not work, because although the pattern has 3 parts, the order is wrong
      If CheckPatternMatchFor(Parts, "NTN") Then EnumerateParts Parts
      
      'this will work, since the pattern is TNT and matches with "abc123xyz"
      If CheckPatternMatchFor(Parts, "TNT") Then EnumerateParts Parts
    End Sub
     
    Sub EnumerateParts(Parts())
    Dim i As Long
      For i = 0 To UBound(Parts): Debug.Print Parts(i), TypeName(Parts(i)): Next
    End Sub
    
    Function CheckPatternMatchFor(Parts(), Optional Pattern As String = "TNT") As Boolean
      If Len(Pattern) = UBound(Parts) + 1 And _
        (VarType(Parts(0)) = vbString And Left$(UCase$(Pattern), 1) = "T") Or _
        (VarType(Parts(0)) <> vbString And Left$(UCase$(Pattern), 1) = "N") Then
        CheckPatternMatchFor = True
      Else
        Debug.Print "The " & UBound(Parts) + 1 & " parts " & Join(Parts, "|") & " don't match the " & Pattern & "-Pattern"
      End If
    End Function
     
    Function NumericSplit(S As String)
    Dim i As Long, j As Long, B() As Byte, WC As Long, IsNum As Boolean, IsTxt As Boolean, V()
      B = S
      For i = 0 To UBound(B) Step 2
        WC = B(i) + B(i + 1) * 256
        Select Case WC
          Case 48 To 57: If Not IsNum Then IsNum = True: IsTxt = False: ReDim Preserve V(j): j = j + 1
          Case Else:     If Not IsTxt Then IsTxt = True: IsNum = False: ReDim Preserve V(j): j = j + 1
        End Select
        If IsNum Then V(j - 1) = V(j - 1) * 10 + WC - 48 Else V(j - 1) = V(j - 1) & ChrW$(WC)
      Next i
      NumericSplit = V
    End Function

    Olaf

  15. #15
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to separate content of a TextBox?!

    Numbers are not only digits. We have sign so a space maybe is a plus sign, and we have decimal point, and in some cases we have e with sign and exponetial value. So you can have 5 numbers one by one and you can parse them. The idea of numbers only with digits displayed, maybe is true for production codes or marks. And for that situation they hold specific position and width.
    In a more universal approach in an textbox someone can iinput values with quality mark..we say 1200km, or we can have an action before: after 2 hr
    For such complex inputs you can use a split function or better a function that parse without needed spaces or some delimiters, because each entity has specific rule to identify.

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to separate content of a TextBox?!

    'Probably' should just wait until OP comes back to the Thread. Doubt he/she is very well versed in VB6, and a lot of these discussions are more than likely way over OP's head. (Just guessing.) (Yesm, many ways to split text.....but first really need to know WHAT text, and how it is desired to be split.

  17. #17
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to separate content of a TextBox?!

    Quote Originally Posted by GlowingVB View Post
    Thanks for your answers ...

    It's better to say that the content of textbox might be anything! For example , You have strange code like : adfjkq2389rfjaiosdjg883 !!!!

    Now you want to use any part of the code for different purposes (for e.g.) .

    I want the program to get , for example , letter k to number 8 (Like kq2389rfjaiosdjg8) and separate it from other parts of content.
    This case is completely different from the first case as here you are not separating the numbers from the text

    So once again if you want to parse out a string you need some kind of rule that you will go by. It is possible to parse out a string in many different ways but if you want some code that can parse out different strings then there has to be a rule that will be used. What worked on the first example will not work on the second and what works on the second will not work on the first.

    Take some time to think about what it is you really want to do, what the content of the different strings may be and how you want them parsed and only then can you come up with suitable code to do what you want to do.

  18. #18

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: How to separate content of a TextBox?!

    Common guys! To tell you frankly , you are making a mountain out of a molehill! What i asked is so simple to understand , but i don't know why you are putting your finger on the format of content to see whether it is number or ... ! What "SamOscarBrown" suggested works well without any dependency to the content format.

    Anyway , Thanks a million for sharing ideas. See you soon ...

  19. #19
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] How to separate content of a TextBox?!

    Yes the method that Sam showed will work just fine to parse characters from those specific positions
    It will not work on your second example though unless you change the numbers in the code.

    So to make it work on strings that are not of the same length and you are not pulling from the same place it will not do it.

    In any case as I said you have to have some rule in place be it the first 3 characters, the next 4, the last 5, up to a comma or a digit or whatever the case may be.

    As for what you asked being simple to understand to you it may have been but it really is not as clear as you might think and the comment about needing a rule to parse is true in any case

  20. #20
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: [RESOLVED] How to separate content of a TextBox?!

    GlowingVB,
    A proper programming method to apply someone must have three parts: knowing what is valid, expected the exceptions, and finally implement a feedback in code. The new programmer do all of this things without a single line of code. Because in his thought, all data he provide are valid, an error from program consider it as a bug or fault of the language or something but no his mistake or misunderstood, and if the output is what was expected to be, then that feedback is o.k. for him.
    Here the objective is only to find someone the asking method. But in my opinion that method must have a validation routine. There is no code to expect anything, even a language which can run several thousands different programs for input has a validation section of what can load and what not.
    Simple thoughts....create complicated programs;

  21. #21

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: [RESOLVED] How to separate content of a TextBox?!

    Quote Originally Posted by georgekar View Post
    GlowingVB,
    A proper programming method to apply someone must have three parts: knowing what is valid, expected the exceptions, and finally implement a feedback in code. The new programmer do all of this things without a single line of code. Because in his thought, all data he provide are valid, an error from program consider it as a bug or fault of the language or something but no his mistake or misunderstood, and if the output is what was expected to be, then that feedback is o.k. for him.
    Here the objective is only to find someone the asking method. But in my opinion that method must have a validation routine. There is no code to expect anything, even a language which can run several thousands different programs for input has a validation section of what can load and what not.
    Simple thoughts....create complicated programs;
    I do agree with this sentence : "Simple thoughts....create complicated programs" , but sometimes humans ditch themselves and choose the hardest way to get an simple answer! I know that if someone wants to ask a question and get a reasonable answer , he should provide all the necessary info and consider all the aspects , but how could i ask this question in apparently understandable way ?!

    After all , I'm not expert at programming ; not a great human either! So I need to be all ear to some guys like you to learn more ...

    Yours sincerely

  22. #22
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: [RESOLVED] How to separate content of a TextBox?!

    Well, your first post only gives ONE example of a text you wanted to split. If that is the ONLY text you wanted to divide into three parts, you said it well enough, except adding that it IS the onlly text you want to split.

    But as you continue to learn to code, you will see there are MANY ways to divide up strings, and in doing so, a logical thought process must be used. You should be anticipating that a user of your application could type in ANYTHING and then could want to split it ANY way.

    So, a better approach could have been:

    "I have a text box that contains twelve characters. I want to split it into three parts: part 1 is the first two characters, part 2 is the next 6 characters, and part 3 is the last four characters. (I have the textbox set to a limit of 12, and I have code that won't let any splitting be done until there is 12 characters entered.). How do I do this?"
    That way, you would not have gotten all those responses about a VERY GOOD function, the Split() function. You would probable have only gotten what I posted.

    Don't worry, you will get better if you keep studying and visiting this forum.

  23. #23

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: [RESOLVED] How to separate content of a TextBox?!

    Quote Originally Posted by SamOscarBrown View Post
    Well, your first post only gives ONE example of a text you wanted to split. If that is the ONLY text you wanted to divide into three parts, you said it well enough, except adding that it IS the onlly text you want to split.

    But as you continue to learn to code, you will see there are MANY ways to divide up strings, and in doing so, a logical thought process must be used. You should be anticipating that a user of your application could type in ANYTHING and then could want to split it ANY way.

    So, a better approach could have been:



    That way, you would not have gotten all those responses about a VERY GOOD function, the Split() function. You would probable have only gotten what I posted.

    Don't worry, you will get better if you keep studying and visiting this forum.
    Of course I can get better with others' guidance. I know there are many ways to do the same thing , but there is no any need to know all the ways ! Everybody could suggest what you generally did to me as a really good way.If I want to go to details , then i can not expand and use my acquired knowledge in other cases. Maybe ones i have a textbox not with limitation of 12 (for e.g.) or ... . So I'm sure that a person with non-special logic agree that learning something's rule basically is better than learning an exception of it.
    Have Fun!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width