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)
Related
When I minimize the form, I lose all the contents drawn on the picture box. I want it to be retain on the picture box. Please help me in this regard.
The code can be seen below..
Private Sub ButtonDraw_Click(sender As Object, e As EventArgs) Handles ButtonDraw.Click
Dim g As Graphics = PictureBox1.CreateGraphics
Dim MyPen As New Pen(Color.Red)
MyPen.Width = 2
MyPen.DashStyle = DashStyle.Solid
g.TranslateTransform(PictureBox1.Width / 2, PictureBox1.Height / 2)
g.DrawEllipse(MyPen, New Rectangle(-150, -150, 300, 300))
End Sub
Example code, using e.Graphics in the Paint() event, as suggested by Jimi:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim g As Graphics = e.Graphics
Using MyPen As New Pen(Color.Red)
MyPen.Width = 2
MyPen.DashStyle = DashStyle.Solid
g.TranslateTransform(PictureBox1.Width / 2, PictureBox1.Height / 2)
g.DrawEllipse(MyPen, New Rectangle(-150, -150, 300, 300))
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
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 this code that I wrote up, but this code was used when I left clicked on the form, but wasn't a random number of circles. I am wondering how I can adapt it to draw 5-10 random circles with random colors across the form when I click a button, but it has to use a loop. Thanks!!
Private Sub CirclePainterForm_MouseDown(ByVal sender As _
Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
Dim graphicsObject As Graphics = CreateGraphics()
Dim randomObject As Random = New Random
Dim diameter As Integer = randomObject.Next(5, 200)
If e.Button = Windows.Forms.MouseButtons.Left Then
graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X, e.Y, _
diameter, diameter)
graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 250, e.Y, _
diameter + 50, diameter + 50)
graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 500, e.Y, _
diameter + 75, diameter + 75)
graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 750, e.Y, _
diameter + 100, diameter + 100)
graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 1000, e.Y, _
diameter + 125, diameter + 125)
Add a button to your form and add code for the Click event:
To do Circles Or Squares [and updated with your/my comments]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rnd As New Random(), rndCol As New Random(DateTime.Now.Ticks Mod ((rnd.Next) + 1))
Dim numShapes = rnd.Next(5, 11), bIsCircleOrSquare As Boolean = (rnd.Next Mod 2)
Using g = Me.CreateGraphics()
g.Clear(Me.BackColor)
Dim diam = rnd.Next(55, Math.Min(255, ClientSize.Width))
While numShapes > 0
Using b As New SolidBrush(Color.FromArgb(rndCol.Next(100, 256), rndCol.Next(256), rndCol.Next(256), rndCol.Next(256)))
Dim x = rnd.Next(ClientSize.Width - diam), y = rnd.Next(ClientSize.Height - diam)
If bIsCircleOrSquare Then
g.FillRectangle(b, x, y, diam, diam)
g.DrawRectangle(Pens.Black, x, y, diam, diam)
Else
g.FillEllipse(b, x, y, diam, diam)
g.DrawEllipse(Pens.Black, x, y, diam, diam)
End If
End Using
numShapes -= 1
End While
End Using
End Sub
If you want to see the circles overlap you can replace:
Using b As New SolidBrush(Color.FromArgb(rndCol.Next(100,256), rndCol.Next(256), rndCol.Next(256), rndCol.Next(256)))
with
Using b As New SolidBrush(Color.FromArgb(255, rndCol.Next(256), rndCol.Next(256), rndCol.Next(256)))