Solved

Dynamically set regexoptions at runtime (VB.NET)

Posted on 2007-03-20
4
326 Views
Last Modified: 2008-02-26
Hi,

I have am using VB.NET and have a form with checkboxes for each of the possible regexoptions (MultiLine, SingleLine, IgnoreCase, etc.). However I do not know how to dynamically set the options on my regex at runtime according to whether the checkbox is checked or not. Has anyone any idea how to do this? Any help would be greatly appreciated.

Thanks!
0
Comment
Question by:siono1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 18759959
The CheckBox has a "CheckedChanged" event, you can use that to change the values as follows:

Imports System.Text.RegularExpressions

Public Class frmRegEx
    Private rx As Regex
    Private rxOptDefault As RegexOptions = RegexOptions.None
    Private strPattern As String

    Private Sub checkboxMultiline_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxMultiline.CheckedChanged

        If Me.checkboxMultiline.Checked = True Then
            rx = New Regex(strPattern, RegexOptions.Multiline)

        Else
            rx = New Regex(strPattern, rxOptDefault)

        End If

    End Sub
End Class

Hope that helps!
0
 

Author Comment

by:siono1
ID: 18760191
Hi VBRocks, thanks for your solution. I see now I didn't put all the information in my question.

Your solution is almost perfect but what if the "rxOptDefault = RegexOptions.IgnoreCase OR RegexOptions.Multiline OR RegexOptions.Singleline" and I check the checkBoxCompiled and checkBoxIgnoreWhite and uncheck checkBoxSingleline. How can I combine these new options with the new default options without having to write lots of if-then statements? You can see how it would easily get complicated!

Thanks again!
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18760411
Here you go - use this.  If the user checks the checkbox, it will add it to the class variable, if they uncheck the checkbox, then it will remove it from the class variable.

Imports System.Text.RegularExpressions

Public Class frmRegEx

    Private rx As Regex
    Private rxOpt As RegexOptions
    Private strPattern As String

    Private Sub checkboxMultiline_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxMultiline.CheckedChanged

        If Me.checkboxMultiline.Checked = True Then

            rxOpt = rxOpt Or RegexOptions.Multiline
            rx = New Regex(strPattern, rxOpt)

        Else
            rxOpt = CType(rxOpt - RegexOptions.Multiline, RegexOptions)
            rx = New Regex(strPattern, rxOpt)

        End If
    End Sub

End Class

Hope that helps!
0
 

Author Comment

by:siono1
ID: 18762665
That helps a lot! Thanks!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month7 days, 4 hours left to enroll

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question