Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VB6 - Wildcards?  (Read 7817 times)

0 Members and 1 Guest are viewing this topic.

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
VB6 - Wildcards?
« on: August 21, 2010, 12:35:39 PM »
What I'm trying to do is change the colour of keywords in a rich text box. I found a nice script on the internet which does just that, it finds any words passed to its sub and changes the colour to what was also passed to the sub. The problem is, I need to be able to change the colour of an unknown word between two #. For example, I need to change #word# to #word#.

Here is the code for the sub:
Code: [Select]
Public Sub ColorWords(p_Rich As RichTextBox, p_strWord As String, p_Color As OLE_COLOR)
    Dim intPos As Integer
       
    With p_Rich
        intPos = InStr(intPos + 1, .Text, p_strWord, vbTextCompare)
        Do While intPos <> 0
            .SelStart = intPos - 1
            .SelLength = Len(p_strWord)
            .SelColor = p_Color
            intPos = InStr(intPos + 1, .Text, p_strWord, vbTextCompare)
        Loop
    End With
End Sub

And here is how you would call the sub:
Code: [Select]
Call ColorWords(Userinput, "CHANGE", vbRed)(Userinput being the name of the richtextbox, CHANGE being the word to be coloured, and vbRed being the colour to change it to)

I tried changing "CHANGE" to "#*#" but that doesn't work, to "#" & * & "#", but that gives an expected expression error. How could this work?
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: VB6 - Wildcards?
« Reply #1 on: August 25, 2010, 04:30:27 PM »
No replies? It seems like my other thread got lots of replies, but I guess that was only because other members were arguing over which programming language is better than the next.
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: VB6 - Wildcards?
« Reply #2 on: August 25, 2010, 05:44:22 PM »
No replies? It seems like my other thread got lots of replies, but I guess that was only because other members were arguing over which programming language is better than the next.


I didn't even see this thread until just now, actually. Your bump made it more visible :)


Short Answer: is that Instr doesn't support Wildcards. As the function documentation states, it finds the First occurence of a string within another string- there are no special characters.


There is, however, an alternative. One could create a alternate Instr routine- say, InstrLike() And allow <that> to support wildcards.

An even better solution would be to create an InstrLike() Function that used regular Expressions... actually it's easier that way as well.

Code: [Select]
Public Function InstrLike(ByVal Start As Long, ByVal String1 As String, ByVal SearchPattern As String, _
Optional Compare As VbCompareMethod) As Long

   
    'normalize start argument, otherwise Mid$ will error out.
   
    If Start <= 0 Then Start = 1
    Dim RegEx As Object
    Dim Matches As Object
    Dim LoopMatch As Object
    Dim StrippedString As String
    'Create a string consisting only of the area we are to search.
    StrippedString = Mid$(String1, Start)
    'Create the Regular Expression Object.
    Set RegEx = CreateObject("VBScript.RegExp")
   
   
   
    RegEx.Pattern = SearchPattern
    'Create the match collection...
    Set Matches = RegEx.Execute(StrippedString)
    'if we don't find any matches, return 0.
    If Matches.Count = 0 Then
        InstrLike = 0
        Exit Function
    Else
        'otherwise, return the first match location, add the start argument as well, for when start is specified.
        'I use a for each here because I couldn't remember if matchcollection was
        '0 or 1-based and I was too lazy to look it up.
        For Each LoopMatch In Matches
            InstrLike = LoopMatch.FirstIndex + Start
            Exit Function
       
        Next
    End If



End Function


in order to integrate it into the function you gave, you would need to change both Instr() calls in the given routine to InstrLike.

An additional problem however is that you will now need to use Regular Expression escapes on all your searches. for most strings this isn't a problem, but things like slashes and periods and a few others need to be escaped if you ever use them.

For your scenario, you would now call the function like so:

Code: [Select]
ColorWords Userinput,"#\S*#", vbRed

You might even want to modify the method to allow for an additional optional boolean argument that can be used to forgo the regular expression code and just directly call and return the result from Instr (could be useful to avoid using regular expression escapes when colourizing literal values)
I was trying to dereference Null Pointers before it was cool.

