Home > Back-end >  How to create and print a table using Drawing Vb.net
How to create and print a table using Drawing Vb.net

Time:03-22

I'm trying to recreate a label using Vb.net

I have this label that I want to recreate: Label

What I did till now: What I tried to do I tried to change lines but couldn't do to look like that, I don't even know how to arrange or move or add new column or rows.

Code that I found on the internet:

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 

        Dim ht As Single = 7 ' Table Height 

        Dim wt As Single = 5 ' Table Width 

        Dim r As Integer = 15, c As Integer = 2 ' Rows and Cols in Table 

        Dim Cht As Single ' Cell Height 

        Dim lst As Single ' last line drawn 

        Dim i As Integer 

        Dim p As Pen 

        Cht = CSng(Math.Round(ht / r, 2)) * c 

        lst = 0.5F   Cht ' 0.5->default margin 

        p = New Pen(Color.Black, 0.025F) 

        e.Graphics.PageUnit = GraphicsUnit.Inch 

        e.Graphics.DrawRectangle(p, 0.5F, 0.5F, wt, ht) ' border of the table 

        p.Color = Color.Blue 

        For i = 0 To CInt(r / c) - 1 ' lines in the table 

            e.Graphics.DrawLine(p, 0.5F, lst, 0.5F   wt, lst) 

            lst  = Cht 

        Next 

 

    End Sub

I'm lost, I don't know how to create a similar label. What's the best way to do that?

CodePudding user response:

The following will help you get started in creating the label pictured in your OP.

I'll be using a Windows Forms App (.NET Framework) project with a Form named Form1.

Add the following Imports statements:

  • Imports System.Drawing.Drawing2D
  • Imports System.Drawing.Printing

To draw a rounded rectangle, we'll convert code from enter image description here

  • Double-click Paint to add the event handler to the form
  • Add a PrintDocument to Form1 (name: PrintDocument1)

    Subscribe to Paint event

    • In Properties Window, select PrintDocument1 from the drop-down
    • Click enter image description here
    • Double-click PrintPage to add the event handler to the form

    Add a Button to the Form (name: btnPrint)

    Subscribe to Click event

    • In Properties Window, select btnPrint from the drop-down
    • Click enter image description here
    • Double-click Click to add the event handler to the form

    Usage (Panel):

    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
        CreateProductLabel(e.Graphics)
    End Sub
    

    Usage (PrintDocument):

    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        PrintDocument1.Print()
    End Sub
    
    Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
        CreateProductLabel(e.Graphics)
    End Sub
    

    Here's what the Form looks like:

    enter image description here

    Resources:

    • Related