Link to home
Start Free TrialLog in
Avatar of teamdad
teamdad

asked on

TextBox formatting

This is part of a program I am working on and it so far works great but I was trying to get the numbers in a multi line textbox or a rich text box with a bit diffrent format than the listboxes give me.  I'm trying to get the output data in a format something like this: TextBox1.Text = intValue & " - " & intValue instead of using this:  ListBox1.Items.Add(intValue)  and  ListBox2.Items.Add(intValue)

Instead of a colum, I want a row with the numbers seperated by a dash -
Anyone got some code that will help me with this?



' Analyze the file
    Private Sub analyzeNumbers()
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        If Not File.Exists(fileName) Then
            Exit Sub
        End If
        frequencies = New Hashtable()
        Dim sr As New StreamReader(fileName)
        Dim inputLine As String
        Dim values As String()
        Dim value As String
        Dim intValue As Integer
        Dim valueFrequency As Integer
        inputLine = sr.ReadLine
        While Not (inputLine Is Nothing)
            values = Split(inputLine, ",")
            For Each value In values
                Try
                    valueFrequency = 0
                    intValue = Integer.Parse(value)
                    If frequencies.ContainsKey(intValue) Then
                        valueFrequency = CType(frequencies.Item(intValue), Integer)
                    End If
                    valueFrequency = valueFrequency + 1
                    frequencies.Item(intValue) = valueFrequency
                Catch ex As Exception
                    MessageBox.Show(inputLine, "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            Next
            inputLine = sr.ReadLine
        End While
        sr.Close()

        ' Find min and max occurrence
        Dim de As DictionaryEntry
        minOccurrence = 0
        maxOccurrence = 0
        For Each de In frequencies
            intValue = CType(de.Key, Integer)
            valueFrequency = CType(de.Value, Integer)
            If minOccurrence = 0 Then
                minOccurrence = valueFrequency
                maxOccurrence = valueFrequency
            Else
                minOccurrence = Math.Min(minOccurrence, valueFrequency)
                maxOccurrence = Math.Max(maxOccurrence, valueFrequency)
            End If
        Next

        ' Build lists of values matching min and max occurrence
        leastOccurring = New SortedList()
        mostOccurring = New SortedList()
        For Each de In frequencies
            intValue = CType(de.Key, Integer)
            valueFrequency = CType(de.Value, Integer)
            If valueFrequency = minOccurrence Then
                leastOccurring.Add(intValue, intValue)
            End If
            If valueFrequency = maxOccurrence Then
                mostOccurring.Add(intValue, intValue)
            End If
        Next

        ' Displays the results
        For Each intValue In leastOccurring.Values
            ListBox1.Items.Add(intValue)
         Next
        For Each intValue In mostOccurring.Values
            ListBox2.Items.Add(intValue)
         Next
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of 123654789987
123654789987

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of teamdad
teamdad

ASKER

Least and Most are two diffrent things and would never have the same number of values.  All I want to do is change how the output looks and instead of it being in two seperate ListBoxes, I want them in two seperate TextBoxes or RichTextBoxes.

Instead of this:

Least       Most
1             2      
3             4
5             6
7        
9  
     
I need this:

Least:
1 - 3 - 5 - 7 - 9

Most:
2 - 4 - 6

Dim s as string
  For Each intValue In leastOccurring.Values
          s = intValue.ToString + "-"
       Next
          Textbox1.text = s.Substring(0,s.Length -1 ) Removes the last  "-"

     Repeat the same thing for MostOccuring

If u want it in the same textbox then

  Textbox1.text = "\r\n"   \\appends a line
Tha means the value on left of "-" and on ight of it have nothing to do wit each other right??

Its just a display

So you can go ahead like this...

What I am Doing is, First Check which of the two list has more no. of values that should be on left of the Dash, Then iterate using index so that both values can be added simulteneously. Also you need to check when the list with less no of values is exhausted

        Dim i As Integer
        If mostOccurring.Count > leastOccurring.Count Then
            For i = 0 To mostOccurring.Count - 1
                If i < leastOccurring.Count - 1 Then
                    TextBox1.Text = mostOccurring.GetByIndex(i) & " - " & leastOccurring.GetByIndex(i)
                Else
                    TextBox1.Text = mostOccurring.GetByIndex(i)
                End If
            Next
        Else
             'This is a Repeatation of If part with LeastOccuring coming in the left side of dash
            For i = 0 To leastOccurring.Count - 1
                If i < mostOccurring.Count - 1 Then
                    TextBox1.Text = leastOccurring.GetByIndex(i) & " - " & mostOccurring.GetByIndex(i)
                Else
                    TextBox1.Text = leastOccurring.GetByIndex(i)
                End If
            Next
        End If





Better still if you use a multiColumn List box with two column having these two values, Your Choice
Avatar of teamdad

ASKER

123654789987 has the right idea but it only puts the last number of leastOccurring and mostOccurring in the textboxes.  What I have is two lists so to say; one for producing the least number of occurring numbers and one for the most occurring numbers in a file.  Both lists need to be seen but instead of having the numbers that a user sees in an up and down format "listbox" I want them to be in side to side format "textboxes" it's just a matter of reading the strings right to do it but i'm not sure how.
Avatar of teamdad

ASKER

The code below is what I came up with that does exactly what I was wanting it to do.  I'm giving the points to 123654789987 since the code provided did do a basic part of what I had asked to do but I needed it to do all the numbers and not just the one.

' Display the results in label or textbox
        Dim OccurringValues As String
        For Each intValue In leastOccurring.Values
            If OccurringValues = "" Then
                OccurringValues = intValue.ToString
            Else
                OccurringValues = OccurringValues & " - " & intValue.ToString
            End If
        Next
        TextBox1.Text = (OccurringValues)

        OccurringValues = ""
        For Each intValue In mostOccurring.Values
            If OccurringValues = "" Then
                OccurringValues = intValue.ToString
            Else
                OccurringValues = OccurringValues & " - " & intValue.ToString
            End If
        Next
        TextBox2.Text = (OccurringValues)
    End Sub