Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > Assigning value 'ranges' from a richtextbox line
 

Assigning value 'ranges' from a richtextbox line

I am trying to make a program, where a user will input 'ranges' in a richtextbox, and then enter a value, which will be stored/categorized (in an array maybe) according to the 'range'.
What I exactly mean is:

User specifies ranges by inputting this line in the richtextbox: <,10,20,30,>
First range = < to 10 (no minimum boundary up to 10), second range = 10 to 20 , 3rd = 20 to 30 , 4th = 30 to > (30 to no maximum boundary)

Then a user enters a number. For example "-1" would fall in the "< to 10" category, "23" in the "20 to 30" category, "125" in the "30 to >" category.

I know there are other ways to do this, but I would like to see a solution (or some guidance) based on these requirements.

Best regards
trashr0x  Monday, November 30, 2009 2:34 PM
try this way, uses linq, if you are not using 3.5, you might have to change but at least gives an idea. You might need validation as well

 Private Sub btnDisplayRange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
        Dim RangeText = RichTextBox1.Text.Trim  ' is <,10,20,30,> 
        Dim splitrange = RangeText.Split(","c).ToList  'split based on comma
        splitrange.RemoveAll(Function(f) f = String.Empty) 'remove empty
        Dim InputValue = Integer.Parse(tbnumber.Text) ' User value
        Dim rangearray = splitrange.Select(Function(f) GetValueInInt(f)) 'get proper value for < >
        For i = 0 To rangearray.Count - 1
            If InputValue >= rangearray(i) And InputValue <= rangearray(i + 1) Then
                'you can replace < > with proper word or something if you want here
                MessageBox.Show(String.Format("{0} falls between {1} and {2}", tbnumber.Text, rangearray(i), rangearray(i + 1)))
            End If
        Next



    End Sub
    Private Function GetValueInInt(ByVal value As String) As Integer
        If value = "<" Then
            Return Integer.MinValue
        ElseIf value = ">" Then
            Return Integer.MaxValue
        Else
            Return Integer.Parse(value)
        End If
    End Function

Arjun Paudel
  • Marked As Answer bytrashr0x Monday, November 30, 2009 3:30 PM
  •  
Arjun Paudel  Monday, November 30, 2009 3:28 PM
Change Accordingly

Private Function GetValueInDec(ByVal value As String) AsDecimal
If value = "<" Then
Return Decimal.MinValue
ElseIf value = ">" Then
Return Decimal.MaxValue
Else
Return Decimal.Parse(value)
End If
End Function

Dim RangeText = RichTextBox1.Text.Trim ' is <,10,20,30,>
Dim splitrange = RangeText.Split(","c).ToList 'split based on comma
splitrange.RemoveAll(Function(f) f = String.Empty) 'remove empty
Dim InputValue = Integer.Parse(tbnumber.Text) ' User value
Dim rangearray = splitrange.Select(Function(f) GetValueInDec(f)) 'get proper value for < >
For i = 0 To rangearray.Count - 1
If InputValue >= rangearray(i) And InputValue <= rangearray(i + 1) Then
'you can replace < > with proper word or something if you want here
MessageBox.Show(String.Format("{0} falls between {1} and {2}", tbnumber.Text, rangearray(i), rangearray(i + 1)))
End If
Next

Arjun Paudel
  • Marked As Answer bytrashr0x Tuesday, December 01, 2009 1:17 AM
  •  
Arjun Paudel  Monday, November 30, 2009 3:35 PM
try this way, uses linq, if you are not using 3.5, you might have to change but at least gives an idea. You might need validation as well

 Private Sub btnDisplayRange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRange.Click
        Dim RangeText = RichTextBox1.Text.Trim  ' is <,10,20,30,> 
        Dim splitrange = RangeText.Split(","c).ToList  'split based on comma
        splitrange.RemoveAll(Function(f) f = String.Empty) 'remove empty
        Dim InputValue = Integer.Parse(tbnumber.Text) ' User value
        Dim rangearray = splitrange.Select(Function(f) GetValueInInt(f)) 'get proper value for < >
        For i = 0 To rangearray.Count - 1
            If InputValue >= rangearray(i) And InputValue <= rangearray(i + 1) Then
                'you can replace < > with proper word or something if you want here
                MessageBox.Show(String.Format("{0} falls between {1} and {2}", tbnumber.Text, rangearray(i), rangearray(i + 1)))
            End If
        Next



    End Sub
    Private Function GetValueInInt(ByVal value As String) As Integer
        If value = "<" Then
            Return Integer.MinValue
        ElseIf value = ">" Then
            Return Integer.MaxValue
        Else
            Return Integer.Parse(value)
        End If
    End Function

