在visual studio中打印多个字符 [英] Printing more than a single character in visual studio

查看:135
本文介绍了在visual studio中打印多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是vb.net的新手,目前正在开发一个排队系统。我现在的问题是如何打印一个包含2个或更多字符的字符串,例如Ticket no.11,我的系统唯一输出的是样本中的第一个字符(字母T)。下面的代码是整个文件,用于保存到数据库,生成票号和打印实际票证。我不知道我是否在这里问正确的问题,无论如何,谢谢你的帮助!



我的尝试:



 Option Strict On 
Imports System.Drawing.Printing
Imports System.IO.TextReader
Imports System.Math

Public Class FrmNumApp

Dim printpsetting As New PageSettings
Dim recordDoc As New PrintDocument

Dim TextToPrint As String =

Private Sub FrmNumApp_Load(sender As System.Object,e As System.EventArgs)Handles MyBase.Load
recordDoc.PrinterSettings.PrinterName =EPSON L120 Series
End Sub

Public Sub PrintHeader()
TextToPrint =
Dim StringToPrint As String =APPROVAL
Dim LineLen As Integer = StringToPrint.Length
TextToPrint& = StringToPrint.Length& Environment.NewLine
End Sub

Public Sub PrintItems()
Dim myConn As New OleDb.OleDbConnection
Dim myCmd As New OleDb.OleDbCommand
NumApp。 ToString()
Dim connStr As String
尝试

connStr =Provider = Microsoft.ACE.OLEDB.12.0;数据源= C:\ Users \MISKRISTIAN \ Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb
myConn.ConnectionString = connStr
myCmd.Connection = myConn
myCmd.Connection.Open()
myCmd.CommandText =select * from query1
myCmd.Prepare()
myCmd.Parameters.AddWithValue(@ NumApp,NumApp.Text)
myCmd.CommandText =( SELECT TOP 1 * FROM query1 ORDER BY ID DESC,myConn)
myCmd.ExecuteNonQuery()
TextToPrint = NumApp.Text

myCmd.Connection.Close()
赶上ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub


Public Sub PrintDocument1_PrintPage(sender As Object,e As PrintPageEventArgs )Handles PrintDocument1.PrintPage
静态计数As Integer = 0
Dim textfont As Font = New Font(Arial Black,42,FontStyle.Bold)

Dim h,w As Integer
Dim left,top As Integer
with recordDoc.DefaultPageSettings
h = 100
w = 100
left = 40
top = 140
结束时

Dim lines As Integer = CInt(Math.Round(h / 1))
Dim b As New Rectangle(left,top,w,h)
Dim format As StringFormat
format = New StringFormat(StringFormatFlags.LineLimit)
Dim line,chars As Integer

e.Graphics.MeasureString(Mid(TextToPrint,count + 1),textfont,新的SizeF(w,h),格式,字符,行)
e.Graphics.DrawString(TextToPrint.Substring(count,chars),New Font(Arial Black,42,FontStyle.Bold),Brushes.Black,b,format)

count = count + chars
如果计数< TextToPrint.Length然后
e.HasMorePages = True
Else
e.HasMorePages = False
count = 0
End if
End Sub

Public Sub ConPrint()
Dim myConn As New OleDb.OleDbConnection
Dim myCmd As New OleDb.OleDbCommand
Dim connStr As String
尝试
connStr = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users\MISKRISTIAN\Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb
myConn.ConnectionString = connStr
myCmd.Connection = myConn
myCmd.Connection.Open()
myCmd.CommandText =select * from query2
myCmd.Prepare()
myCmd .Parameters.AddWithValue(@ NumCon,NumCon.Text)
myCmd.CommandText =SELECT TOP 1 * FROM query2 ORDER BY ID DESC
myCmd.ExecuteNonQuery()
myCmd.Connection.Close()
TextToPrint = NumCon.Text

Catch ex As Exception
MessageBox.Show(ex.Message)
结束尝试
End Sub

Private Sub APPROVAL_Click(sender As Object,e As EventArgs)处理APPROVAL.Click
Me.NumApp.Text = CStr(CDbl(NumApp.Text)+ 1)
Me.Label16.Text =0
Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquery As New OleDb.OleDbCommand
Dim connString As String
尝试
connString =Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users \MISKRISTIAN\Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb
sqlconn。 ConnectionString = connString
sqlquery.Connection = sqlconn
sqlconn.Open()
sqlquery.CommandText =INSERT INTO query1([Number])VALUES(@Number)
sqlquery。参数.AddWithValue(@ Number,NumApp.Text)
sqlquery.ExecuteNonQuery()
'
Catch ex As Exception
MessageBox.Show(ex.Message)
结束尝试
PrintHeader()
PrintItems()
Dim printControl = New Printing.StandardPrintController
PrintDocument1.PrintController = printControl
尝试
Dim prntdiag As New PrintDialog
如果CBool​​(prntdiag.ShowDialog)那么
PrintDocument1.Print()
结束如果
Catch ex As Exception
MsgBox(ex.Message)

