Solved

TextBox formatting

Posted on 2004-10-19
6
183 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now