0

I don't know create a multi line string please the best solution. The code below does not all use multi line strings. You can see the code I commented out was because I didn't know how to create a multi line string.

Thanks Jack outwithmycode 'information code

Public Shared Function GetReceipt(ByVal transaction_Conflict As Transaction) As String
            Return GenerateReceiptTemplate().Replace("<Orders/>", GetOrderRows(transaction_Conflict)).Replace("<Total/>", transaction_Conflict.Total.ToString() & "PHP").Replace("<Cash/>", transaction_Conflict.Cash.ToString() & "PHP").Replace("<Change/>", transaction_Conflict.Change.ToString() & "PHP").Replace("<Id/>", transaction_Conflict.Id).Replace("<Cashier/>", transaction_Conflict.GetCashier().Fullname).Replace("<Date/>", transaction_Conflict.Date.ToString())
        End Function

Private Shared Function GetOrderRows(ByVal transaction_Conflict As Transaction) As String
            Dim result As String = ""
            transaction_Conflict.GetOrders().ForEach(Sub(item As Order)
                result &= "<tr>"
                result &= "<td>" & item.GetProduct().Name & "</td>"
                result &= "<td align='center'>x " & item.Quantity & "</td>"
                result &= "<td align='right'>" & item.Subtotal & "PHP</td>"
                result &= "</tr>"
            End Sub)
            Return result
        End Function

 'code output in VB.NET
        
 Private Shared Function GenerateReceiptTemplate() As String
            Return "<center>" & vbCrLf &
                           "<font size='24px'><b>WcDonalds</b></font><br/>" & vbCrLf &
                           "<span>wcdonalds@gmail.com</span>" & vbCrLf &
                       "</center>" & vbCrLf &
                       "<br/><br/>" & vbCrLf &
                       "<table width='100%'>" & vbCrLf &
                           "<thead>" & vbCrLf &
                               "<tr>" & vbCrLf &
                                   "<th align='left'>Product Name</th>" & vbCrLf &
                                   "<th align='center'>Quantity</th>" & vbCrLf &
                                   "<th align='right'>Subtotal</th>" & vbCrLf &
                               "</tr>" & vbCrLf &
                           "</thead>" & vbCrLf &
                           "<tbody>" & vbCrLf &
                               "<Orders/>" & vbCrLf &
                           "</tbody>" & vbCrLf &
                       "</table>" & vbCrLf &
                       "<br/>" & vbCrLf &
                       "<center>---------------------------------------</center>" & vbCrLf &
                       "<br/>" & vbCrLf &

            'Total: <b><Total/></b><br/>
            'Cash: <b><Cash/></b><br/>
            'Change: <b><Change/></b><br/>
            '               <br/>
            'Transaction ID:   #<Id/><br/>
            'Cashier: <Cashier/><br/>
            'Date: <Date/><br/>

            '               <br/>
            '               <center>---------------------------------------</center>
            '               <br/>

            '               <center><b>Thanks For visiting WcDonalds</b></center>





        End Function
roy
  • 693
  • 2
  • 11
  • Mr. Jack, `vbCrLf` has no effect nor meaning in HTML structure. Don't mix up string manipulation and HTML elements and tags. Use `
    ` tag to break HTML lines, `

    ` to start a new paragraph....etc. See [HTML Element Reference](https://www.w3schools.com/TAgs/default.asp).
    – dr.null Jun 24 '22 at 17:49
  • You can use [StringBuilder](https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder?view=net-6.0) to create the HTML output line by line using the [StringBuilder.AppendLine] method. – dr.null Jun 24 '22 at 17:55
  • @dr.null , this is just for string manipulation. if I use the html tag then the output is empty product name, quantity and total – roy Jun 24 '22 at 18:15
  • @dr.null , I believe you can provide solutions and answers to me because of your ability and experience. – roy Jun 24 '22 at 18:19
  • @dr.null , from the code that I posted, it only remains to give 'vbCrLf' in the code that I made a comment out. because it uses 'total:' so I'm still confused about using 'vbcrlf' . if the code before commenting out it has succeeded in providing print output for me. – roy Jun 24 '22 at 18:23
  • Thank you for your kind words. Could you please edit your question to tell us where do you get the values of the labels? The list of products which includes `Product Name`, `Quantity`, and `Subtotal` properties? and `Transaction ID: #`, `Cashier:`, `Date:`? – dr.null Jun 24 '22 at 18:32
  • @Jack Is there a reason that you cannot use a modern version of Visual Studio? It would be easier to answer your questions if you could use, say, at least VS2017. – Andrew Morton Jun 24 '22 at 21:51
  • @dr.null , I have updated according to the information you need . – roy Jun 26 '22 at 14:00