结束尝试
sqlconn.Close()

结束Sub
Public Sub PrintCon_PrintPage(ByVal sender As System.Object,ByVal e As System.Drawing.Printing.PrintPageEventArgs)处理PrintCon.PrintPage
静态currentChar作为整数
Dim textfont As Font = New Font(Arial Black,36,FontStyle.Bold)

Dim h,w As Integer
Dim left,top As Integer
with recordDoc.DefaultPageSettings
h = 100
w = 100
left = 100
top = 140
结束时

TextToPrint =Approval

Dim lines As Integer = CInt(Math.Round(h / 1))
Dim b As New Rectangle(left,top,w,h)
Dim format As StringFormat
format = New StringFormat(StringFormatFlags.LineLimit)
Dim line,chars As Integer

e.Graphics.MeasureString(Mid(TextToPrint,currentChar + 1),textfont,New SizeF(w,h),format,chars,line)
e.Graphics.DrawString(TextToPrint.Substring(currentChar) ,字符),新字体(Arial Black,60,FontStyle.Bold),Brushes.Black,b,格式)

currentChar = currentChar + chars
如果currentChar< TextToPrint.Length然后
e.HasMorePages = True
Else
e.HasMorePages = False
currentChar = 0
End if

End Sub

Private Sub CONSULTATION_Click(发件人作为对象,e作为EventArgs)处理CONSULTATION.Click
Me.NumCon.Text = CStr(CDbl(NumCon.Text)+ 1)
Me .Label18.Text =0
尝试
Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquery As New OleDb.OleDbCommand
Dim connString As String
connString = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users \MISKRISTIAN \Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb
sqlconn.ConnectionString = connString
sqlquery.Connection = sqlconn
sqlconn.Open()
sqlquery.CommandText =INSERT INTO query2([Number])VALUES(@Number)
sqlquery.Parameters.AddWithValue(@ Number,NumCon.Text)
sqlquery.ExecuteNonQuery()
sqlconn.Close()
Catch ex As Exception
MessageBox.Show(ex .Message)
结束尝试
ConPrint()
Dim printControl = New Printing.StandardPrintController
PrintCon.PrintController = printControl
尝试
Dim prntdiag作为新的PrintDialog
如果CBool​​(prntdiag.ShowDialog)那么
PrintCon.Print()
结束如果
Catch ex As Exception
MsgBox(ex.Message)

结束尝试
结束子

结束类

解决方案

这是一个完整的教程:在VB.Net中打印 - VB.NET教程Dream.In.Code [ ^ ]

I'm new at vb.net and currently developing a queuing system. What my problem now is how I can print a string with 2 or more characters, for example "Ticket no. 11", The only output that my system makes is the first character in the sample(the letter "T"). The code below is the whole file for saving to the database, generating a ticket no., and for printing the actual ticket. I don't know if I'm asking the right question here, anyway thanks for any kind of help!

What I have tried:

Option Strict On
Imports System.Drawing.Printing
Imports System.IO.TextReader
Imports System.Math

