Solved

TextBox formatting

Posted on 2004-10-19
6
186 Views
Last Modified: 2010-04-24
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
0
Comment
Question by:teamdad
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
123654789987 earned 500 total points
ID: 12355076
U can do something like this

Dim str as string
for i = 0  to 1
         str = leastOccurring(i)
         str = str + "-" + mostOccurring(i)
Textbox1.text = str + "\r\n"
next

For this to work the no of values in leastOccurring should be equal to the number of values in mostOccurring

0
 

Author Comment

by:teamdad
ID: 12355176
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

0
 
LVL 10

Expert Comment

by:123654789987
ID: 12355282
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
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12355463
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
0
 

Author Comment

by:teamdad
ID: 12358101
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.
0
 

Author Comment

by:teamdad
ID: 12364756
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
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with preventing selection from a combobox 11 34
get combo value in class 5 14
Code enhancement 4 21
VB.NET Application Installation with sqlserver 8 29
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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