ghostdog74



    Specialist

    Thanked: 27
    Re: VB6 - Wildcards?
    « Reply #3 on: August 25, 2010, 08:03:13 PM »
    curious, why are you even using vb6? for School? for  work? for fun?

    Helpmeh

      Topic Starter


      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: VB6 - Wildcards?
    « Reply #4 on: August 30, 2010, 03:22:50 PM »
    curious, why are you even using vb6? for School? for  work? for fun?
    I was taught VB6 in school, and I just like the feel of it.
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    ghostdog74



      Specialist

      Thanked: 27
      Re: VB6 - Wildcards?
      « Reply #5 on: August 30, 2010, 07:46:35 PM »
      I was taught VB6 in school, and I just like the feel of it.
      not that its my business to interfere with what you like to do, but my $0.02 is , VB6 is old. You should get on and learn modern languages. At least, they can do what VB6 can, (Python/Perl/VB.NET/Ruby/Java etc) and is catered to modern IT  technology

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: VB6 - Wildcards?
      « Reply #6 on: August 30, 2010, 08:48:41 PM »
      not that its my business to interfere with what you like to do, but my $0.02 is , VB6 is old. You should get on and learn modern languages.
      In a response that I'm sure will stun us all, I agree.

      Quote
      At least, they can do what VB6 can, (Python/Perl/VB.NET/Ruby/Java etc) and is catered to modern IT  technology

      C# FTW. :P

      Also, my experiments with IronPython (and to a lesser extent, IronRuby) are great; I haven't upped to VS 2010 yet, but to my knowledge it's quite possible to have a single project that uses any number of CLR languages- you could implement one part in python, other parts in C#, Ruby, VB.NET, and so forth. it's great for taking advantage of the abilities of each language where their purposes make the feature nice and short.

      Technically you can do it with the older versions as well but the ability to have them all integrated in a single project in the IDE wasn't until 2010.

      Another nice "side-effect" of the CLR-ization of Python into IronPython is that when you compile it, your users won't need to have the python interpreter. Sure, it's only a small download and I believe there are ways to inline it and stuff, but It's cool to be able to reflect on python/perl etc. defined CLR classes as if they were any other C# or VB.NET class, without any special treatment.

      On the flip-side of course is that although on windows the .NET framework (in more recent versions) is preinstalled, python isn't, but with Linux, Python is almost always available, and it's Mono (the Linux/Mac framework implementation) that would need to be installed. Either way, though, nearly any of them will be better then VB6 as far as built-in language features go. Just thinking back to my attempts at implementing IEnumVariant in VB6 to define a custom enumerator which required like 5 extra classes and Actually memory editing the Vtable due to Visual Basic limitations on method names is enough for me to shudder. C# (and I'm sure VB.NET, and of course python and perl and other languages) can have an enumerator via a simple function/method.

      When I first went into C# I figured I would still use VB6 a lot. I hardly ever use it now, and when I do, it feels, well, clunky. As ghostdog likes to point out in our little arguments now and then, it doesn't even have a way to sort arrays or pretty well anything built in. That's sort of silly. I mean, sure, it's not designed for short scripts... but VBScript is and it doesn't have Sort either. I wrote a class and interface (the latter a result of VB6's lack of delegates/function pointers, which is more a artifact of age then it is of design oversight (or maybe some of both)) to sort Variant Arrays, but it's still type sensitive (it can't sort arrays of integers, for example) and I shouldn't have had to write it. The best sort of implementation is one where you call a sort function or method and optionally pass in a comparison routine delegate. This is how almost all modern languages implement this, and the more Object oriented languages even define interfaces that allow objects to sort themselves, as well, by defining comparison routines and interfaces.

      And this doesn't even yet touch on the rising paradigm of "functional programming" epitomized in .NET as the first-class language F#. but most languages are capable of "functional programming" that have a concept of either function pointers (more messy, but still... doable) or lambda expressions/closures (python, perl (? I think) .NET languages, java, etc).

      I got stuck with VB6 and refused to switch until a few months ago (3?) and now I consider C# my main language, and only really use VB6 for maintaining my older projects, which have for the most part fallen by the wayside. For example, BCFile was a file library intended to replace the file access statements (Open, Put, Get, etc) with an Object Model- MS does this sort of with the FileSystemObject but that's more geared for scripting and text files, mine was also going to be able to read and write binary data as well as alternate data streams.

      It works fine, and is rather powerful, but there is a lot of stuff I had to do for the aforementioned custom enumerator that I ended up undoing because it turns out that messing around with the machine code of a compiled program can be unpredictable (who knew?  ::)). But the thing is, That is the type of thing that should be built in.

      Whatever you choose to switch to, make sure to stick with it. For your next project, instead of immediately starting a VB project, do it in Python, or C#, or whatever language you want to try. And STICK to it, don't give up and do it in VB anyway. The more small, and the more large projects you do in the language the better you will get and the more familar you will be with it. I started by rewriting my expression evaluator in C# from scratch, the idea was to best leverage the far more powerful Object capabilities of the language. I had the algorithms pretty well memorized and it was merely a matter of learning the syntax- the language, and learning how to best express myself. I started out constantly missing semicolons (due to my long years with VB6) but now I find myself adding semicolons to my old VB6 programs when performing maintenance.


      I was trying to dereference Null Pointers before it was cool.