我不知道創建一個多行字串請最好的解決方案。下面的代碼并不都使用多行字串。您可以看到我注釋掉的代碼是因為我不知道如何創建多行字串。
謝謝杰克
的資訊代碼
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>[email protected]</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
uj5u.com熱心網友回復:
好吧,這里你需要的只是生成和回傳收據的第一個函式。我將使用相同的方法修改您
uj5u.com熱心網友回復:
您只需String在多行中鍵入文字,在第一行使用左雙引號,在最后一行使用右雙引號。請注意,您需要將除第一條物理行之外的所有內容都推到代碼視窗的左側,因為您包含的任何前導空格都將被視為String. 例如
Dim str = "First Line
Second Line
Third Line"
我不記得這個特性是什么時候引入的,所以它可能在 VB 2010 中不可用。在這種情況下,您仍然可以使用 XML 文字,例如
Dim str = <text>First Line
Second Line
Third Line</text>.Value
uj5u.com熱心網友回復:
你的 HTML 是一團糟。 這只是好一點。
Dim xe As XElement = <div>
<p style="text-align: center; font-size: 24px;font-weight:700;">WcDonalds</p>
<p style="text-align: center;">[email protected]</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
uj5u.com熱心網友回復:
文本可以保存為嵌入式資源(文本檔案)并從那里檢索。這有助于一個人的代碼看起來更干凈。
嘗試以下操作:
與 2010 年相比:
打開解決方案資源管理器
- 在 VS 選單中,選擇查看
- 選擇解決方案資源管理器
打開屬性視窗
- 在 VS 選單中,選擇查看
- 選擇屬性視窗
添加檔案夾(名稱:模板)
- 在解決方案資源管理器中,右鍵單擊 <專案名稱>
- 選擇添加
- 選擇新檔案夾
- 輸入所需的名稱(例如:模板)
添加文本檔案(名稱:ReceiptTemplate.txt)
- 在解決方案資源管理器中,右鍵單擊您剛剛創建的新檔案夾(例如:模板)
- 選擇添加
- 選擇新專案
- 展開常見專案
- 單擊常規
- 選擇文本檔案(名稱:ReceiptTemplate.txt)
- 點擊添加
設定文本檔案的屬性
- 在解決方案資源管理器中,單擊您添加的文本檔案(例如:ReceiptTemplate.txt)
- 在屬性視窗中,將Build Action設定為Embedded Resource
將所需的文本添加到文本檔案。例如:
收據模板.txt:
<center>
<font size='24px'>
<b>ABC Cafe</b>
</font>
<br/>
<span>[email protected]</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>
添加一個模塊(名稱: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
注意:這也適用于較新版本的 Visual Studio,但是“添加文本檔案”中列出的一些步驟是不同的。
檔案/檔案夾結構應類似于以下內容:

用法:
Dim receiptTemplate As String = HelperLoadResource.ReadResource("ReceiptTemplate.txt")
資源:
- 如何讀取嵌入式資源文本檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496063.html
標籤:VB.net 视觉工作室 2010 视觉工作室 2012 多线串
