0

VB2010. May be a bit hard to understand but I have a list of classes and one field is a string message. I'm outputting these messages to an RTF document but want to maximize use of horizontal space so am trying to dynamically create a table and fit as many messages in one row as possible and then another row. This while I maintain a max width possible for a row.

Public Class TripInfo
    Public SysId As String = ""
    Public CreateDate As DateTime
    Public OutMessage As String = ""
    Public OutMessageWidth As Integer = 0 'the width of the message in char count up to first LF
End Class

Dim myTrips1 as New List(Of TripInfo)
Dim myTrips2 as New List(Of TripInfo)

So as I iterate through the lists I want to create rows that are themselves no longer than 45 characters. Something like:

---------------------------------------------
|"Message1 |"Message2 |"Much longer message |
| Trip1   "| Trip2"   | with two lines"     |
---------------------------------------------
|"message is even longer than the others"   |
---------------------------------------------
|"trip is ok |"trip was cancelled due to CW |
| enroute"   | must log to system"          |
---------------------------------------------
|"Message3 |"Message4 |"Message5 |"Message6"|
| Trip3   "| Error"   | Stop"    |          |
---------------------------------------------

*Note that the message itself can span more than 1 line with LFs to display a multi-line message

I have scratch code to write the RTF code for the tables and have substituted fake messages with multiple embedded LFs and the output looks good.

Dim sbTable As New StringBuilder
sbTable.Append("\pard \trowd\trql\trgaph108\trleft36\cellx1636\cellx3236\cellx4836\intbl R1C1\cell R1C2\cell R1C3\cell\row \pard")
sbTable.Append("\pard \trowd\trql\trgaph108\trleft36\cellx4642\intbl R1C1\cell\row \pard")
sbTable.Append("\pard \trowd\trql\trgaph0\trleft36\cellx4642\cellx5500\intbl R1C1\cell R1C2\cell\row \pard")

However I just cant seem to get my head around how to even start this to do it dynamically. It seems like I may need to do two iterations. One to break up the messages into rows and then another to actually write the RTF code. I have so far pseudo code but need some help with my logic.

dim v as integer = 0 'total width of current row
For each t as TripInfo in myTrips1 and myTrips2
    if (t.OutMessageWidth added to v) > 45 then
      start new row and append
    else
      append to current row
    endif
Next t
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sinDizzy
  • 1,300
  • 7
  • 28
  • 60

1 Answers1

0

Without knowing the properties of your TripInfo class, I'm going to have to make some assumptions. If any property I assume doesn't exist, you can either create it or modify the code to get the same effect.

Dim t As TripInfo, AllTrips As New List(Of TripInfo)
For Each t In myTrips1
    AllTrips.Add(t)
Next
For Each t In myTrips2
    AllTrips.Add(t)
Next
If AllTrips.Count > 0 Then
    For Each t In AllTrips
        Dim NewRow() As String = t.Lines
        Dim w As Integer = t.OutMessageWidth
        Dim h As Integer = t.Lines.Count
        For ItemHeight As Integer = h To 1 Step -1
            For Each CompareTrip As TripInfo In AllTrips
                If AllTrips.IndexOf(t) <> AllTrips.IndexOf(CompareTrip) _
                And CompareTrip.Lines.Count = ItemHeight _
                And w + CompareTrip.OutMessageWidth <= 45 Then
                    w += CompareTrip.OutMessageWidth
                    For l As Integer = 0 To h -1
                        NewRow(l) = NewRow(l).PadRight(w) & CompareTrip.Lines(l)
                    Next
                    AllTrips.Remove(CompareTrip)
                End If
            Next
        Next
        AllTrips.Remove(t)
        'Write lines of NewRow to your RTF
    Next
End If
Josh
  • 1,088
  • 1
  • 7
  • 16
  • Let me take some time to digest this. The only thing I see is that maybe I did not such a good job of relaying my idea as each box above is actually one cell in the table. Let me see if I can work with your sample and adjust as necessary. – sinDizzy Sep 18 '15 at 19:35