Results 1 to 22 of 22

Thread: Printing help

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Printing help

    I and new to this but I have done a VB app to send inputs and receive times from a Arduino that I have programed to run a drag racing tree and timing system I want to set up a printer to print the times but I have no clue where to even start could I get some help please

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Printing help

    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Printing help

    What data do you have now? What code do you already have to keep track of the data? I assume you have times for each race?

    How do you show your data on the computer screen now? Can you print that? Is that what you want to do? Screen and print out?

    What do you want to the printout to look like? What size paper? etc. A letter? A sales slip?

    Here is a start on something. There is a data class for each race with two lanes left and right. A list is made to keep multiple events. There is only one event created in the example and shown in the print out. The example creates a sample time slip with two cars left and right lanes.

    Standard print document and preview is used to print the data as a text string.

    Of course there are many ways it could be done. Show a sample of what you want to achieve.

    Name:  a1.png
Views: 297
Size:  25.4 KB

    Code:
    Imports System.Drawing.Printing
    
    Public Class Form4
        Private WithEvents PrintDoc As New PrintDocument
        Private PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private Class TimeSlip   'class structure to group races into a list
            Public RunDateTime As Date
            Public CarNo_L As Integer
            Public Time60_L As Single
            Public Time330_L As Single
            Public TimeEigth_L As Single
            Public TimeQuart_L As Single
            Public MPH_L As Single
            Public CarNo_R As Integer
            Public Time60_R As Single
            Public Time330_R As Single
            Public TimeEigth_R As Single
            Public TimeQuart_R As Single
            Public MPH_R As Single
        End Class
        Private TimeSlips As New List(Of TimeSlip)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ts1 As New TimeSlip
            With ts1
                .RunDateTime = Now
                .CarNo_L = 42
                .Time60_L = 1.855
                .Time330_L = 5.354
                .TimeEigth_L = 8.414
                .TimeQuart_L = 13.191
                .MPH_L = 103.12
                .CarNo_R = 96
                .Time60_R = 1.528
                .Time330_R = 4.534
                .TimeEigth_R = 6.915
                .TimeQuart_R = 11.091
                .MPH_R = 100.08
                TimeSlips.Add(ts1)
            End With
    
            PrintPreviewDialog1.Document = PrintDoc
            PrintDoc.OriginAtMargins = True
    
            PrintPreviewDialog1.ShowDialog()
    
            'PrintDocH.Print()
    
        End Sub
    
    
        Private Sub PrintDoc_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
            With e.Graphics
                Using fText As New Font("consolas", 12),
                        fHeading As New Font("gill sans ultra bold", 18),
                        fScript As New Font("script mt bold", 14, FontStyle.Bold)
    
                    .DrawString("Demon's Dragstrip", fHeading, Brushes.Black, 0, 0)
                    .DrawString("Performance Nationals", fScript, Brushes.Black, 0, 30)
                    .DrawString(TimeSlips(0).RunDateTime.ToShortDateString & "   " & TimeSlips(0).RunDateTime.ToShortTimeString, fText, Brushes.Black, 0, 50)
    
                    Dim i As Integer = 0
    
                    .DrawString(vbTab & vbTab & "Left" & vbTab & vbTab & "Right" & vbLf &
                        "Car No: " & vbTab & TimeSlips(i).CarNo_L.ToString & vbTab & vbTab &
                                             TimeSlips(i).CarNo_R.ToString & vbLf &
                        "   60': " & vbTab & TimeSlips(i).Time60_L.ToString & vbTab & vbTab &
                                             TimeSlips(i).Time60_R.ToString & vbLf &
                        "  330': " & vbTab & TimeSlips(i).Time330_L.ToString & vbTab & vbTab &
                                             TimeSlips(i).Time330_R.ToString & vbLf &
                        "  1/8m: " & vbTab & TimeSlips(i).TimeEigth_L.ToString & vbTab & vbTab &
                                             TimeSlips(i).TimeEigth_R.ToString & vbLf &
                        "  1/4m: " & vbTab & TimeSlips(i).TimeQuart_L.ToString & vbTab & vbTab &
                                             TimeSlips(i).TimeQuart_R.ToString & vbLf &
                        "   MPH: " & vbTab & TimeSlips(i).MPH_L.ToString & vbTab & vbTab &
                                             TimeSlips(i).MPH_R.ToString & vbLf,
                                             fText, Brushes.Black, 0, 100)
                End Using
            End With
        End Sub
    End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Printing help

    Quote Originally Posted by JuggaloBrotha View Post
    Here's the version of that posted in our own CodeBank forum.

    http://www.vbforums.com/showthread.p...rinting-in-NET

    It's older than the Code Project one, so I'm not sure whether it may be missing some information or not.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    Here is the code i am using
    Code:
    Imports System.IO.Ports
    Public Class Form1
        Dim comPORT As String
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            comPORT = ""
            For Each sp As String In My.Computer.Ports.SerialPortNames
                comPORT_comboBox.Items.Add(sp)
            Next
    
        End Sub
    
        Private Sub comPORT_comboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPORT_comboBox.SelectedIndexChanged
            If (comPORT_comboBox.SelectedItem <> "") Then
                comPORT = comPORT_comboBox.SelectedItem
            End If
        End Sub
    
        Private Sub connect_Btn_Click(sender As Object, e As EventArgs) Handles connect_Btn.Click
            If (connect_Btn.Text = "Connect") Then
                If (comPORT <> "") Then
                    SerialPort1.Close()
                    SerialPort1.PortName = comPORT
                    SerialPort1.BaudRate = 115200
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = Parity.None
                    SerialPort1.StopBits = StopBits.One
                    SerialPort1.Handshake = Handshake.None
                    SerialPort1.Encoding = System.Text.Encoding.Default
                    SerialPort1.ReadTimeout = 10000
                    SerialPort1.Open()
                    connect_Btn.Text = "Dis-connect"
                Else
                    MsgBox("Select a COM Port First")
                End If
            Else
                SerialPort1.Close()
                connect_Btn.Text = "Connect"
    
            End If
    
        End Sub
    
        Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click
            If BtnIG.Checked = True Then
                SerialPort1.Write("I")
                TextBox1.Text = ""
            ElseIf BtnPT.Checked = True Then
                SerialPort1.Write("P")
                TextBox1.Text = ""
            ElseIf BtnNPT.Checked = True Then
                SerialPort1.Write("NP")
                TextBox1.Text = ""
            ElseIf BtnST.Checked = True Then
                SerialPort1.Write("S")
                TextBox1.Text = ""
            End If
        End Sub
    
    
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles AutoWin.CheckedChanged
            If AutoWin.Checked Then
                SerialPort1.Write("AW")
            Else
                SerialPort1.Write("aw")
            End If
    
        End Sub
    
        Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Try
                Dim mydata As String
                mydata = SerialPort1.ReadLine()
    
                If TextBox1.InvokeRequired Then
                    TextBox1.Invoke(DirectCast(Sub() TextBox1.Text &= mydata, MethodInvoker))
                Else
                    TextBox1.Text &= mydata
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            Dim s As String
    
            s = TextBox1.Text + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + ""
    
            Dim timeString() As String
            timeString = s.Split(New Char() {","c})
            reacL.Text = TimeString(2)
            sixtyL.Text = TimeString(3)
            mphL.Text = TimeString(4)
            etL.Text = TimeString(5)
            reacR.Text = TimeString(6)
            sixtyR.Text = TimeString(7)
            mphR.Text = TimeString(8)
            etR.Text = TimeString(9)
    
            winl.Visible = If(timeString(10) < timeString(11), True, False)
            winr.Visible = If(timeString(10) > timeString(11), True, False)
            jumpl.Visible = If(timeString(0) > timeString(1), True, False)
            jumpr.Visible = If(timeString(0) < timeString(1), True, False)
    
    
        End Sub
    
        Private Sub BtnRightWin_Click(sender As Object, e As EventArgs) Handles BtnRightWin.Click
            SerialPort1.Write("Rwin")
        End Sub
    
        Private Sub BtnLeftWin_Click(sender As Object, e As EventArgs) Handles BtnLeftWin.Click
            SerialPort1.Write("Lwin")
        End Sub
    End Class
    Name:  Screenshot (1).jpg
