Printing Datagridview value [duplicate] - vb.net

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

Related

How can I add/subtract to one axis on a control's location in a form?

I know I can do .Location = new Point(x,y) but how can I add or subtract the value of strictly the X or Y coordinate?
Something like this?
lblTester.Location.X = lblTester.Location.X + 100
lblTester.Location = new Point(lblTester.Location.X + 100, lblTester.Location.Y)
What olaf suggests should work, and this should work too:
lblTester.Top += 100 'Move Down
lblTester.Top -= 100 'Move Up
lblTester.Left += 100 'Move Right
lblTester.Left -= 100 'Move Left

I want to draw a line chart in vb.net and do calculations based on that graph

I want to draw a line chart in vb.net 2013, something like shown in the picture:
I managed to draw a tangent but I am unable to draw the parallel lines.
Not exactly sure if this what you need, but you only need to construct new points at (0.1, 0) and (0.2, 0) and use slope factor of tangent line to get points where you need to draw parallel lines.
Dim A = New Point(120, 80)
Dim k As Double = 0.5
Dim kx = 1.0
Dim ky = kx * k
Dim A1 = New Point(A.X + kx * 30, A.Y - ky * 30)
' Line at 0.1
Dim B = New Point(110, 100)
Dim B1 = New Point(B.X + kx * 30, B.Y - ky * 30)
' Line at 0.2
Dim C = New Point(120, 100)
Dim C1 = New Point(C.X + kx * 30, C.Y - ky * 30)

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

Drawing clickable boxes in VB.net

I am currently making a Chess game as a side project, but i'm struggling to figure out how to make the different tiles clickable. Searching only yielded a C# answer which didn't work.
I've used the creategraphics command to draw on my form, if that's of any help.
Sub DrawBoard()
Dim Board As Graphics = Me.CreateGraphics
Dim BlackPen As New Pen(Color.Black, 3)
Board.Clear(Color.White)
For i = 0 To 3
For j = 0 To 6 Step 2
Board.FillRectangle(Brushes.Gray, j * 60, 120 * i, 60, 60)
Next
For j = 1 To 7 Step 2
Board.FillRectangle(Brushes.Gray, j * 60, (120 * i) + 60, 60, 60)
Next
Next
For i = 0 To 7
For f = 0 To 7
Board.DrawRectangle(BlackPen, i * 60, (f * 60), 60, 60)
Next
Next
BlackPen.Dispose()
End Sub
This is the code I'm using to draw the chess board at the moment.

Spacing out 4 areas in a defined width

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");