10 Replies - 10308 Views - Last Post: 30 June 2011 - 06:58 PM Rate Topic: -----

#1 RemyAble  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-June 11

Aligning array output in a richtextbox

Posted 29 June 2011 - 10:00 PM

Hello,

I have two 1d arrays each containing some string data. I need to dump the contents of the arrays to a single richtextbox. I can do this, however the text isn't aligned properly. See the example:

Dr. No		$425,488,741.00
From Russia with Love		$555,909,803.00
Goldfinger		$868,659,354.00
Thunderball		$966,435,555.00
You Only Live Twice		$720,388,023.00
On Her Majesty's Secret Service		$513,445,231.00



I need to get the text to line up cleanly as in the next example:
Dr. No                            $425,488,741.00
From Russia with Love             $555,909,803.00
Goldfinger                        $868,659,354.00
Thunderball                       $966,435,555.00
You Only Live Twice               $720,388,023.00
On Her Majesty's Secret Service   $513,445,231.00



Can anyone suggest a solution or method I can look at as an example. I am doing this for a class of mine, so I would prefer some direction rather than code. Any help is appreciated.

Thanks!

-Remy

Is This A Good Question/Topic? 0
  • +

Replies To: Aligning array output in a richtextbox

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Aligning array output in a richtextbox

Posted 29 June 2011 - 10:35 PM

String.PadRight is what your after.

Dim str1 As String = "some string"
Dim str2 As String = "other"
Debug.WriteLine(str1.PadRight(15) & str2)

Was This Post Helpful? 1
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Aligning array output in a richtextbox

Posted 29 June 2011 - 10:55 PM

Also using a fixed-width font is a must.
Was This Post Helpful? 0
  • +
  • -

#4 RemyAble  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-June 11

Re: Aligning array output in a richtextbox

Posted 29 June 2011 - 10:57 PM

Not exactly producing the ouput I'm looking for, but thanks for the quick reply. Here's the piece of code I'm working with:

        Array.Sort(TitleData, BoxOfficeData)
        For i = 0 To TitleData.Length - 1
            'Console.WriteLine("   {0,-40}: {1}", TitleData(i), BoxOfficeData(i))
            rtxtListings.Text = rtxtListings.Text & (TitleData(i).PadRight(80) & _
                                                     BoxOfficeData(i) & Chr(13))
        Next i
        'Console.WriteLine()



The console output produces exactly what I'm looking for, however I'm not sure how to get that format to the richtextbox. Even with the padding in place, the righ "column" of data still doesn't line up cleanly as in my second example from the first post.

This post has been edited by RemyAble: 29 June 2011 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 RemyAble  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-June 11

Re: Aligning array output in a richtextbox

Posted 29 June 2011 - 11:32 PM

Essentially, I now need to convert the following line to one that will allow me to write the format and data to the richtextbox.

Console.WriteLine("   {0,-40}: {1}", TitleData(i), BoxOfficeData(i))



obviously, this:
rtxtListings.Text = ("   {0,-40}: {1}", TitleData(i), BoxOfficeData(i))



doesn't work. The IDE expects a parantheses in place of the comma after the second double-quotes.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Aligning array output in a richtextbox

Posted 30 June 2011 - 06:45 AM

This is most likely in a loop - right? You change the 15 to whatever works.

rtxtListings.Text &= String.Format("   {0:} {1}{2}", TitleData(i).RadRight(15), BoxOfficeData(i), Environment.Newline)

This post has been edited by hawkvalley1: 30 June 2011 - 07:33 AM

Was This Post Helpful? 1
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Aligning array output in a richtextbox

Posted 30 June 2011 - 07:39 AM

Have you thought about using a ListView control and using the columns. You could produce pretty close to the same effect you're describing. Just remove the column headers and change some selection settings. Just a thought in case you're open to other suggestions.

EDIT:

Just messing around, here is how it looks taking your array and putting it into columns in a listview.
Attached Image

2 columns, second column is right justified.
Column Headers are turned off

Details view.

This post has been edited by CharlieMay: 30 June 2011 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#8 RemyAble  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-June 11

Re: Aligning array output in a richtextbox

Posted 30 June 2011 - 09:51 AM

View PostCharlieMay, on 30 June 2011 - 06:39 AM, said:

Have you thought about using a ListView control and using the columns. You could produce pretty close to the same effect you're describing. Just remove the column headers and change some selection settings. Just a thought in case you're open to other suggestions.

EDIT:

Just messing around, here is how it looks taking your array and putting it into columns in a listview.
Attachment LVColumns.JPG

2 columns, second column is right justified.
Column Headers are turned off

Details view.


That looks exactly what I'm going for, but this is for an assignment for school and I have strict requirements of what I supposed to do. I must sort the two 1d arrays and display the sorted results to a richtextbox. If this is something I can do with a listview, then I will check it out.

View Posthawkvalley1, on 30 June 2011 - 05:45 AM, said:

This is most likely in a loop - right? You change the 15 to whatever works.