Views: 248
Size:  18.9 KB
    Last edited by 11mike23; Jun 10th, 2019 at 07:11 PM.

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Printing help

    Quote Originally Posted by 11mike23 View Post
    Here is the code i am using
    Code:
    Imports System.IO.Ports
    Public Class Form1
        Dim comPORT As String
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            comPORT = ""
            For Each sp As String In My.Computer.Ports.SerialPortNames
                comPORT_comboBox.Items.Add(sp)
            Next
    
        End Sub
    
        Private Sub comPORT_comboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPORT_comboBox.SelectedIndexChanged
            If (comPORT_comboBox.SelectedItem <> "") Then
                comPORT = comPORT_comboBox.SelectedItem
            End If
        End Sub
    
        Private Sub connect_Btn_Click(sender As Object, e As EventArgs) Handles connect_Btn.Click
            If (connect_Btn.Text = "Connect") Then
                If (comPORT <> "") Then
                    SerialPort1.Close()
                    SerialPort1.PortName = comPORT
                    SerialPort1.BaudRate = 115200
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = Parity.None
                    SerialPort1.StopBits = StopBits.One
                    SerialPort1.Handshake = Handshake.None
                    SerialPort1.Encoding = System.Text.Encoding.Default
                    SerialPort1.ReadTimeout = 10000
                    SerialPort1.Open()
                    connect_Btn.Text = "Dis-connect"
                Else
                    MsgBox("Select a COM Port First")
                End If
            Else
                SerialPort1.Close()
                connect_Btn.Text = "Connect"
    
            End If
    
        End Sub
    
        Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click
            If BtnIG.Checked = True Then
                SerialPort1.Write("I")
                TextBox1.Text = ""
            ElseIf BtnPT.Checked = True Then
                SerialPort1.Write("P")
                TextBox1.Text = ""
            ElseIf BtnNPT.Checked = True Then
                SerialPort1.Write("NP")
                TextBox1.Text = ""
            ElseIf BtnST.Checked = True Then
                SerialPort1.Write("S")
                TextBox1.Text = ""
            End If
        End Sub
    
    
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles AutoWin.CheckedChanged
            If AutoWin.Checked Then
                SerialPort1.Write("AW")
            Else
                SerialPort1.Write("aw")
            End If
    
        End Sub
    
        Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Try
                Dim mydata As String
                mydata = SerialPort1.ReadLine()
    
                If TextBox1.InvokeRequired Then
                    TextBox1.Invoke(DirectCast(Sub() TextBox1.Text &= mydata, MethodInvoker))
                Else
                    TextBox1.Text &= mydata
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            Dim s As String
    
            s = TextBox1.Text + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + ""
    
            Dim timeString() As String
            timeString = s.Split(New Char() {","c})
            reacL.Text = TimeString(2)
            sixtyL.Text = TimeString(3)
            mphL.Text = TimeString(4)
            etL.Text = TimeString(5)
            reacR.Text = TimeString(6)
            sixtyR.Text = TimeString(7)
            mphR.Text = TimeString(8)
            etR.Text = TimeString(9)
    
            winl.Visible = If(timeString(10) < timeString(11), True, False)
            winr.Visible = If(timeString(10) > timeString(11), True, False)
            jumpl.Visible = If(timeString(0) > timeString(1), True, False)
            jumpr.Visible = If(timeString(0) < timeString(1), True, False)
    
    
        End Sub
    
        Private Sub BtnRightWin_Click(sender As Object, e As EventArgs) Handles BtnRightWin.Click
            SerialPort1.Write("Rwin")
        End Sub
    
        Private Sub BtnLeftWin_Click(sender As Object, e As EventArgs) Handles BtnLeftWin.Click
            SerialPort1.Write("Lwin")
        End Sub
    End Class
    I don't see anything to do with printing in your code here, can you post the code you have so we can help troubleshoot it?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    I don’t have anything coded to print I have no clue where to even start I have worked on this project for about a year now just to get what I have now and get everything working

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Printing help

    Quote Originally Posted by 11mike23 View Post
    I have no clue where to even start
    Hopefully you now know that the tutorial that a couple of us linked to is the place to start.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    I’ll look through everything and see what I can figure out

  10. #10
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Printing help

    Quote Originally Posted by 11mike23 View Post
    I’ll look through everything and see what I can figure out
    Oh yeah I see. Thats a good start! I like the red and green buttons.

    Your TimeString array has the information similar to what I set up in the timeslip class of the example I posted. Your code now puts the data from the TimeString array into the textboxes.

    All you need to do is print the TimeString data array that you already have. Similar to the example I posted. You need to change the example variables to your own TimeString() data variables. So you can remove the timeSlip class in the example and just use the TimeString() you already use to fill the textboxes.

    See where the data is assembled into a string to print in the PrintPage Sub of the example? You just need to modify that to use the data that now shows in the text boxes.

    That is what you want to do correct? Print the data in the textboxes? And whatever else you want to add.

    You can add a print button to your form to run the preview and print code (now in form load of the example).

    Study the basic printing info documents referenced and then try it. Try to make a simple example of your own just print "Hello World" or something to start. Search for info on the key words.

    Come back with any questions.
    Last edited by tommytwotrain; Jun 11th, 2019 at 05:45 AM.

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    OK this is what i have now at least it will print something but i want it set it up to look something like the post above but i am not sure what i need to do next to set the page up and print the data in that layout

    Code:
    Imports System.IO.Ports
    Public Class Form1
        Dim comPORT As String
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            comPORT = ""
            For Each sp As String In My.Computer.Ports.SerialPortNames
                comPORT_comboBox.Items.Add(sp)
            Next
    
        End Sub
    
        Private Sub comPORT_comboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPORT_comboBox.SelectedIndexChanged
            If (comPORT_comboBox.SelectedItem <> "") Then
                comPORT = comPORT_comboBox.SelectedItem
            End If
        End Sub
    
        Private Sub connect_Btn_Click(sender As Object, e As EventArgs) Handles connect_Btn.Click
            If (connect_Btn.Text = "Connect") Then
                If (comPORT <> "") Then
                    SerialPort1.Close()
                    SerialPort1.PortName = comPORT
                    SerialPort1.BaudRate = 115200
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = Parity.None
                    SerialPort1.StopBits = StopBits.One
                    SerialPort1.Handshake = Handshake.None
                    SerialPort1.Encoding = System.Text.Encoding.Default
                    SerialPort1.ReadTimeout = 10000
                    SerialPort1.Open()
                    connect_Btn.Text = "Dis-connect"
                Else
                    MsgBox("Select a COM Port First")
                End If
            Else
                SerialPort1.Close()
                connect_Btn.Text = "Connect"
    
            End If
    
        End Sub
    
        Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click
            If BtnIG.Checked = True Then
                SerialPort1.Write("I")
                TextBox1.Text = ""
            ElseIf BtnPT.Checked = True Then
                SerialPort1.Write("P")
                TextBox1.Text = ""
            ElseIf BtnNPT.Checked = True Then
                SerialPort1.Write("NP")
                TextBox1.Text = ""
            ElseIf BtnST.Checked = True Then
                SerialPort1.Write("S")
                TextBox1.Text = ""
            End If
        End Sub
    
    
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles AutoWin.CheckedChanged
            If AutoWin.Checked Then
                SerialPort1.Write("AW")
            Else
                SerialPort1.Write("aw")
            End If
    
        End Sub
    
        Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Try
                Dim mydata As String
                mydata = SerialPort1.ReadLine()
    
                If TextBox1.InvokeRequired Then
                    TextBox1.Invoke(DirectCast(Sub() TextBox1.Text &= mydata, MethodInvoker))
                Else
                    TextBox1.Text &= mydata
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            Dim s As String
    
            s = TextBox1.Text + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + ""
    
            Dim timeString() As String
            timeString = s.Split(New Char() {","c})
            reacL.Text = timeString(2)
            sixtyL.Text = TimeString(3)
            mphL.Text = TimeString(4)
            etL.Text = TimeString(5)
            reacR.Text = timeString(6)
            sixtyR.Text = TimeString(7)
            mphR.Text = TimeString(8)
            etR.Text = timeString(9)
    
    
    
    
            winl.Visible = If(timeString(10) < timeString(11), True, False)
            winr.Visible = If(timeString(10) > timeString(11), True, False)
            jumpl.Visible = If(timeString(0) > timeString(1), True, False)
            jumpr.Visible = If(timeString(0) < timeString(1), True, False)
    
    
        End Sub
    
        Private Sub BtnRightWin_Click(sender As Object, e As EventArgs) Handles BtnRightWin.Click
            SerialPort1.Write("Rwin")
        End Sub
    
        Private Sub BtnLeftWin_Click(sender As Object, e As EventArgs) Handles BtnLeftWin.Click
            SerialPort1.Write("Lwin")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Print.Click
            PrintDocument1.Print()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Print_prev.Click
            If TextBox1.Text = "" Then
                MsgBox("No Data")
            Else
                PrintPreviewDialog1.ShowDialog()
            End If
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim font1 As New Font("arial", 28, FontStyle.Regular)
            e.Graphics.DrawString(TextBox1.Text, font1, Brushes.Black, 100, 100)
        End Sub
    End Class
    Name:  Screenshot (2).jpg
