Spacing out 4 areas in a defined width - vb.net

Hey all i am trying to add 4 boxes to an image that's 1280 x 720.
I am wanting to add the boxes to the top of the image but space them out evenly across the 1280 width.
Dim g As Graphics = Graphics.FromImage(image)
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(3, 7, 270, 25)) 'The transparent square for Date
g.DrawString(Format(DateTime.Now, "MM/dd/yyyy HH:mm:ss tt"), New Font("Arial", 18), Brushes.Black, New PointF(3, 5)) 'The date
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(350, 7, 170, 25)) 'The transparent square for Latitude
g.DrawString("Lat: " & "30.976154", New Font("Arial", 18), Brushes.Black, New PointF(352, 5))
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(670, 7, 180, 25)) 'The transparent square for longitude
g.DrawString("Lng: " & "33.351328", New Font("Arial", 18), Brushes.Black, New PointF(672, 5))
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(970, 7, 120, 25)) 'The transparent square for MPH
g.DrawString("MPH: " & "000", New Font("Arial", 18), Brushes.Black, New PointF(972, 5))
g.Dispose()
However i haven't found a sure fire way to making them even across the screen since each rectangle/text is a different width than the ones around it.
Any ideas, thoughts would be great!

Simply divide the width by the number of labels. Here's some pseudocode:
const int NUM_LABELS = 4;
int divWidth = width / NUM_LABELS;
int i;
for i = 0 to (NUM_LABELS - 1)
FillRect(i * divWidth, LABEL_HEIGHT, (i + 1) * divWidth, 0); // or whatever you want to do
MoveTo (i * divWidth, LABEL_HEIGHT);
DrawString("some string");

Related

how do i print 2 pages using vb.net without overlapping

Hi i have problem on printing output on vb.net, I run-out of ideas in my mind. I hope you can help me. Here's my code:
If ComboBox1.Text = "Sampled" Then
For i As Integer = 0 To DataGridView1.RowCount - 1
If DataGridView1.Item(0, i).Value = "True" Then
Dim ContainerNo = DataGridView1.Item(18, i).Value
Dim TotalContainer = DataGridView1.Item(19, i).Value
'e.Graphics.DrawImage(image:=myBitmap2, point:=New Point(110, 40))
e.Graphics.DrawString("SAMPLED", font3, Brushes.Black, 120, 70)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 90, 140)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 90, 160)
e.Graphics.DrawString("SAMPLED BY:", font1, Brushes.Black, 170, 220)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 155, 235)
e.Graphics.DrawString("QC SAMPLER/DATE", font1, Brushes.Black, 160, 250)
e.Graphics.DrawString(ContainerNo, font1, Brushes.Black, 190, 138)
e.Graphics.DrawString(TotalContainer, font1, Brushes.Black, 280, 138)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 160, 158)
' e.HasMorePages = True
index += 1
If index <= x Then
e.HasMorePages = True
MessageBox.Show("True")
Else
e.HasMorePages = False
MessageBox.Show("false")
End If
End If
Next
here's my current output now
I want my output to be like this
1st page: 2 of 3 (2 is based from Dim ContainerNo = DataGridView1.Item(18, i).Value) and (3
is from Dim TotalContainer = DataGridView1.Item(19, i).Value)
2nd page: 3 of 3
Sadly my current output now is containerNo:2 and containerNo:3 are overlapping each other on both pages, I hope you can help me
Remove the loop. This function can be called multiple times in a single print job (HasMorePages controls whether it will be called again). Each time it's called you only print one page worth of data.
Dim i As Integer = index
If ComboBox1.Text = "Sampled" AndAlso DataGridView1.Item(0, i).Value = "True" Then
Dim ContainerNo = DataGridView1.Item(18, i).Value
Dim TotalContainer = DataGridView1.Item(19, i).Value
e.Graphics.DrawString("SAMPLED", font3, Brushes.Black, 120, 70)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 90, 140)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 90, 160)
e.Graphics.DrawString("SAMPLED BY:", font1, Brushes.Black, 170, 220)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 155, 235)
e.Graphics.DrawString("QC SAMPLER/DATE", font1, Brushes.Black, 160, 250)
e.Graphics.DrawString(ContainerNo, font1, Brushes.Black, 190, 138)
e.Graphics.DrawString(TotalContainer, font1, Brushes.Black, 280, 138)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 160, 158)
End If
'Make sure index is declared OUTSIDE THE METHOD
index += 1
e.HasMorePages = index <= DataGridView1.RowCount - 1