Public Class FrmNumApp

    Dim printpsetting As New PageSettings
    Dim recordDoc As New PrintDocument

    Dim TextToPrint As String = ""

    Private Sub FrmNumApp_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        recordDoc.PrinterSettings.PrinterName = "EPSON L120 Series"
    End Sub

    Public Sub PrintHeader()
        TextToPrint = ""
        Dim StringToPrint As String = "APPROVAL"
        Dim LineLen As Integer = StringToPrint.Length
        TextToPrint &= StringToPrint.Length & Environment.NewLine
    End Sub

    Public Sub PrintItems()
        Dim myConn As New OleDb.OleDbConnection
        Dim myCmd As New OleDb.OleDbCommand
        NumApp.ToString()
        Dim connStr As String
        Try

            connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MISKRISTIAN\Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb"
            myConn.ConnectionString = connStr
            myCmd.Connection = myConn
            myCmd.Connection.Open()
            myCmd.CommandText = "Select * from query1"
            myCmd.Prepare()
            myCmd.Parameters.AddWithValue("@NumApp", NumApp.Text)
            myCmd.CommandText = ("SELECT TOP 1 * FROM query1 ORDER BY ID DESC", myConn)
            myCmd.ExecuteNonQuery()
            TextToPrint = NumApp.Text

            myCmd.Connection.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Public Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Static count As Integer = 0
        Dim textfont As Font = New Font("Arial Black", 42, FontStyle.Bold)

        Dim h, w As Integer
        Dim left, top As Integer
        With recordDoc.DefaultPageSettings
            h = 100
            w = 100
            left = 40
            top = 140
        End With

        Dim lines As Integer = CInt(Math.Round(h / 1))
        Dim b As New Rectangle(left, top, w, h)
        Dim format As StringFormat
        format = New StringFormat(StringFormatFlags.LineLimit)
        Dim line, chars As Integer

        e.Graphics.MeasureString(Mid(TextToPrint, count + 1), textfont, New SizeF(w, h), format, chars, line)
        e.Graphics.DrawString(TextToPrint.Substring(count, chars), New Font("Arial Black", 42, FontStyle.Bold), Brushes.Black, b, format)

        count = count + chars
        If count < TextToPrint.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            count = 0
        End If
    End Sub

    Public Sub ConPrint()
        Dim myConn As New OleDb.OleDbConnection
        Dim myCmd As New OleDb.OleDbCommand
        Dim connStr As String
        Try
            connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MISKRISTIAN\Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb"
            myConn.ConnectionString = connStr
            myCmd.Connection = myConn
            myCmd.Connection.Open()
            myCmd.CommandText = "Select * from query2"
            myCmd.Prepare()
            myCmd.Parameters.AddWithValue("@NumCon", NumCon.Text)
            myCmd.CommandText = "SELECT TOP 1 * FROM query2 ORDER BY ID DESC"
            myCmd.ExecuteNonQuery()
            myCmd.Connection.Close()
            TextToPrint = NumCon.Text

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub APPROVAL_Click(sender As Object, e As EventArgs) Handles APPROVAL.Click
        Me.NumApp.Text = CStr(CDbl(NumApp.Text) + 1)
        Me.Label16.Text = "0"
        Dim sqlconn As New OleDb.OleDbConnection
        Dim sqlquery As New OleDb.OleDbCommand
        Dim connString As String
        Try
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MISKRISTIAN\Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb"
            sqlconn.ConnectionString = connString
            sqlquery.Connection = sqlconn
            sqlconn.Open()
            sqlquery.CommandText = "INSERT INTO query1([Number])VALUES(@Number)"
            sqlquery.Parameters.AddWithValue("@Number", NumApp.Text)
            sqlquery.ExecuteNonQuery()
            '
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        PrintHeader()
        PrintItems()
        Dim printControl = New Printing.StandardPrintController
        PrintDocument1.PrintController = printControl
        Try
            Dim prntdiag As New PrintDialog
            If CBool(prntdiag.ShowDialog) Then
                PrintDocument1.Print()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
        sqlconn.Close()

    End Sub
    Public Sub PrintCon_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintCon.PrintPage
        Static currentChar As Integer
        Dim textfont As Font = New Font("Arial Black", 36, FontStyle.Bold)

        Dim h, w As Integer
        Dim left, top As Integer
        With recordDoc.DefaultPageSettings
            h = 100
            w = 100
            left = 100
            top = 140
        End With

        TextToPrint = "Approval"

        Dim lines As Integer = CInt(Math.Round(h / 1))
        Dim b As New Rectangle(left, top, w, h)
        Dim format As StringFormat
        format = New StringFormat(StringFormatFlags.LineLimit)
        Dim line, chars As Integer

        e.Graphics.MeasureString(Mid(TextToPrint, currentChar + 1), textfont, New SizeF(w, h), format, chars, line)
        e.Graphics.DrawString(TextToPrint.Substring(currentChar, chars), New Font("Arial Black", 60, FontStyle.Bold), Brushes.Black, b, format)

        currentChar = currentChar + chars
        If currentChar < TextToPrint.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            currentChar = 0
        End If

    End Sub

    Private Sub CONSULTATION_Click(sender As Object, e As EventArgs) Handles CONSULTATION.Click
        Me.NumCon.Text = CStr(CDbl(NumCon.Text) + 1)
        Me.Label18.Text = "0"
        Try
            Dim sqlconn As New OleDb.OleDbConnection
            Dim sqlquery As New OleDb.OleDbCommand
            Dim connString As String
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MISKRISTIAN\Desktop\kuwewe\queuingsystem\bin\Debug\HMOOPD_DB1.mdb"
            sqlconn.ConnectionString = connString
            sqlquery.Connection = sqlconn
            sqlconn.Open()
            sqlquery.CommandText = "INSERT INTO query2([Number])VALUES(@Number)"
            sqlquery.Parameters.AddWithValue("@Number", NumCon.Text)
            sqlquery.ExecuteNonQuery()
            sqlconn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        ConPrint()
        Dim printControl = New Printing.StandardPrintController
        PrintCon.PrintController = printControl
        Try
            Dim prntdiag As New PrintDialog
            If CBool(prntdiag.ShowDialog) Then
                PrintCon.Print()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

End Class

解决方案

Here is a complete tutorial: Printing In VB.Net - VB.NET Tutorials | Dream.In.Code[^]


这篇关于在visual studio中打印多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