Views: 207
Size:  23.3 KB

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    Would this be my page layout if so how do i incorporate this instead of the text box in my code
    Code:
     Private Sub PrintDoc_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
            With e.Graphics
                Using fText As New Font("consolas", 12),
                        fHeading As New Font("gill sans ultra bold", 18),
                        fScript As New Font("script mt bold", 14, FontStyle.Bold)
    
                    .DrawString("336 Dragway", fHeading, Brushes.Black, 0, 0)
                    .DrawString(" ", fScript, Brushes.Black, 0, 30)
                    .DrawString(TimeSlips(0).RunDateTime.ToShortDateString & "   " & TimeSlips(0).RunDateTime.ToShortTimeString, fText, Brushes.Black, 0, 50)
    
                    .DrawString("Left" & vbTab & vbTab & vbTab & vbTab & vbTab & "Right" & vbLf &
                        TimeString(2) & vbTab & vbTab & "R/T" & vbTab & vbTab & TimeString(6) & vbLf &
                        TimeString(3) & vbTab & vbTab & "60'" & vbTab & vbTab & TimeString(7) & vbLf &
                        TimeString(4) & vbTab & vbTab & "MPH" & vbTab & vbTab & TimeString(8) & vbLf &
                        TimeString(5) & vbTab & vbTab & "ET" & vbTab & vbTab & TimeString(9) & vbLf,
                                             fText, Brushes.Black, 0, 100)
                End Using
            End With
        End Sub
    i also need to change the date and time

  13. #13
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Printing help

    Quote Originally Posted by 11mike23 View Post
    Would this be my page layout if so how do i incorporate this instead of the text box in my code
    Code:
     Private Sub PrintDoc_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
            With e.Graphics
                Using fText As New Font("consolas", 12),
                        fHeading As New Font("gill sans ultra bold", 18),
                        fScript As New Font("script mt bold", 14, FontStyle.Bold)
    
                    .DrawString("336 Dragway", fHeading, Brushes.Black, 0, 0)
                    .DrawString(" ", fScript, Brushes.Black, 0, 30)
                    .DrawString(TimeSlips(0).RunDateTime.ToShortDateString & "   " & TimeSlips(0).RunDateTime.ToShortTimeString, fText, Brushes.Black, 0, 50)
    
                    .DrawString("Left" & vbTab & vbTab & vbTab & vbTab & vbTab & "Right" & vbLf &
                        TimeString(2) & vbTab & vbTab & "R/T" & vbTab & vbTab & TimeString(6) & vbLf &
                        TimeString(3) & vbTab & vbTab & "60'" & vbTab & vbTab & TimeString(7) & vbLf &
                        TimeString(4) & vbTab & vbTab & "MPH" & vbTab & vbTab & TimeString(8) & vbLf &
                        TimeString(5) & vbTab & vbTab & "ET" & vbTab & vbTab & TimeString(9) & vbLf,
                                             fText, Brushes.Black, 0, 100)
                End Using
            End With
        End Sub
    i also need to change the date and time
    Yes, very good, that looks correct without detailed study. Run it and see if it works or not. There are several ways it can be done. The array can be used to print or you can use the text boxes.

    I made this example from the text boxes. See the form with the textboxes and the times in the textboxe.text on the form? Then see in the print page handler how the time table is made into a string and printed?

    So that is more or less the same as what you have done there. One can eithere use sixtyL.Text or TimeString(3) or "1.855" to print. Its all the same mostly.


    In the previous example that you copied I put the NOW time in the array at TimeSlips(0). However your array appears to have different info there. So you need to deal with that somehow. Do you want to save the time in your data array then do that.

    The NOW date/time give the current system time. Is that what you want?

    The key is to learn the different ways things can be done, one at a time, then figure the best and easiest ways to do what you want using the tools you have learned.

    Below is another example I had already made. This is the form made by the example with the textboxes and data from the example.

    Name:  a2.png