Printing Datagridview value [duplicate]

This question already has an answer here:
How to print datagridview table with its header in vb.net?
(1 answer)
Closed 2 years ago.
when i print this from Datagridview the values are on top of one another,enter image description here
Dim rowCount As Integer = DataGridView6.Rows.Count
For i = 0 To rowCount - 1
e.Graphics.DrawString("PARTICULARS", ReportBodyFont, Brushes.Black, 70, 300)
e.Graphics.DrawString("QTY", ReportBodyFont, Brushes.Black, 400, 300)
e.Graphics.DrawString("AMOUNT", ReportBodyFont, Brushes.Black, 600, 300)
e.Graphics.DrawString("----------------------------------------------------------", ReportFont, Brushes.Black, 50, 310)
''
e.Graphics.DrawString(DataGridView6.Rows(i).Cells(1).Value, ReportBodyFont, Brushes.Black, 50, 320)
e.Graphics.DrawString(DataGridView6.Rows(i).Cells(3).Value, ReportBodyFont, Brushes.Blue, 400, 320)
e.Graphics.DrawString(DataGridView6.Rows(i).Cells(2).Value, ReportBodyFont, Brushes.Black, 600, 320)
Next
Of course they are. You're using the exact same coordinates for every row. If you expect each row to be printed below the previous one then you need to specify a Y that is greater than the previous one. Generally speaking, you would establish a baseline for the first record and the height of a record and then use a Y coordinate for each row that is that baseline plus the row index multiplied by the height, e.g.
Dim y0 = 10
Dim rowHeight = 50
For rowIndex = 0 To rowCount - 1
Dim y = y0 + rowIndex * rowHeight
'Print at y.
Next

3 way check box (toggle)

