none
Alignment of columns of numbers RRS feed

  • Question

  • Hi,

      My program requires to display several rows of 4 columns of decimal numbers with variable 'sizes'.

    I wrote the code to align the rows & columns (making each element in a row the same size (by padding).

    After scratching my head for a while I realised that the font was of variable size so I looked

    for a fixed size font (Vis. Studio Express 2010) but although I tried them I got the same result!

    If you think my reasoning is correct can you inform me of a fixed width font or if it's possible  to

    download a set ?

    Regards Pete Hepple

    Friday, March 15, 2019 5:27 PM

Answers

  • Hi

    You could use this

    New Font(FontFamily.GenericMonospace, 12)
    

    or maybe one of these

    Courier New
    Courier
    Lucida Console
    Fixedsys
    Monospace


    Regards Les, Livingston, Scotland

    • Marked as answer by ArthurItis Monday, March 18, 2019 5:57 PM
    Friday, March 15, 2019 6:13 PM
  • Hi Les,

      Used Courier New. It worked a treat.

    Thanks

    Regards Pete

    • Marked as answer by ArthurItis Monday, March 18, 2019 5:57 PM
    Monday, March 18, 2019 5:56 PM

All replies

  • Hi

    You could use this

    New Font(FontFamily.GenericMonospace, 12)
    

    or maybe one of these

    Courier New
    Courier
    Lucida Console
    Fixedsys
    Monospace


    Regards Les, Livingston, Scotland

    • Marked as answer by ArthurItis Monday, March 18, 2019 5:57 PM
    Friday, March 15, 2019 6:13 PM
  • In regards to displaying four columns of decimal numbers, not sure what you mean  by variable sizes but perhaps this is what you mean.

    In this example I use a WebBrowser control which is populated with a List using XML literals and embedded expressions.

    Alignment, padding, margin etc can easily be tweaked to suit your needs. Downside is at least VS2013 is needed but you could (a tad messy) do the same by using for/each statements to create the structure shown in the code below. Otherwise there are other ways (at least a handful I can think of but messy) ranging from using string format e.g. String.Format("Some string {0,10}",12) in tangent with someDecimal.ToString("Nx"). Another choice would be to modify a DataGridView to not look like a DataGridView more or less setting a bunch of properties (not getting into this now but have done this).

    VS2013+ solution.

    Concrete class for sample data

    Public Class Item
        Public Property Value1() As Decimal
        Public Property Value2() As Decimal
        Public Property Value3() As Decimal
        Public Property Value4() As Decimal
    End Class
    

    Form code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim itemList As New List(Of Item)
    
            itemList.Add(New Item With
                    {.Value1 = 10.77D, .Value2 = 100D, .Value3 = 1D, .Value4 = 34.11D})
            itemList.Add(New Item With
                    {.Value1 = 110.77D, .Value2 = 90D, .Value3 = 1D, .Value4 = 33.5D})
            itemList.Add(New Item With
                    {.Value1 = 4.77D, .Value2 = 6D, .Value3 = 1D, .Value4 = 34.22D})
            itemList.Add(New Item With
                    {.Value1 = 0.77D, .Value2 = 69.65D, .Value3 = 1D, .Value4 = 34.22D})
    
            Dim data =
                    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
                        <head>
                            <style>
                            body{
                                margin:5px;
                                padding:0;
                            }
                            table {
                                  border-collapse: collapse;
                                  border-spacing: 0;
                                  border: 1px solid #ddd;
                            }
                             table.center {
                                width:70%; 
                                margin-left:15%; 
                                margin-right:15%;
                              }
                            th {  
                                background: #DFDFDF;
                                font-weight: bold;
                            }
                            th,td{
                                padding-right:10px;
                            }
                            </style>
                        </head>
                        <body style="">
                            <H3 align="center">Report</H3>
                            <table class="center">
                                <tr>
                                    <th align="right">First </th>
                                    <th align="right">Second </th>
                                    <th align="right">Third </th>
                                    <th align="right">Fourth </th>
                                </tr>
                                <%= From aItem In itemList Select
                                <tr>
                                    <td align="right"><%= aItem.Value1.ToString("n2") %></td>
                                    <td align="right"><%= aItem.Value2.ToString("n2") %></td>
                                    <td align="right"><%= aItem.Value3.ToString("n2") %></td>
                                    <td align="right"><%= aItem.Value4.ToString("n2") %></td>
                                </tr>
                                %>
                            </table>
                        </body>
                    </html>.ToString()
    
            WebBrowser1.ScrollBarsEnabled = False
            WebBrowser1.DocumentText = data
        End Sub
    
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, March 15, 2019 11:38 PM
    Moderator
  • Hi Les

      Thanks, I'll give it a try.

    Also very retired (83).

    Cheeky bit : Any chance I could eMail you about various (VB) issues ?

    Did I mention that it was a text box I was talking about ?

    By lengths I meant the different number of numbers per element.

    Anyway - thanks again

    Pete

    Saturday, March 16, 2019 6:06 PM
  • Hi Karen,

      Thanks, but you're way ahead of me...

    Pete

    Saturday, March 16, 2019 6:07 PM
  • Hi Les,

      Used Courier New. It worked a treat.

    Thanks

    Regards Pete

    • Marked as answer by ArthurItis Monday, March 18, 2019 5:57 PM
    Monday, March 18, 2019 5:56 PM
  • Hi Les,

      Used Courier New. It worked a treat.

    Thanks

    Regards Pete

    Hi

    Thanks.

    I did post my email address in this thread for a few hours, but not wanting to leave it I deleted the post. If you still want contact then post your own email here (obscured of course)


    Regards Les, Livingston, Scotland

    Monday, March 18, 2019 6:07 PM