none
Print All IP between a given range or using CIDR RRS feed

  • Question

  • I have never used visual basic before (past experience with coding in college using python and Java) so please help me figure this out. I have a list of IP addresses (1st column --> Network Address, 2nd column --> last address in subnet, 3rd colum --> Network Address in CIDR notation) and I want to print every address between in the subnet. 

    Below is my attempt thus far (pitiful but I have spent a good amount of time figuring out other ways)

    Private Function IP_Range_Print() As String

        Dim firstOctet As Integer
        Dim secondOctet As Integer
        Dim thirdOctet As Integer
        Dim fourthOctet As Integer
        For counter [As String] = "
        
        Next
        Return printRange

    End Function

    Tuesday, December 5, 2017 1:02 PM

All replies

  • Hi Humanoid88,

    Do you just want to know how to print some data? What controls do you use to display this data and then print it? DataGridview or other? I suggest you to use DataGridview to display these data and then print it. Here is the example about printing DataGridview.

     Dim strFormat As StringFormat
        'Used to format the grid rows.
        Dim arrColumnLefts As New ArrayList()
        'Used to save left coordinates of columns
        Dim arrColumnWidths As New ArrayList()
        'Used to save column widths
        Dim iCellHeight As Integer = 0
        'Used to get/set the datagridview cell height
        Dim iTotalWidth As Integer = 0
        '
        Dim iRow As Integer = 0
        'Used as counter
        Dim bFirstPage As Boolean = False
        'Used to check whether we are printing first page
        Dim bNewPage As Boolean = False
        ' Used to check whether we are printing a new page
        Dim iHeaderHeight As Integer = 0
        'Used for the header height
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("Column1", GetType(Integer))
            dt.Columns.Add("Column2", GetType(String))
            dt.Columns.Add("Column3", GetType(String))
            dt.Rows.Add(1, "AA", "AAA")
            dt.Rows.Add(2, "BB", "BBB")
            dt.Rows.Add(3, "CC", "CCC")
            DataGridView1.DataSource = dt
        End Sub
    
        Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.WindowState = FormWindowState.Maximized
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            PrintDocument1.Print()
        End Sub
        Private bitmap As Bitmap
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Try
                'Set the left margin
                Dim iLeftMargin As Integer = e.MarginBounds.Left
                'Set the top margin
                Dim iTopMargin As Integer = e.MarginBounds.Top
                'Whether more pages have to print or not
                Dim bMorePagesToPrint As Boolean = False
                Dim iTmpWidth As Integer = 0
    
                'For the first page to print set the cell width and header height
                If bFirstPage Then
                    For Each GridCol As DataGridViewColumn In DataGridView1.Columns
                        iTmpWidth = CInt(Math.Floor(CDbl(CDbl(GridCol.Width) / CDbl(iTotalWidth) * CDbl(iTotalWidth) * (CDbl(e.MarginBounds.Width) / CDbl(iTotalWidth)))))
    
                        iHeaderHeight = CInt(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11
    
                        ' Save width and height of headers
                        arrColumnLefts.Add(iLeftMargin)
                        arrColumnWidths.Add(iTmpWidth)
                        iLeftMargin += iTmpWidth
                    Next
                End If
                'Loop till all the grid rows not get printed
                While iRow <= DataGridView1.Rows.Count - 1
                    Dim GridRow As DataGridViewRow = DataGridView1.Rows(iRow)
                    'Set the cell height
                    iCellHeight = GridRow.Height + 5
                    Dim iCount As Integer = 0
                    'Check whether the current page settings allows more rows to print
                    If iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
                        bNewPage = True
                        bFirstPage = False
                        bMorePagesToPrint = True
                        Exit While
                    Else
                        If bNewPage Then
                            'Draw Header
                            e.Graphics.DrawString("The IP Address", New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
    
                            Dim strDate As [String] = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString()
                            'Draw Date
                            e.Graphics.DrawString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
    
                            'Draw Columns                 
                            iTopMargin = e.MarginBounds.Top
                            For Each GridCol As DataGridViewColumn In DataGridView1.Columns
                                e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
    
                                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
    
                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight), strFormat)
                                iCount += 1
                            Next
                            bNewPage = False
                            iTopMargin += iHeaderHeight
                        End If
                        iCount = 0
                        'Draw Columns Contents                
                        For Each Cel As DataGridViewCell In GridRow.Cells
                            If Cel.Value IsNot Nothing Then
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), CSng(iTopMargin), CInt(arrColumnWidths(iCount)), CSng(iCellHeight)), strFormat)
                            End If
                            'Drawing Cells Borders 
                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iCellHeight))
                            iCount += 1
                        Next
                    End If
                    iRow += 1
                    iTopMargin += iCellHeight
                End While
                'If more lines exist, print another page.
                If bMorePagesToPrint Then
                    e.HasMorePages = True
                Else
                    e.HasMorePages = False
                End If
            Catch exc As Exception
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub
    
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            Try
                strFormat = New StringFormat()
                strFormat.Alignment = StringAlignment.Near
                strFormat.LineAlignment = StringAlignment.Center
                strFormat.Trimming = StringTrimming.EllipsisCharacter
    
                arrColumnLefts.Clear()
                arrColumnWidths.Clear()
                iCellHeight = 0
                iRow = 0
                bFirstPage = True
                bNewPage = True
    
                ' Calculating Total Widths
                iTotalWidth = 0
                For Each dgvGridCol As DataGridViewColumn In DataGridView1.Columns
                    iTotalWidth += dgvGridCol.Width
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, December 6, 2017 6:29 AM
    Moderator
  • SO if the start IP is 192.0.0.0 and the end IP is 192.255.255.255 (192.0.0.0/8) - You want to print 16,777,216 IP addresses ?

    There are probably many examples of CIDR to Range and vice-versa but wanting to print them all ? A slash 8 CIDR will use up a carton of paper.....

    Friday, December 8, 2017 5:59 AM