Views: 199
Size:  14.7 KB

    THis is the printout made using the data in the textboxes. You can also use the array as you have shown.

    Name:  a1.png
Views: 205
Size:  32.5 KB

    Here is the example code. The code makes the controls. Just cut and paste to an empty form. Change the form name as required.

    Code:
    Imports System.Drawing.Printing
    
    Public Class Form5
        Private WithEvents PrintDoc As New PrintDocument
        Private PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private WithEvents reacL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 60)}
        Private WithEvents sixtyL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 100)}
        Private WithEvents mphL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 140)}
        Private WithEvents etL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 180)}
        Private WithEvents quarterL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 220)}
    
        Private WithEvents reacR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 60)}
        Private WithEvents sixtyR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 100)}
        Private WithEvents mphR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 140)}
        Private WithEvents etR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 180)}
        Private WithEvents quarterR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 220)}
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'fill the textboxes with data for two cars
            Dim TimeString(12) As String
            TimeString(2) = "0.000"
            TimeString(3) = "1.855"
            TimeString(4) = "103.12"
            TimeString(5) = "8.414"
            TimeString(10) = "13.191"
    
            reacL.Text = TimeString(2)
            sixtyL.Text = TimeString(3)
            mphL.Text = TimeString(4)
            etL.Text = TimeString(5)
            quarterL.Text = TimeString(10)
    
            TimeString(6) = "0.000"
            TimeString(7) = "1.528"
            TimeString(8) = "100.08"
            TimeString(9) = "6.915"
            TimeString(11) = "11.091"
    
            reacR.Text = TimeString(6)
            sixtyR.Text = TimeString(7)
            mphR.Text = TimeString(8)
            etR.Text = TimeString(9)
            quarterR.Text = TimeString(11)
    
            'show the print preview dialog
            PrintPreviewDialog1.Document = PrintDoc
            PrintDoc.OriginAtMargins = True
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub PrintDoc_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
            With e.Graphics
                Using fText As New Font("consolas", 12),
                        fHeading As New Font("gill sans ultra bold", 18),
                        fScript As New Font("script mt bold", 14, FontStyle.Bold)
    
                    'draw the heading at coordinates 0,0 upper left of margins
                    .DrawString("Mike's Dragstrip", fHeading, Brushes.Black, 0, 0)
                    .DrawString("Performance Nationals", fScript, Brushes.Black, 0, 30)
    
                    'make a text string of the times into two columns and multiple lines
                    'vbtab is like the old tab to produce column spacing between words
                    'vblf starts a new line at the left column
                    .DrawString("Left" & vbTab & vbTab & "Right" & vbLf & vbLf &
                                reacL.Text & vbTab & vbTab & reacR.Text & vbLf &
                                sixtyL.Text & vbTab & vbTab & sixtyR.Text & vbLf &
                                mphL.Text & vbTab & vbTab & mphR.Text & vbLf &
                                etL.Text & vbTab & vbTab & etR.Text & vbLf &
                                quarterL.Text & vbTab & vbTab & quarterR.Text,
                                fText, Brushes.Black, 0, 100)
                End Using
            End With
        End Sub
    
    End Class

  14. #14

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    Ok how do I add either my test code or the way you have coded it on to my current code to work off the print button I added like I had said before I am new to all this so I don't know much about it but i am trying to learn

  15. #15
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Printing help

    Quote Originally Posted by 11mike23 View Post
    Ok how do I add either my test code or the way you have coded it on to my current code to work off the print button I added like I had said before I am new to all this so I don't know much about it but i am trying to learn
    Yes it is hard and frustrating at first. You need to learn one thing at a time. Practice, debug, change, and learn it.

    Then you can learn something new and add it to what you previously learned.

    In the example below I added code to make a button control. You can just make controls in the form designer. I use code so it is easy to reproduce the posted example and see exactly how it is set up.

    So add a button to your form and move the print preview dialog code from the form load event to the button click event. Now the form loads and initializes the text box data. Then you click the button to print.

    See the print button? See the code in the Printbutton click event at the bottom?

    Name:  a2.png