I have a custom 'toggle' (using ButterscotchTheme) and I am trying to add a 3rd option.
This is how it currently is: (cant post pictures due to being new, but here is links to the pictures)
I would like a third option. I have checked the theme code and it is done by Checkbox rules. And I know a Tricheckbox is a thing, I am just unsure how to get it to work. I did some research and was unable to figure it out in this situation.
Here is all the Themes toggle code:
Public Class ButterscotchToggle : Inherits Control
Private _check As Boolean
Public Property Checked As Boolean
Get
Return _check
End Get
Set(ByVal value As Boolean)
_check = value
Invalidate()
End Set
End Property
Sub New()
MyBase.New()
SetStyle(ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.SupportsTransparentBackColor, True)
DoubleBuffered = True
BackColor = Color.Transparent
Size = New Size(80, 25)
End Sub
Protected Overrides Sub OnClick(ByVal e As EventArgs)
Checked = Not Checked
MyBase.OnClick(e)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim b As Bitmap = New Bitmap(Width, Height)
Dim g As Graphics = Graphics.FromImage(b)
Dim outerrect As Rectangle = New Rectangle(0, 0, Width - 1, Height - 1)
Dim maininnerrect As Rectangle = New Rectangle(7, 7, Width - 15, Height - 15)
Dim buttonrect As New LinearGradientBrush(outerrect, Color.FromArgb(100, 90, 80), Color.FromArgb(48, 43, 39), 90S)
MyBase.OnPaint(e)
g.Clear(BackColor)
g.SmoothingMode = SmoothingMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.FillPath(New SolidBrush(Color.FromArgb(40, 37, 33)), RoundRect(outerrect, 5))
g.DrawPath(New Pen(Color.FromArgb(0, 0, 0)), RoundRect(outerrect, 5))
g.FillPath(New SolidBrush(Color.FromArgb(26, 25, 21)), RoundRect(maininnerrect, 3))
g.DrawPath(New Pen(Color.FromArgb(0, 0, 0)), RoundRect(maininnerrect, 3))
If Checked Then
g.FillPath(buttonrect, RoundRect(New Rectangle(3, 3, CInt((Width / 2) - 3), Height - 7), 7))
g.DrawString("ON", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(2, 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
Else
g.FillPath(buttonrect, RoundRect(New Rectangle(CInt((Width / 2) - 3), 3, CInt((Width / 2) - 3), Height - 7), 7))
g.DrawString("OFF", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(CInt((Width / 2) - 2), 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
End If
e.Graphics.DrawImage(b, New Point(0, 0))
g.Dispose() : b.Dispose()
End Sub
End Class
Any help would be greatly appreciated!
Theme downloaded from Butterscotch Theme GDI+
You would have to change that boolean property to an integer (or even better, create an enum):
Private _check As Integer
Public Property Checked As Integer
Get
Return _check
End Get
Set(ByVal value As Integer)
_check = value
Invalidate()
End Set
End Property
Change the OnClick behavior:
Protected Overrides Sub OnClick(ByVal e As EventArgs)
If Checked + 1 > 2 Then
Checked = 0
Else
Checked += 1
End If
MyBase.OnClick(e)
End Sub
and then adjust the drawing accordingly (placing the third option in the middle I'm guessing):
Select Case Checked
Case 0
g.FillPath(buttonrect, RoundRect(New Rectangle(CInt((Width / 2) - 3), 3, CInt((Width / 2) - 3), Height - 7), 7))
g.DrawString("OFF", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(CInt((Width / 2) - 2), 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
Case 1
g.FillPath(buttonrect, RoundRect(New Rectangle(3, 3, CInt((Width / 2) - 3), Height - 7), 7))
g.DrawString("ON", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(2, 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
Case 2
g.FillPath(buttonrect, RoundRect(New Rectangle((Width / 2) - (Width / 4), 3, CInt((Width / 2) - 3), Height - 7), 7))
g.DrawString("???", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle((Width / 2) - (Width / 4), 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
End Select

VB.NET Draw on top of rectangle e.Graphics

I want to draw to my form using e.Graphics. So I've drawn a set of rectangles (for the chess tiles for the game I'm making) and now I want to draw the pieces on top of the already drawn (and working) rectangles. The chess pieces are transparent png's saved in my Resources folder. I have no problem drawing them normally, but whenever I want to draw them on top of the tiles, only the tiles are visible - regardless of which line of code goes first. How do I add the pieces on top of the tiles, so the tiles are underneath?
This is the problematic code:
If Not alreadydrawn Then
Dim g As Graphics = Graphics.FromImage(screenbuffer)
Checked = False
For y = 1 To 8
For x = 1 To 8
If Checked Then g.FillRectangle(Brushes.LightGray, (x * 85) - 40, (y * 85) - 40, 85, 85)
If Not Checked Then g.FillRectangle(Brushes.Black, (x * 85) - 40, (y * 85) - 40, 85, 85)
Checked = Not Checked
Next
Checked = Not Checked
Next
e.Graphics.DrawImage(My.Resources.Bishop_White, New Rectangle(New Point(50, 50), New Size(64, 64)))
alreadydrawn = True
End If
e.Graphics.DrawImageUnscaledAndClipped(screenbuffer, New Rectangle(New Point(0, 0), New Size(795, 805)))
This is the solution I made:
checked = False
For y = 1 To 8
For x = 1 To 8
If clickedsquare(0) = x - 1 And clickedsquare(1) = y - 1 And Not boardlayout(y - 1, x - 1) = 0 And clickmode = "options" Then
t.FillRectangle(New SolidBrush(Color.FromArgb(225, 212, 128)), x * 75, y * 75, 75, 75)
Else
If checked Then t.FillRectangle(New SolidBrush(Color.FromArgb(64, 64, 64)), x * 75, y * 75, 75, 75)
If Not checked Then t.FillRectangle(New SolidBrush(Color.FromArgb(224, 224, 224)), x * 75, y * 75, 75, 75)
End If
checked = Not checked
Next
checked = Not checked
Next
...
Then:
tiles.Image = tilebuffer
pieces.Image = piecebuffer
BackgroundImage = tiles.Image
pieces.BackColor = Color.Transparent
alreadydrawn = True

Printing More than One Page with a Loop From DataGridView

I have a code am working that prints a sales report from a DataGridView. The Code prints well but can only print one page when the Value of the control Variable I goes out of the printable area. I really Need your help. Below is the code.
Dim fntAddress As New Font("Comic Sans MS", 10, FontStyle.Regular)
Dim fntHeader As New Font("Calibri", 20, FontStyle.Bold)
Dim fntBodyText As New Font("Calibri", 12, FontStyle.Regular)
Dim fntHeaderText As New Font("Calibri", 13, FontStyle.Bold)
Dim strTotalSale = txtTotal.Text
e.Graphics.DrawString("SFC POINT OF SALE AND INVENTORY MANAGEMENT", fntHeader, Brushes.Black, 100, 0)
e.Graphics.DrawString("GENERATED SALES REPORT", New Font("Calibri", 18, FontStyle.Bold), Brushes.Black, 250, 30)
Dim strDateString As String = ""
If mtbStartDate.Text = " / /" Or mtbEndDate.Text = " / /" Then
strDateString = ""
ElseIf mtbStartDate.Text = mtbEndDate.Text Then
strDateString = "Report For Date Of : " & mtbStartDate.Text
ElseIf mtbStartDate.Text <> mtbEndDate.Text Then
strDateString = "Report For Dates Of : " & mtbStartDate.Text & " - " & mtbEndDate.Text
End If
e.Graphics.DrawString(strDateString, New Font("Courier New", 15, FontStyle.Regular), Brushes.Black, 5, 70)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, 100, 770, 35))
e.Graphics.DrawString("Item Barcode", fntHeaderText, Brushes.Black, 10, 107)
e.Graphics.DrawString("Item Name", fntHeaderText, Brushes.Black, 160, 107)
e.Graphics.DrawString("Quantity", fntHeaderText, Brushes.Black, 360, 107)
e.Graphics.DrawString("Unit Cost", fntHeaderText, Brushes.Black, 450, 107)
e.Graphics.DrawString("Sub Total", fntHeaderText, Brushes.Black, 560, 107)
e.Graphics.DrawString("Date of Sale", fntHeaderText, Brushes.Black, 660, 107)
Dim RowCount As Integer = dgvSales.Rows.Count - 1
Static i As Integer = 139
Dim x1 = 10
Dim x2 = 700
Dim y1 = 155
Dim n As Integer = 0
While n < RowCount
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, i - 5, 770, 35))
e.Graphics.DrawString(dgvSales.Rows(n).Cells(0).Value, fntBodyText, Brushes.Black, 16, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(1).Value, fntBodyText, Brushes.Black, 160, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(2).Value, fntBodyText, Brushes.Black, 360, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(3).Value, fntBodyText, Brushes.Black, 450, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(4).Value, fntBodyText, Brushes.Black, 560, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(5).Value, fntBodyText, Brushes.Black, 660, i)
i = i + 35
n = n + 1
End While
e.HasMorePages = False
e.Graphics.DrawLine(Pens.Black, 150, 100, 150, i - 5)
e.Graphics.DrawLine(Pens.Black, 350, 100, 350, i - 5)
e.Graphics.DrawLine(Pens.Black, 440, 100, 440, i - 5)
e.Graphics.DrawLine(Pens.Black, 550, 100, 550, i - 5)
e.Graphics.DrawLine(Pens.Black, 650, 100, 650, i - 5)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(340, i + 40, 174, 50))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(341, i + 41, 172, 48))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(342, i + 42, 170, 46))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(343, i + 43, 168, 44))
e.Graphics.DrawString("Total Sales.", New Font("Times New Roman (Headings CS)", 22, FontStyle.Bold), Brushes.Black, 341, i + 47)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(525, i + 40, 250, 50))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(526, i + 41, 248, 48))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(527, i + 42, 246, 46))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(528, i + 43, 244, 44))
e.Graphics.DrawString(strTotalSale, New Font("Times New Roman (Headings CS)", 23, FontStyle.Bold), Brushes.Black, 538, i + 47)
Honestly, I haven't written one line of VB code in my life, but the printing mechanism at a glance seems extremely similar to the Java Printing API.
You have to mark the fact that the document you're printing has more pages yourself, using e.hasMorePages = True.
Here is a working example:
http://www.dreamincode.net/forums/topic/139447-help-with-hasmorepages-please/
Take into consideration the fact that you must always know the place you left off on the former page. Here is another, more realistic example: http://www.dreamincode.net/forums/topic/128639-ehasmorepages-and-logic-to-use-it/
Hope you understand that it could be complex because you have to calculate fontheight, papersize height, etc.
In this case, consider that there would be a fixed setting
Dim nMaxLine as Integer = 30 ' ----------> lines count per page
Dim x as Integer
Dim n as Integer
For x = 0 to datagridview.rows.count - 1
' ..
e.Graphics.DrawString( ... )
' ..
n += 1
If n > nMaxLine Then
e.hasMorePages = True
n = 0
End If
Next