Related
my problem here is how to print it on multiple pages, page counts are depends on the user input on textbox9. please help me. thanks
Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
PrintDocument1.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("Paper Size Name", 300, 300)
Dim CT As Date = Now ' Use of Now.
TextBox17.Text = (CT.ToShortDateString)
Dim font1 As New Font("arial", 6, FontStyle.Bold)
Dim font2 As New Font("arial", 10, FontStyle.Bold)
Dim myBitmap As New System.Drawing.Bitmap(filename:="C:\Users\jtapellido\Desktop\sticker pictures\slogo1.png")
Dim index As Integer = 1
Do
e.Graphics.DrawImage(image:=myBitmap, point:=New Point(70, 20))
e.Graphics.DrawString("QUARANTINE", font2, Brushes.Black, 70, 40)
e.Graphics.DrawString("MATERIAL NAME:_________________________________", font1, Brushes.Black, 70, 60)
e.Graphics.DrawString("QC No:_____________", font1, Brushes.Black, 70, 80)
e.Graphics.DrawString("LOT No:____________", font1, Brushes.Black, 70, 100)
e.Graphics.DrawString("DATE RECEIVED:______________", font1, Brushes.Black, 170, 80)
e.Graphics.DrawString("EXPIRATION DATE:____________", font1, Brushes.Black, 170, 100)
e.Graphics.DrawString("QUANTITY:____________", font1, Brushes.Black, 70, 120)
e.Graphics.DrawString("VENDOR/MANUFATURER:", font1, Brushes.Black, 70, 140)
e.Graphics.DrawString("________________________________________________", font1, Brushes.Black, 70, 160)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 70, 180)
e.Graphics.DrawString("STORAGE:_______________________________________", font1, Brushes.Black, 70, 200)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 70, 220)
e.Graphics.DrawString("PREPARED BY/DATE:", font1, Brushes.Black, 70, 240)
e.Graphics.DrawString("CHECKED BY/DATE:", font1, Brushes.Black, 190, 240)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 70, 255)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 190, 255)
e.Graphics.DrawString(TextBox1.Text, font1, Brushes.Black, 145, 58)
e.Graphics.DrawString(TextBox2.Text, font1, Brushes.Black, 110, 78)
e.Graphics.DrawString(TextBox3.Text, font1, Brushes.Black, 110, 98)
e.Graphics.DrawString(TextBox4.Text, font1, Brushes.Black, 255, 78)
e.Graphics.DrawString(TextBox5.Text, font1, Brushes.Black, 255, 98)
e.Graphics.DrawString(TextBox6.Text, font1, Brushes.Black, 120, 118)
'e.Graphics.DrawString("VENDOR/MANUFATURER:", font1, Brushes.Black, 10, 120)
e.Graphics.DrawString(TextBox7.Text, font1, Brushes.Black, 70, 158)
e.Graphics.DrawString(index, font1, Brushes.Black, 170, 178)
e.Graphics.DrawString(TextBox9.Text, font1, Brushes.Black, 255, 178)
e.Graphics.DrawString(TextBox11.Text, font1, Brushes.Black, 120, 198)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 120, 218)
'e.Graphics.DrawString("PREPARED BY/DATE:", font1, Brushes.Black, 10, 220)
'e.Graphics.DrawString("CHECKED BY/DATE:", font1, Brushes.Black, 120, 220)
e.Graphics.DrawString(TextBox15.Text + " " + TextBox17.Text, font1, Brushes.Black, 70, 253)
e.Graphics.DrawString(ComboBox2.Text + " " + TextBox17.Text, font1, Brushes.Black, 190, 253)
'e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
index += 1
Loop Until index > TextBox9.text
End Sub
The logic for printing multiple pages is pretty simple. The PrintPage event handler does just what the name suggests, i.e. it prints a page. At the end of the method, you set e.HasMorePages to True if there are more pages to print and that's it. The event will be raised again and another page printed.
It's up to you to provide logic at the start of the method to determine what page you're printing and at the end to determine whether there are more pages to print. Because you need to remember a state between calls to that method, you'll likely store the relevant data in one or more fields.
Here is an example of printing record from a list, ten to a page:
Private allData As New List(Of String)
Private dataToPrint As Queue(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Start printing.
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
'Create a new queue containing all the current data.
dataToPrint = New Queue(Of String)(allData)
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Print a maximum of 10 records.
For i = 1 To Math.Min(dataToPrint.Count, 10)
e.Graphics.DrawString(dataToPrint.Dequeue(),
Font,
Brushes.Black,
25,
i * 25)
Next
'Keep printing if and only if there are more records to print.
e.HasMorePages = (dataToPrint.Count > 0)
End Sub
Private Sub PrintDocument1_EndPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.EndPrint
dataToPrint = Nothing
End Sub
If you wanted to print a specific number of pages based on the contents of a TextBox then you might convert that contents to an Integer and assign that to a field at the start. You could then decrement that variable at the end of the PrintPage event handler and print another page if and only if that counter is greater than zero.
I know how to rotate a text or a rectangle using the top-sx corner as pivot. For example:
Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
Dim g As Graphics = Panel1.CreateGraphics()
Dim font As Font = New Font("Arial", 42, FontStyle.Regular, GraphicsUnit.Pixel)
Dim i As Single
For i = 0 To 255 Step 30
Dim myBrush As SolidBrush = New SolidBrush(Color.FromArgb(255, i, 255 - i, i)) 'Green to violet
'Draw a string and a Rectangle of the same size
Dim stringSize As SizeF = g.MeasureString("Hello", font)
g.TranslateTransform(200, 200)
g.RotateTransform(-i)
g.DrawString("Hello", font, myBrush, 0, 0)
g.DrawRectangle(New Pen(Color.FromArgb(50, 255, 0, 0), 1), 0, 0, stringSize.Width, stringSize.Height)
g.ResetTransform()
myBrush.Dispose()
Next
'Draw the center of the rotation
g.DrawRectangle(Pens.Black, 200 - 5, 200 - 5, 10, 10)
g.Dispose()
End Sub
With this code I have the following output:
How can I rotate my graphic elements by using the bottom-sx corner as pivot?
Replace as follows:
Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
Dim g As Graphics = Panel1.CreateGraphics()
Dim font As Font = New Font("Arial", 42, FontStyle.Regular, GraphicsUnit.Pixel)
Dim i As Single
For i = 0 To 255 Step 30
Dim myBrush As SolidBrush = New SolidBrush(Color.FromArgb(255, CInt(i), CInt(255 - i), CInt(i))) 'Green to violet
'Draw a string and a Rectangle of the same size
Dim stringSize As SizeF = g.MeasureString("Hello", font)
g.TranslateTransform(200, 200)
g.RotateTransform(-i)
Dim coorX As Single = 0
Dim coorY As Single = -stringSize.Height
g.DrawString("Hello", font, myBrush, coorX, coorY)
g.DrawRectangle(New Pen(Color.FromArgb(50, 255, 0, 0), 1), coorX, coorY, stringSize.Width, stringSize.Height)
g.ResetTransform()
myBrush.Dispose()
Next
'Draw the center of the rotation
g.DrawRectangle(Pens.Black, 200 - 5, 200 - 5, 10, 10)
g.Dispose()
End Sub
And here the same version optimized as Memory and Code Friendly:
Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
Using g As Graphics = Panel1.CreateGraphics()
Using font As Font = New Font("Arial", 42, FontStyle.Regular, GraphicsUnit.Pixel)
'Draw a string and a Rectangle of the same size
Dim stringSize As SizeF = g.MeasureString("Hello", font)
Dim coorX As Single = 0
Dim coorY As Single = -stringSize.Height
For i As Integer = 0 To 255 Step 30
Using myBrush As SolidBrush = New SolidBrush(Color.FromArgb(255, CInt(i), CInt(255 - i), CInt(i))) 'Green to violet
g.TranslateTransform(200, 200)
g.RotateTransform(-i)
g.DrawString("Hello", font, myBrush, coorX, coorY)
g.DrawRectangle(New Pen(Color.FromArgb(50, 255, 0, 0), 1), coorX, coorY, stringSize.Width, stringSize.Height)
g.ResetTransform()
End Using
Next
End Using
'Draw the center of the rotation
g.DrawRectangle(Pens.Black, 200 - 5, 200 - 5, 10, 10)
End Using
End Sub
How can I go about referencing a barcode font in graphics.drawstring?
This is my code so far. Its printing the text not the barcode:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Drugname1, New Font("Arial", 8), Brushes.Black, 10, 10)
e.Graphics.DrawString("Exp: " & Expirydate_new, New Font("Arial", 8), Brushes.Black, 10, 20)
e.Graphics.DrawString("Ds Remaining: " & DsRemaining1, New Font("Arial", 8), Brushes.Black, 10, 40)
e.Graphics.DrawString(barCode, New Font("PrecisionID C128 04 Regular", 10), Brushes.Black, 10, 60)
Dim g As Graphics
'Dim Picturebox1 As Image = Image.FromFile("barcode.bmp")
'g = e.Graphics
'g.DrawImage(Picturebox1, 0, 0)
'g.Dispose()
e.HasMorePages = False
End Sub
Any ideas?
Thanks
I want to draw circles within circles and i have tried this but having difficulties
here is my code
Private Sub DrawCircle()
Dim g As Graphics
g = Panel1.CreateGraphics
Dim yellowPen As New Pen(Color.Yellow, 20)
Dim bluePen As New Pen(Color.Blue, 30)
Dim greenPen As New Pen(Color.Green, 20)
Dim skybluePen As New Pen(Color.AliceBlue, 20)
Dim voiletPen As New Pen(Color.Violet, 15)
Dim blackPen As New Pen(Color.Black, 2)
' Draw ellipses
g.DrawEllipse(yellowPen, 260, 180, 10, 10)
g.DrawEllipse(greenPen, 240, 160, 50, 50)
g.DrawEllipse(bluePen, 220, 140, 90, 90)
g.DrawEllipse(greenPen, 200, 120, 130, 130)
g.DrawEllipse(skybluePen, 180, 100, 170, 170)
g.DrawEllipse(blackPen, 180, 100, 170, 170)
g.DrawEllipse(voiletPen, 170, 90, 190, 190)
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
DrawCircle()
End Sub
It is working well but if there is any other option because if you change a little in this code everything goes wrong please help me out
May be i did't get your point right and i think you are looking for an alternate solution
here is one
Private colorForAllCircles As Color
Private ReadOnly rand As New Random
Function RandomColor() As Color
Return Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256))
End Function
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim centerX, centerY As Integer
Dim cornerX, cornerY As Integer
Dim radius As Integer
Dim greenPen As New Pen(Brushes.Blue)
centerX = 300
centerY = 200
Dim i As Integer
For i = 20To 200 Step 20
greenPen = New Pen(RandomColor, 20)
radius = i
cornerX = centerX - radius / 2
cornerY = centerY - radius / 2
e.Graphics.DrawEllipse(greenPen, cornerX, cornerY, radius, radius)
Next
End Sub
I have run into another problem with my current project. I have a form that I need to print. Doing some searching online I found a few examples and implemented it into my code. It prints but prints all my text boxes and labels stacked on top of one another. How can I get this to print my textboxes and labels formatted as I have them in the form?
Here is my code:
Private Sub PrintToolStripMenuItem1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem1.Click
PrintDocument1.PrinterSettings.Copies = 2
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(Label2.Text, Label2.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(datebox.Text, datebox.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(Label3.Text, Label3.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(locationbox.Text, locationbox.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(Label4.Text, Label4.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(groupbox.Text, groupbox.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(Label5.Text, Label5.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(passbox.Text, passbox.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(Label7.Text, Label7.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(TextBox6.Text, TextBox6.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(apbox.Text, apbox.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(Label8.Text, Label8.Font, Brushes.Blue, 100, 100)
e.Graphics.DrawString(TextBox7.Text, TextBox7.Font, Brushes.Blue, 100, 100)
End Sub
Im still new to vb 2010 so any help is appreciated, Thanks!
Just realized my mistake. I'm as blind as a bat, as it was in my face the entire time.
Just in case anyone else ever needs to figure this out.
e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Blue, X-coordinate, Y-coordinate)