4 Answers4

1

You just type the String literal over multiple lines, with the opening double-quote on the first line and the closing double-quote on the last line. Just note that you'll need to push all but the first physical line to the left of the code window as any leading whitespace you include will be considered part of the String. E.g.

    Dim str = "First Line
Second Line
Third Line"

I can't recall exactly when this feature was introduced, so it may not be available in VB 2010. In that case, you can still use an XML literal, e.g.

    Dim str = <text>First Line
Second Line
Third Line</text>.Value
user18387401
  • 2,514
  • 1
  • 3
  • 8
1

Your HTML is a mess. This is just a little better.

    Dim xe As XElement = <div>
                             <p style="text-align: center; font-size: 24px;font-weight:700;">WcDonalds</p>
                             <p style="text-align: center;">wcdonalds@gmail.com</p>
                             <table style="width: 100%;">
                                 <thead>
                                     <tr>
                                         <th style="width: 33.3%; text-align: left;">Product Name</th>
                                         <th style="width: 33.3%; text-align: center;">Quantity</th>
                                         <th style="width: 33.3%; text-align: right;">Subtotal</th>
                                     </tr>
                                 </thead>
                                 <tbody>
                                     <tr>
                                         <td style="width: 33.3%; text-align: left;">a product</td>
                                         <td style="width: 33.3%; text-align: center;">1</td>
                                         <td style="width: 33.3%; text-align: right;">$1.23</td>
                                     </tr>
                                 </tbody>
                             </table>
                             <br/>
                             <p style="text-align: center;">---------------------------------------</p>
                             <p>Total</p>
                             <p>Cash</p>
                             <p>Change</p>
                             <p style="text-align: center;">---------------------------------------</p>
                             <p style="text-align: center; font-weight:700;">Thanks For visiting WcDonalds</p>
                         </div>

    Return xe.ToString
dbasnett
  • 11,334
  • 2
  • 25
  • 33
1

The text can be saved as an Embedded Resource (Text file) and retrieved from there. This helps one's code to look cleaner.

Try the following:

VS 2010:

Open Solution Explorer

  • In VS menu, select View
  • Select Solution Explorer

Open Properties Window

  • In VS menu, select View
  • Select Properties Window

Add Folder (name: Templates)

  • In Solution Explorer, right-click <project name>
  • Select Add
  • Select New Folder
  • Type desired name (ex: Templates)

Add Text File (name: ReceiptTemplate.txt)

  • In Solution Explorer, right-click on the new folder you just created (ex: Templates)
  • Select Add
  • Select New Item
  • Expand Common Items
  • Click General
  • Select Text File (name: ReceiptTemplate.txt)
  • Click Add

Set Properties for Text File

  • In Solution Explorer, click the text file that you added (ex: ReceiptTemplate.txt)
  • In the Properties Window, set Build Action to Embedded Resource

Add desired text to text file. For example:

ReceiptTemplate.txt:

<center>
  <font size='24px'>
    <b>ABC Cafe</b>
  </font>
  <br/>
  <span>abccafe@gmail.com</span>
</center>

<br/>
<br/>

<table width='100%'>
    <thead>
        <tr>
            <th align='left'>Product Name</th>
            <th align='center'>Quantity</th>
            <th align='right'>Subtotal</th>
        </tr>
    </thead>
    <tbody>
        <Orders/>
    </tbody>
</table>
<br/>
<center>---------------------------------------</center>
<br/>

Total: <b><Total/></b><br/>
Cash: <b><Cash/></b><br/>
Change: <b><Change/></b><br/>
<br/>
Transaction ID:   #<Id/><br/>
Cashier: <Cashier/><br/>
Date: <Date/><br/>

<br/>

<center>---------------------------------------</center>
<br/>
<center><b>Thanks For visiting ABC Cafe</b></center>

Add a module (name: HelperLoadResource.vb)

'Notes: 
'        Need to set property Build Action: Embedded Resource For Each file that 
'        needs to be loaded
'
'Resources: 
'        https'stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file

Imports System.Text
Imports System.IO
Imports System.Reflection