Arjun Paudel
  • Marked As Answer bytrashr0x Monday, November 30, 2009 3:30 PM
  •  
Arjun Paudel  Monday, November 30, 2009 3:28 PM
Thanks Arjun, one more question though. What if a user enters a decimal number like "0.123" or "25.435" though?
trashr0x  Monday, November 30, 2009 3:31 PM
Change Accordingly

Private Function GetValueInDec(ByVal value As String) AsDecimal
If value = "<" Then
Return Decimal.MinValue
ElseIf value = ">" Then
Return Decimal.MaxValue
Else
Return Decimal.Parse(value)
End If
End Function

Dim RangeText = RichTextBox1.Text.Trim ' is <,10,20,30,>
Dim splitrange = RangeText.Split(","c).ToList 'split based on comma
splitrange.RemoveAll(Function(f) f = String.Empty) 'remove empty
Dim InputValue = Integer.Parse(tbnumber.Text) ' User value
Dim rangearray = splitrange.Select(Function(f) GetValueInDec(f)) 'get proper value for < >
For i = 0 To rangearray.Count - 1
If InputValue >= rangearray(i) And InputValue <= rangearray(i + 1) Then
'you can replace < > with proper word or something if you want here
MessageBox.Show(String.Format("{0} falls between {1} and {2}", tbnumber.Text, rangearray(i), rangearray(i + 1)))
End If
Next

Arjun Paudel
  • Marked As Answer bytrashr0x Tuesday, December 01, 2009 1:17 AM
  •  
Arjun Paudel  Monday, November 30, 2009 3:35 PM
Hey, gonna have to annoy you once more ;)
I just found a situation where things can get more complex (and unfortunately I need to implement it).

What if we have an example like this? "<=50,>50" (so 49 is <=50, 51 is >50).. This gets a bit more confusing as after the '<' or '>' it will have to check if there is a '=' as well?

Me = confused.
trashr0x  Tuesday, December 01, 2009 3:53 AM
Let me be more exact on what my requirement is.

There is an input file with multiple lines, each line consists of 'product' and 'price'

The file looks like this:

meat1, 20
meat2 2.54
meat3, 21.45


What I am exactly trying to do is specify (from the richtextbox) how I want to categorize the 'price' attribute. So by entering in the richtextbox <=10, 15, 20 I would like the results of the 'categorization' exported to a file (after reading the data from the input file) which will say:

meat1 falls in the '15 to 20' category
meat2 falls in the '<=10' category
meat3 falls in the '>20' category


Or for example <=50,>50 would result to:

meat1 falls in the <=50 category
meat2 falls in the <=50 category
meat3 falls in the <=50 category


So I guess that in order to be able to specify the 'ranges', we would have to be able to deal with these situations: actual numbers , "> ", "< ", "<= " and ">= "

And another problem with the numbers in the input file is that some numbers don't have a decimal value (i.e. 20) while others do (i.e. 14.5)


It's a lot to ask I know, but if I wasn't stuck I wouldn't of be asking.

Thanks for the help in advance (if i get any).

Best regards.
trashr0x  Tuesday, December 01, 2009 4:41 AM
Disregard this, it's too silly and complex. I'll just stick with the <,10,20,30> format.
trashr0x  15 hours 8 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Arithmatic Expression: One Less Than?
• How to get datasource.tableRow
• System.Data.Entity Required by ClickOnce Deployment
• Setting focus on textbox when form loads
• IP Subnet and IP Range
• Mysterious Disabled KeyDown events on VB6....
• save own settings class using typeConverter
• Searching LDAP for field with \'s doesn't work (VB script)
• .exe only runs on some computers
• Performanceproblem in a large loop under XP but not under W2K