Views: 191
Size:  15.1 KB


    Code:
    Imports System.Drawing.Printing
    
    Public Class Form5
        Private WithEvents PrintDoc As New PrintDocument
        Private PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private WithEvents PrintButton As New Button With {.Parent = Me, .Location = New Point(200, 200),
            .Text = "Print..."}
    
        Private WithEvents reacL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 60)}
        Private WithEvents sixtyL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 100)}
        Private WithEvents mphL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 140)}
        Private WithEvents etL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 180)}
        Private WithEvents quarterL As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(20, 220)}
    
        Private WithEvents reacR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 60)}
        Private WithEvents sixtyR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 100)}
        Private WithEvents mphR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 140)}
        Private WithEvents etR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 180)}
        Private WithEvents quarterR As New TextBox With {.Parent = Me, .Size = New Size(60, 30), .Location = New Point(100, 220)}
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'fill the textboxes with data for two cars
            Dim TimeString(12) As String
            TimeString(2) = "0.000"
            TimeString(3) = "1.855"
            TimeString(4) = "103.12"
            TimeString(5) = "8.414"
            TimeString(10) = "13.191"
    
            reacL.Text = TimeString(2)
            sixtyL.Text = TimeString(3)
            mphL.Text = TimeString(4)
            etL.Text = TimeString(5)
            quarterL.Text = TimeString(10)
    
            TimeString(6) = "0.000"
            TimeString(7) = "1.528"
            TimeString(8) = "100.08"
            TimeString(9) = "6.915"
            TimeString(11) = "11.091"
    
            reacR.Text = TimeString(6)
            sixtyR.Text = TimeString(7)
            mphR.Text = TimeString(8)
            etR.Text = TimeString(9)
            quarterR.Text = TimeString(11)
    
    
        End Sub
    
        Private Sub PrintDoc_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
            With e.Graphics
                Using fText As New Font("consolas", 12),
                        fHeading As New Font("gill sans ultra bold", 18),
                        fScript As New Font("script mt bold", 14, FontStyle.Bold)
    
                    'draw the heading at coordinates 0,0 upper left of margins
                    .DrawString("Mike's Dragstrip", fHeading, Brushes.Black, 0, 0)
                    .DrawString("Performance Nationals", fScript, Brushes.Black, 0, 30)
    
                    'make a text string of the times into two columns and multiple lines
                    'vbtab is like the old tab to produce column spacing between words
                    'vblf starts a new line at the left column
                    .DrawString("Left" & vbTab & vbTab & "Right" & vbLf & vbLf &
                                reacL.Text & vbTab & vbTab & reacR.Text & vbLf &
                                sixtyL.Text & vbTab & vbTab & sixtyR.Text & vbLf &
                                mphL.Text & vbTab & vbTab & mphR.Text & vbLf &
                                etL.Text & vbTab & vbTab & etR.Text & vbLf &
                                quarterL.Text & vbTab & vbTab & quarterR.Text,
                                fText, Brushes.Black, 0, 100)
                End Using
            End With
        End Sub
    
        Private Sub PrintButton_Click(sender As Object, e As EventArgs) Handles PrintButton.Click
            'show the print preview dialog
            PrintPreviewDialog1.Document = PrintDoc
            PrintDoc.OriginAtMargins = True
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    End Class

  16. #16
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

  17. #17

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    Yes I looked through both links but is there any way to add this to my main form or screen and use the print button I added or does it have to be split to different form like you have done.

  18. #18
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Printing help

    Quote Originally Posted by 11mike23 View Post
    Yes I looked through both links but is there any way to add this to my main form or screen and use the print button I added or does it have to be split to different form like you have done.
    Yes you can add everything into one form. Use the form you have or... sometimes... it is good to just start a new project and start over and make it all again. Copy and paste as you can, and understand it.

    It is good practice to start over. Make a new project. Make a new form. Add the controls again. Add the code again. Then you will remember it better. You will probably realize a better way and improve what you have the second time you do it!

    Or not.

    Btw are you making backups of your project in case something goes south?

    Plus, best to ask a new question if it is a different subject. You will get others helping again with new ideas.

    I am going to be gone for a while...

  19. #19

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    thanks for your help i think have every thing working i get a print prev on what i wont just waiting on the printer its a 80mm thermal printer do i need to do some kind of page setup or just make sure it is scaled right in my print layout
    Code:
     Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            RunDateTime = Now
    
            With e.Graphics
                Using fText As New Font("consolas", 12),
            fHeading As New Font("Impact", 25),
            fScript As New Font("Segoe Script", 14)
    
                    .DrawString("336 Dragway", fHeading, Brushes.Black, 65, 0)
                    .DrawString("Burlington, NC", fScript, Brushes.Black, 75, 35)
                    .DrawString(RunDateTime.ToShortDateString & "   " & RunDateTime.ToShortTimeString, fText, Brushes.Black, 65, 60)
                    .DrawString("Left" & vbTab & vbTab & vbTab & vbTab & "Right" & vbLf &
             reacL.Text & vbTab & vbTab & "R/T" & vbTab & vbTab & reacR.Text & vbLf &
             sixtyL.Text & vbTab & vbTab & "60'" & vbTab & vbTab & sixtyR.Text & vbLf &
             mphL.Text & vbTab & vbTab & "MPH" & vbTab & vbTab & mphR.Text & vbLf &
             etL.Text & vbTab & vbTab & "ET" & vbTab & vbTab & etR.Text & vbLf,
            fText, Brushes.Black, 0, 100)
                End Using
            End With
        End Sub

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Printing help

    May I recommend that you acquaint yourself with the String.Format method and also with string interpolation, and even the String.Concat method. Using loads of string concatenation operators (&) makes code hard to read and thus error prone. I generally recommend string interpolation these days, although insertion of a repeated substring, as you're doing with the Tab character here, can make String.Format a better option. It's also worth noting that VB supports multiline String literals these days.
    vb.net Code:
    1. .DrawString($"{RunDateTime:d}   {RunDateTime:t}", fText, Brushes.Black, 65, 60)
    2. .DrawString($"Left{vbTab}{vbTab}{vbTab}{vbTab}Right
    3. {reacL.Text}{vbTab}{vbTab}R/T{vbTab}{vbTab}{reacR.Text}
    4. {sixtyL.Text}{vbTab}{vbTab}60'{vbTab}{vbTab}{sixtyR.Text}
    5. {mphL.Text}{vbTab}{vbTab}MPH{vbTab}{vbTab}{mphR.Text}
    6. {etL.Text}{vbTab}{vbTab}ET{vbTab}{vbTab}{etR.Text}
    7. ",
    8.             fText,
    9.             Brushes.Black,
    10.             0,
    11.             100)
    You can, of course, make that clearer by using a variable for things that you use often, which you should ALWAYS do:
    vb.net Code:
    1. Dim twoTabs = ControlChars.Tab & ControlChars.Tab
    2. Dim fourTabs = twoTabs & twoTabs
    3.  
    4. .DrawString($"Left{fourTabs}Right
    5. {reacL.Text}{twoTabs}R/T{twoTabs}{reacR.Text}
    6. {sixtyL.Text}{twoTabs}60'{twoTabs}{sixtyR.Text}
    7. {mphL.Text}{twoTabs}MPH{twoTabs}{mphR.Text}
    8. {etL.Text}{twoTabs}ET{twoTabs}{etR.Text}
    9. ",
    10.             fText,
    11.             Brushes.Black,
    12.             0,
    13.             100)

  21. #21
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Printing help

    Quote Originally Posted by 11mike23 View Post
    its a 80mm thermal printer do i need to do some kind of page setup or just make sure it is scaled right in my print layout
    Yes you need to adjust the printout drawing code to fit that paper size. To draw the receipt that size you may need to use a smaller font and abbreviate more. Set margins to zero. May need to use a monotype font like Consolas which gives uniform letter size and spacing. Experiment.

    The printer and the paper size needs to be set for the PrintDocument.PrinterSettings using several possible ways. The user can setup the printer and paper using the application dialogs (and code you write). Or the application can use the default windows setup for the printer (also set by user through windows). If you know you have a fixed setup you can have the application specify printer and paper or use default.

    For now you can use the default settings for the printer which you set in windows printer setup before you start the app. Then the PrintDocument is set to the current windows defaults when the application is started.

    How do you want it to work? User set the printer and paper or the application sets it?

  22. #22

    Thread Starter
    Member
    Join Date
    Jun 2019
    Posts
    34

    Re: Printing help

    I don't really care it will be the only printer and only used for these time slips

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width