Module HelperLoadResource

    Public Function ReadResource(filename As String) As String
        Return ReadResource(filename, System.Text.Encoding.UTF8)
    End Function

    Public Function ReadResource(filename As String, fileEncoding As System.Text.Encoding) As String
        Dim fqResourceName As String = String.Empty
        Dim result As String = String.Empty

        'get executing assembly
        Dim execAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

        'get resource names
        Dim resourceNames As String() = execAssembly.GetManifestResourceNames()

        If resourceNames IsNot Nothing AndAlso resourceNames.Length > 0 Then
            For Each rName As String In resourceNames
                If rName.EndsWith(filename) Then
                    fqResourceName = rName
                    Exit For
                End If
            Next

            If String.IsNullOrEmpty(fqResourceName) Then
                Throw New Exception(String.Format("Resource '{0}' not found.", filename))
            End If

            'get file text
            Using s As Stream = execAssembly.GetManifestResourceStream(fqResourceName)
                Using reader As StreamReader = New StreamReader(s, fileEncoding)

                    'read text
                    result = reader.ReadToEnd()
                End Using
            End Using
        End If

        Return result
    End Function
End Module

Note: This also works for newer versions of Visual Studio, however some of the steps listed in "Add Text File" are different.


The file/folder structure should look similar to the following:

enter image description here


Usage:

Dim receiptTemplate As String = HelperLoadResource.ReadResource("ReceiptTemplate.txt")

Resources:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
1

All right, all what you need here is the first function to generate and return the receipt. I'll modify the answer of your previous question using the same XElement approach to get the values from transaction_Conflict parameter.

For the labels, append the corresponding values from the transaction_Conflict object enclosed with <%= %>. For the table part, call the transaction_Conflict.GetOrders() method in <%= %> to get the list and call the .Select extension method to create the rows and cells as shown in the code.

Public Shared Function GetReceipt(ByVal transaction_Conflict As Transaction) As String
    Return <html>
                <center>
                    <font size='24px'><b>WcDonalds</b></font><br/>
                    <span>wcdonalds@gmail.com</span>
                </center><br/>
                <table width='100%'>
                    <thead>
                        <tr>
                            <th align='left'>Product Name</th>
                            <th align='center'>Quantity</th>
                            <th align='right'>Subtotal</th>
                        </tr>
                    </thead>
                    <tbody>
                        <%= transaction_Conflict.GetOrders().Select(
                            Function(order)
                                Return _
                        <tr>
                            <td><%= order.GetProduct().Name %></td>
                            <td align='center'><%= order.Quantity %></td>
                            <td align='right'><%= order.SubTotal %></td>
                        </tr>
                            End Function) %>
                    </tbody>
                </table>
                <br/><hr/><br/>
            
                Total: <b><%= transaction_Conflict.Total %> PHP</b><br/>
                Cash: <b><%= transaction_Conflict.Cash %> PHP</b><br/>
                Change: <b><%= transaction_Conflict.Change %> PHP</b><br/><br/>
                Transaction ID: #<%= transaction_Conflict.Id %><br/>
                Cashier: <%= transaction_Conflict.GetCashier().Fullname %>><br/>
                Date: <%= transaction_Conflict.Date.ToString() %><br/><br/><hr/>

                <center><b>Thanks For visiting WcDonalds</b></center>
            </html>.ToString()
End Function

Which generates in my browser something like:

SOQ72747486

dr.null
  • 4,032
  • 3
  • 9
  • 12
  • thanks reply from you . but I have a slight error `<%= order.ProductName %>` ProductName' is not a member and `$` Character is not valid – roy Jun 27 '22 at 04:03
  • @Jack ah, replace it with `<%= order.GetProduct().Name %>`. And replace `$` with string concatenations. See the revision. – dr.null Jun 27 '22 at 04:11
  • thanks reply from you `<%= order.GetProduct().Name %>` ok for this no more errror but for `$` Character is not valid – roy Jun 27 '22 at 04:16
  • is there another solution to replace the character `$` so I can use it in vS 2012? – roy Jun 27 '22 at 04:48
  • @Jack Why? Don't tell me it doesn't work! Anyways, you can do instead `String.Concat(transaction_Conflict.Total, " PHP")`. Have a good day! – dr.null Jun 27 '22 at 04:53
  • thank you for having the best ability and experience so that it is easy for you to provide the best solution. You are a master – roy Jun 27 '22 at 05:00
  • @Jack Thank you again Mr. Kind Jack. Please don't forget VS2022. Good luck. – dr.null Jun 27 '22 at 05:18
  • you're welcome. Okay sir – roy Jun 27 '22 at 06:11
  • @Jack `PHP` is a currency isn't it? If so then we don't need any string concatenations here. Just move it out `<%= %>` as the edit. `PHP` in HTML/Web context is really confusing word/Abbr. – dr.null Jun 27 '22 at 07:34
  • 1
    yes that's right `PHP` is a currency . I've used the last code edit from you running perfectly – roy Jun 27 '22 at 07:44