rtxtListings.Text &= String.Format("   {0:} {1}{2}", TitleData(i).RadRight(15), BoxOfficeData(i), Environment.Newline)



Yes, this is withn a For loop. I'm at work now, but I will try this solution when I get home.

Thanks!

This post has been edited by RemyAble: 30 June 2011 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Aligning array output in a richtextbox

Posted 30 June 2011 - 10:27 AM

What is with Tutor and insisting on using multiple 1D arrays? for conveying different fields of an subject.
Doesn't this suggest, that you're going about thing wrong.
Encapsulate the fields into a class.

Public Class Movie_BoxOffice
 Protected _Title As String
 Protected _BoxOffice As Long
 
 Public Sub New(ByVal Title As String, ByVal BoxOffice As Long)
  _Title = Title : _BoxOffice = BoxOffice
 End Sub

 Public ReadOnly Property Title() As String
  Get
   Return _Title
  End Get
 End Property
 Public ReadOnly Property BoxOffice() As Long
  Get
   Return _BoxOffice
  End Get
 End Property
 End Sub 


End Class


So much easier to use.
Dim Movies() = {
  New Movie_BoxOffice("From Russia with Love ",555909803),
  New Movie_BoxOffice("Dr. No",425488741),
  New Movie_BoxOffice("From Russia with Love",555909803),
  New Movie_BoxOffice("Goldfinger",868659354),
  New Movie_BoxOffice("Thunderball",966435555),
  New Movie_BoxOffice("You Only Live Twice",720388023),
  New Movie_BoxOffice("On Her Majesty's Secret Service",513445231)
 }
Dim ByBox = From Movie In Move OrderBy Movie.BoxOffice Descending




Suggestion: Put your tutor towards this parallel vs class tutorial and this sorting tutorial.
I think there's even one on custom formatting.

This post has been edited by AdamSpeight2008: 30 June 2011 - 10:30 AM

Was This Post Helpful? 1
  • +
  • -

#10 RemyAble  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-June 11

Re: Aligning array output in a richtextbox

Posted 30 June 2011 - 06:02 PM

View PostAdamSpeight2008, on 30 June 2011 - 10:27 AM, said:

What is with Tutor and insisting on using multiple 1D arrays? for conveying different fields of an subject.
Doesn't this suggest, that you're going about thing wrong.
Encapsulate the fields into a class.

Public Class Movie_BoxOffice
 Protected _Title As String
 Protected _BoxOffice As Long
 
 Public Sub New(ByVal Title As String, ByVal BoxOffice As Long)
  _Title = Title : _BoxOffice = BoxOffice
 End Sub

 Public ReadOnly Property Title() As String
  Get
   Return _Title
  End Get
 End Property
 Public ReadOnly Property BoxOffice() As Long
  Get
   Return _BoxOffice
  End Get
 End Property
 End Sub 


End Class


So much easier to use.
Dim Movies() = {
  New Movie_BoxOffice("From Russia with Love ",555909803),
  New Movie_BoxOffice("Dr. No",425488741),
  New Movie_BoxOffice("From Russia with Love",555909803),
  New Movie_BoxOffice("Goldfinger",868659354),
  New Movie_BoxOffice("Thunderball",966435555),
  New Movie_BoxOffice("You Only Live Twice",720388023),
  New Movie_BoxOffice("On Her Majesty's Secret Service",513445231)
 }
Dim ByBox = From Movie In Move OrderBy Movie.BoxOffice Descending




Suggestion: Put your tutor towards this parallel vs class tutorial and this sorting tutorial.
I think there's even one on custom formatting.


AdamSpeight2008,

Hey, I appreciate your input. Unfortunately the class I take is part of a University program and I'm not sure how much control he has over the curriculum. While I'm sure your method is easier to implement and makes more sense, unfortunately, I don't think the instructor is going to bend on this topic...
Was This Post Helpful? 0
  • +
  • -

#11 RemyAble  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-June 11

Re: Aligning array output in a richtextbox

Posted 30 June 2011 - 06:58 PM

Thanks everyone for the assistance! Below is the code I am using. Hawk, As you stated, I switched the richtextbox's font to a fixed-width font (courier) and the following works great! Thanks again everyone!!

    Private Sub btnSortAZ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortAZ.Click
        If loaded = True Then
            rtxtListings.Text = ""
            rtxtListings.Text &= "Title                               Box Office Amount" & Chr(13)
            rtxtListings.Text &= "-------------------------------     -----------------" & Chr(13)
            'Order by title name
            Array.Sort(TitleData, BoxOfficeData)
            For i = 0 To TitleData.Length - 1
                boxofficedollars = Convert.ToDouble(BoxOfficeData(i))
                rtxtListings.Text &= String.Format("{0:} {1}{2}", TitleData(i).PadRight(37), _
                                                   String.Format("{0:C}", boxofficedollars), Environment.NewLine)
            Next i
            lblDataStatus.Text = "Data Loaded - Ordered by Title"
        Else
            MsgBox("You must first load the files.")
        End If
    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1