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
I was trying to build a custom CheckBox, but I see a black rectangle around it. Why does this happen? And why the second CheckBox has the first overlapped?
It looks like there's a black rectangle in a default position of the control. When I add one more CheckBox, the second looks like 2 CheckBox-es in the same position.
Public Class mycheckbox
Inherits CheckBox
Protected Overrides Sub onpaint(pevent As PaintEventArgs)
pevent.Graphics.FillRectangle(New SolidBrush(BackColor), Location.X, Location.Y, Width, Height)
Dim brsh As New SolidBrush(Color.YellowGreen)
Dim boxside As Integer = CInt(pevent.Graphics.MeasureString(Text, Font, Width).Height)
pevent.Graphics.FillRectangle(brsh, Location.X, Location.Y, Width, Height)
If Checked And Enabled Then
pevent.Graphics.DrawImage(My.Resources.X, Location.X + 1, Location.Y + 1, 18, 18)
pevent.Graphics.DrawRectangle(Pens.Black, New Rectangle(Location.X, Location.Y, 19, 19))
pevent.Graphics.DrawString(Text, Font, Brushes.Black, boxside + 15, 11)
End If
brsh.Dispose()
End Sub
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim mcb1 As New mycheckbox
Dim mcb2 As New mycheckbox
mcb1.Name = "cb1"
mcb1.Text = "Hello!!!"
mcb1.Location = New Point(10, 10)
mcb1.Size = New Size(300, 30)
mcb2.Name = "cb2"
mcb2.Text = "Hi!!!"
mcb2.Location = New Point(10, 50)
mcb2.Size = New Size(300, 30)
Me.Controls.Add(mcb1)
Me.Controls.Add(mcb2)
End Sub
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 problem to print all the item inside richtextbox. here is my code for printing.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim PrintDocument1 As New Printing.PrintDocument
Dim PrintSettings1 As New Printing.PrinterSettings
Dim PrintPreview1 As New PrintPreviewDialog
PrintDocument1.PrinterSettings.PrintRange = Printing.PrintRange.AllPages
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
'Set the printer name.
PrintDocument1.PrinterSettings.PrinterName = PrintSettings1.PrinterName
PrintPreview1.Document = PrintDocument1
PrintPreview1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim intX, intTotal, intA, intB, heightImg, heightText1, heightText2, heightText3, heightText4, heightBlank As Integer
Dim randNum, Sum As Integer
randNum = 1000
intTotal = 0
If ComboBox1.Text = "AE" Then
For intA = 1 To Convert.ToInt32(TextBox2.Text)
Sum = randNum + intA
For intX = 1 To 8
Dim SumAE As String = "AE-" & Sum.ToString
Dim font As New Font("Arial", 8, FontStyle.Regular)
e.Graphics.DrawString("TEMPORARY MRN - HOSPITAL ", font, Brushes.Black, 10, heightText1 + 10)
e.Graphics.DrawString(SumAE, font, Brushes.Black, 10, heightText2 + 20)
AxBarcode1.Text = SumAE
AxBarcode1.Refresh()
Dim img As Image = AxBarcode1.Picture
Dim orgData = Clipboard.GetDataObject
Clipboard.SetImage(img)
Me.RichTextBox1.Paste()
e.Graphics.DrawImage(Clipboard.GetImage, 10, heightImg + 30)
e.Graphics.DrawString(" Name : ", font, Brushes.Black, 10, heightText3 + 110)
e.Graphics.DrawString(" IC : ", font, Brushes.Black, 10, heightText4 + 120)
e.Graphics.DrawString("", font, Brushes.Black, 10, heightBlank + 150)
heightImg = heightImg + 150
heightText1 = heightText1 + 150
heightText2 = heightText2 + 150
heightText3 = heightText3 + 150
heightText4 = heightText4 + 150
heightBlank = heightBlank + 150
intTotal += intX
Next intX
intB += intA
Next intA
End If
End Sub
The page preview only 1 page. It should more because there is a 8times loop based on value inserted in textbox2.
Any help is much appreciated. Thanks :)