Form2 Druck funktioniert nicht!

  • VB.NET

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Form2 Druck funktioniert nicht!

    Hallo Leute!

    Bin gerade dabei, ein Programm für Hotelbuchung zu machen, komme aber mit dem Druck nicht klar der 2. Form. Kann mir jemand sagen wie das design der form2 auszusehen hat mit labels damit es vernünftig ausgedruckt wird?

    img821.imageshack.us/i/picturexa.jpg/ So sieht das 2. Formular aus.

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3. Class1.Form1 = Me
    4. ComboBox1.Items.Add("Economy")
    5. ComboBox1.Items.Add("Business")
    6. ComboBox1.Items.Add("Royal")
    7. ComboBox2.Items.Add("Economy")
    8. ComboBox2.Items.Add("Business")
    9. ComboBox2.Items.Add("Royal")
    10. ComboBox3.Items.Add("Economy")
    11. ComboBox3.Items.Add("Business")
    12. ComboBox3.Items.Add("Royal")
    13. End Sub
    14. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    15. Dim Form2 As New Form2
    16. Dim SingleRoom As Double
    17. Dim TwinRoom As Double
    18. Dim TripleRoom As Double
    19. Dim Pets As Double
    20. Dim Total As Double
    21. If ComboBox1.Text = "Choose Class" Then
    22. SingleRoom = 0.0
    23. Else
    24. If ComboBox1.Text = "Economy" Then
    25. SingleRoom = SingleR + Economy
    26. End If
    27. If ComboBox1.Text = "Business" Then
    28. SingleRoom = SingleR + Business
    29. End If
    30. If ComboBox1.Text = "Royal" Then
    31. SingleRoom = SingleR + Royal
    32. End If
    33. End If
    34. If ComboBox2.Text = "Choose Class" Then
    35. TwinRoom = 0.0
    36. Else
    37. If ComboBox2.Text = "Economy" Then
    38. TwinRoom = TwinR + Economy
    39. End If
    40. If ComboBox2.Text = "Business" Then
    41. TwinRoom = TwinR + Business
    42. End If
    43. If ComboBox2.Text = "Royal" Then
    44. TwinRoom = TwinR + Royal
    45. End If
    46. End If
    47. If ComboBox3.Text = "Choose Class" Then
    48. TripleRoom = 0.0
    49. Else
    50. If ComboBox3.Text = "Economy" Then
    51. TripleRoom = TripleR + Economy
    52. End If
    53. If ComboBox3.Text = "Business" Then
    54. TripleRoom = TripleR + Business
    55. End If
    56. If ComboBox3.Text = "Royal" Then
    57. TripleRoom = TripleR + Royal
    58. End If
    59. End If
    60. If CheckBox1.Checked = "0" Then
    61. Pets = 0.0
    62. Else
    63. Pets = 30.0
    64. End If
    65. Total = SingleRoom * Val(TextBox1.Text) + TwinRoom * Val(TextBox2.Text) + TripleRoom * Val(TextBox3.Text) + Pets
    66. If ComboBox1.Text = "Choose Class" Then
    67. Form2.Label21.Text = "-------"
    68. Form2.Label5.Text = "-----"
    69. Form2.Label6.Text = "0"
    70. ElseIf TextBox1.TextLength = "0" Then
    71. Form2.Label21.Text = "-------"
    72. Form2.Label5.Text = "-----"
    73. Form2.Label6.Text = "0"
    74. Else
    75. Form2.Label21.Text = ComboBox1.Text
    76. Form2.Label5.Text = Format(SingleRoom, "Currency")
    77. Form2.Label6.Text = TextBox1.Text
    78. End If
    79. If ComboBox2.Text = "Choose Class" Then
    80. Form2.Label20.Text = "-------"
    81. Form2.Label8.Text = "-----"
    82. Form2.Label9.Text = "0"
    83. ElseIf TextBox2.TextLength = "0" Then
    84. Form2.Label20.Text = "-------"
    85. Form2.Label8.Text = "-----"
    86. Form2.Label9.Text = "0"
    87. Else
    88. Form2.Label20.Text = ComboBox2.Text
    89. Form2.Label8.Text = Format(TwinRoom, "Currency")
    90. Form2.Label9.Text = TextBox2.Text
    91. End If
    92. If ComboBox3.Text = "Choose Class" Then
    93. Form2.Label19.Text = "-------"
    94. Form2.Label11.Text = "-----"
    95. Form2.Label12.Text = "0"
    96. ElseIf TextBox3.TextLength = "0" Then
    97. Form2.Label19.Text = "-------"
    98. Form2.Label11.Text = "-----"
    99. Form2.Label12.Text = "0"
    100. Else
    101. Form2.Label19.Text = ComboBox3.Text
    102. Form2.Label11.Text = Format(TripleRoom, "Currency")
    103. Form2.Label12.Text = TextBox3.Text
    104. End If
    105. If CheckBox1.Checked = "0" Then
    106. Form2.Label14.Text = ""
    107. Else
    108. Form2.Label14.Text = Format(Pets, "Currency")
    109. End If
    110. Form2.Label17.Text = Format(Total, "Currency")
    111. If ComboBox1.Text = "Choose Class" And ComboBox2.Text = "Choose Class" And ComboBox3.Text = "Choose Class" Then
    112. MsgBox("Choose at least one room and class!", 0)
    113. End If
    114. Form2.Show()
    115. End Sub
    116. Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    117. End Sub
    118. Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    119. End Sub
    120. End Class
    121. 5.2 Form2.vb
    122. Imports System.Drawing.Printing
    123. Public Class Form2
    124. Inherits System.Windows.Forms.Form
    125. Dim LabelArray(5, 4) As Label
    126. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    127. Class1.Form2 = Me
    128. End Sub
    129. Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
    130. End Sub
    131. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    132. Class1.Form2.Hide()
    133. End Sub
    134. Private WithEvents myDocument As PrintDocument
    135. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    136. LabelArray(1, 1) = Label1
    137. LabelArray(1, 2) = Label22
    138. LabelArray(1, 3) = Label2
    139. LabelArray(1, 4) = Label3
    140. LabelArray(2, 1) = Label4
    141. LabelArray(2, 2) = Label21
    142. LabelArray(2, 3) = Label4
    143. LabelArray(2, 4) = Label6
    144. LabelArray(3, 1) = Label7
    145. LabelArray(3, 2) = Label20
    146. LabelArray(3, 3) = Label8
    147. LabelArray(3, 4) = Label9
    148. LabelArray(4, 1) = Label13
    149. LabelArray(4, 3) = Label14
    150. LabelArray(5, 3) = Label5
    151. LabelArray(5, 4) = Label17
    152. myDocument = New PrintDocument
    153. myDocument.Print()
    154. End Sub
    155. Private Sub myDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles myDocument.PrintPage
    156. Dim myText As String
    157. Dim N As Integer = 1
    158. Dim T As New Date
    159. Dim myFont As New Font("Courier New", 12, FontStyle.Regular, GraphicsUnit.Point)
    160. e.Graphics.DrawString(Label15.Text, myFont, Brushes.Black, (50 + 50), 50)
    161. e.Graphics.DrawString(Date.Today(), myFont, Brushes.Black, (50 + 100), (50 + 24))
    162. N = N + 2
    163. myText = Label1.Text + " " + Label22.Text + " " + Label2.Text + " " + Label3.Text
    164. N = N + 2
    165. e.Graphics.DrawString(myText, myFont, Brushes.Black, 50, (50 + N * 24))
    166. myText = Label4.Text + " " + Label21.Text + " " + Label5.Text + " " + Label6.Text
    167. N = N + 1
    168. e.Graphics.DrawString(myText, myFont, Brushes.Black, 50, (50 + N * 24))
    169. myText = Label7.Text + " " + Label20.Text + " " + Label8.Text + " " + Label9.Text
    170. N = N + 1
    171. e.Graphics.DrawString(myText, myFont, Brushes.Black, 50, (50 + N * 24))
    172. myText = Label10.Text + " " + Label19.Text + " " + Label11.Text + " " + Label12.Text
    173. N = N + 1
    174. e.Graphics.DrawString(myText, myFont, Brushes.Black, 50, (50 + N * 24))
    175. myText = Label13.Text + " " + " " + Label14.Text
    176. N = N + 1
    177. e.Graphics.DrawString(myText, myFont, Brushes.Black, 50, (50 + N * 24))
    178. myText = "------------------------------------------------------------"
    179. N = N + 1
    180. e.Graphics.DrawString(myText, myFont, Brushes.Black, 50, (50 + N * 24))
    181. myText = Label16.Text + " " + " " + Label17.Text
    182. N = N + 1
    183. e.Graphics.DrawString(myText, myFont, Brushes.Black, 50, (50 + N * 24))
    184. End Sub
    185. Private Sub PrintDocument1_QueryPageSettings(ByVal sender As Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles myDocument.QueryPageSettings
    186. e.PageSettings.Landscape = False
    187. End Sub
    188. End Class
    189. 5.3 Module1.vb
    190. Module Module1
    191. Public Const Economy = 60.0
    192. Public Const Business = 100.0
    193. Public Const Royal = 160.0
    194. Public Const SingleR = 30.0
    195. Public Const TwinR = 45.0
    196. Public Const TripleR = 60.0
    197. Sub main()
    198. Dim Form2 = New Form2
    199. Form2.ShowDialog()
    200. End Sub
    201. End Module
    202. 5.4 Class1.vb
    203. Public Class Class1
    204. Public Shared Form1 As Form
    205. Public Shared Form2 As Form
    206. End Class
    Option Strict On
    Da sind doch einige Fehler im Quelltext, die Du beheben solltest.
    Arbeite mit Druck-Vorschau, da kannst Du besser experimentieren, um den Ausdruck zu optimieren:

    VB.NET-Quellcode

    1. Private Sub btnPreView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
    2. Using dlg As New System.Windows.Forms.PrintPreviewDialog
    3. dlg.Document = PrintDocument1
    4. dlg.WindowState = FormWindowState.Maximized
    5. dlg.ShowDialog()
    6. End Using
    7. End Sub
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!