VB.net Printform cant find path - vb.net

So I am working on a form which need to be printed. I want to end up with a PDF file using Foxit PDF printer. The problem is that I cant figure out how to get the selected path as file location so I keep getting an the Path cannot be null.
error. Where in the code should I put my filelocation when using the Printform?
In this code the foldername is the location where I want to print.
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
Dim folderDlg As New FolderBrowserDialog
Dim foldername As String
folderDlg.ShowNewFolderButton = True
If (folderDlg.ShowDialog() = DialogResult.OK) Then
foldername = folderDlg.SelectedPath
Dim root As Environment.SpecialFolder = folderDlg.RootFolder
End If
PrintForm1.Print()
End Sub
Edit:
Actually deleted part of the code and still getting the same error (first part wasnt doing anything to start with I know that). All I am using now is:
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintForm1.Print()
End Sub
Also microsoft help database about Printform isnt helping since I have done exactly what it says and still getting the Path is Null error
Edit 2:
So I am using this code now and it is working.
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintDialog1.PrinterSettings = PrintForm1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings
With Me.PrintForm1
.PrintAction = Printing.PrintAction.PrintToPreview
Dim MyMargins As New Margins
With MyMargins
.Left = 10
.Right = 10
.Top = 10
.Bottom = 10
End With
.PrinterSettings.DefaultPageSettings.Margins = MyMargins
.Print()
End With
End Sub
but as soon as I try to set which area it should print I get the following error: "Printing is not a member of powerpacks". I tried using the following code according to microsoft this is the way it should work.. I have no clue where the error comes from
.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)

You don't need a path to use printform. Printform just prints what you see on the screen to your default printer. You need to install "Visual Basic PowerPacks" to use this command. More explanation you may find here:
https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/printing/how-to-print-a-form-by-using-the-printform-component

To preview your print you don't need to use printdialog and all this. You just click on printform1 in the designer,to get up the properties window of printform1. In printaction you choose PrintToPreview. Thats all its needed.
These are all the lines I need:
Public Class Form1
Private Sub Exit_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
Private Sub Print_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintForm1.Print()
End Sub
End Class

Related

Saving and reading files on Visual basic

Hi I'm creating a "Toilet paper tracker" on visual basic and I'm struggling with saving and reading files, I know I am missing stuff. The user should be able to login and input a threshold and when reached a warning will pop up saying "buy more toilet paper" (i haven't coded this yet) and the user can add to create a total and subtract from it too. The user should also be able to save the total to a file and I want the program to be able to read the file and change the total if the user wants to add or subtract again. It would be greatly appreciated if you pointed me in the right direction, I'm only young so it's relatively simple. Here is my program :)
Imports System.IO
Public Class frmTPT
Private Sub TPT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call loadchangetotal()
End Sub
Sub loadchangetotal()
cboChange.Items.Add("Add to Total")
cboChange.Items.Add("Subtract from Total")
End Sub
Private Sub cboVenue_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboChange.SelectedIndexChanged
If cboChange.Text = "Add to Total" Then
Dim frmChangeACopy As New frmChangeA
frmChangeACopy.Show()
Me.Hide()
ElseIf cboChange.Text = "Subtract from Total" Then
Dim frmChangeSCopy As New frmChangeS
frmChangeSCopy.Show()
Me.Hide()
End If
End Sub
Private Sub btnReturn_Click(sender As Object, e As EventArgs)
Dim frmLoginCopy As New frmLogin
frmLoginCopy.Show()
Me.Hide()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtThreshold.Text = ""
cboChange.Text = ""
txtTotal.Text = ""
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub LogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogoutToolStripMenuItem.Click
Dim frmLoginCopy As New frmLogin
frmLoginCopy.Show()
Me.Hide()
End Sub
Private Sub btnReadTotal_Click(sender As Object, e As EventArgs) Handles btnReadTotal.Click
Dim FileReader As StreamReader
Dim result As DialogResult
result = OpenFileDialog1.ShowDialog
If result = DialogResult.OK Then
FileReader = New StreamReader(OpenFileDialog1.Filename)
txtFileContent.Text = FileReader.ReadToEnd() 'i want to be able to read a
'previously saved total so that
FileReader.Close() 'it can be used to find the new total
'after it has been added to
End If 'or subtratced
End Sub
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Call SaveFile()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim A, S, NewTotal As Integer
A = Val(frmChangeA.txtAdd.Text)
S = Val(frmChangeS.txtSubtract.Text)
NewTotal = A - S 'I want to be able to load a previously saved total if one exists and add and
'subtract from it
End Sub
End Class
Sub SaveFile()
Dim FileWriter As StreamWriter
Dim results As DialogResult
results = SaveFileDialog1.ShowDialog
If results = DialogResult.OK Then
FileWriter = New StreamWriter(SaveFileDialog1.FileName, False)
FileWriter.Write(txtFileContent.Text) ' is txtFileContent supposed to be
' the name of my textbox?
FileWriter.Close()
End If
End Sub
Design
You didn't mention if you were using .Net Core or 4.x. If the later, you can sometimes use the Insert Snippet functionality to learn how to do common tasks. For example in this case you could right click in the code editor and select Insert Snippet then Fundamentals then File System and finally Write text to a file. This will result in the following VB code:
My.Computer.FileSystem.WriteAllText("C:\Test.txt", "Text", True)
Unfortunately, this option doesn't work with .Net core since the My namespace wasn't ported to core.
The key point of this problem lies in reading and writing data from text. It is a clear idea to write two methods to achieve read and write.
You can refer to the following code. The two methods in the following example are WriteTotal(), ReadTotal().
Design:
Public Class Form1
Dim Oldtotal As Integer
Dim Newtotal As Integer
Private Sub btnLoadTotal_Click(sender As Object, e As EventArgs) Handles btnLoadTotal.Click
WriteTotal()
ReadTotal()
End Sub
Private Sub btnUpdateTotal_Click(sender As Object, e As EventArgs) Handles btnUpdateTotal.Click
cboChange.Text = Nothing
WriteTotal()
ReadTotal()
MsgBox("Inventory updated")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cboChange.SelectedIndex = 0
ReadTotal()
End Sub
Sub WriteTotal()
Using swriter As IO.StreamWriter = New IO.StreamWriter("D:/paperstore.txt", False, System.Text.Encoding.UTF8)
If cboChange.Text = "Add to Total" Then
Newtotal = Oldtotal + CType(txtThreshold.Text, Integer)
swriter.WriteLine(Newtotal)
ElseIf cboChange.Text = "Subtract from Total" Then
If CType(txtThreshold.Text, Integer) > Oldtotal Then
swriter.WriteLine(Oldtotal)
MsgBox("buy more toilet paper")
Else
Newtotal = Oldtotal - CType(txtThreshold.Text, Integer)
swriter.WriteLine(Newtotal)
End If
Else
swriter.WriteLine(txtTotal.Text)
End If
End Using
End Sub
Sub ReadTotal()
Using sreader As IO.StreamReader = New IO.StreamReader("D:/paperstore.txt", System.Text.Encoding.UTF8)
Try
Oldtotal = sreader.ReadLine()
txtTotal.Text = Oldtotal
Catch ex As Exception
MsgBox(ex.Message)
End
End Try
End Using
End Sub
End Class

when the text in a textbox is equal to a certain word i need to value in the combo box to be saved for that text

I have orders in text files in the debug folder and when i type the name of the order in a text box it displays the order is a list box and there is a combo box underneath where i can change the status of the meal preparation (Being prepared, ready to deliver etc,). If i go back to that form and type in the same order name into the textbox i need to previous prepartion status to be already in the textbox. Thanks for any help!
Public Class frmOrderStatus
Private Sub btnStatus_Click(sender As Object, e As EventArgs) Handles btnStatus.Click
Dim sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
lstOrder.Items.Add(sr.ReadLine)
Loop
End Sub
Private Sub OrderStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstOrder.Items.Clear()
btnStatus.Enabled = False
ChangeStatus.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
strTxtOrderNum = txtOrderNum2.Text
btnStatus.Enabled = True
ChangeStatus.Enabled = True
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
strSaveStatus = ChangeStatus.SelectedIndex
End Sub
Private Sub ChangeStatus_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ChangeStatus.SelectedIndexChanged
End Sub
End Class
It recognizes the file; it just tells you it is in use. A Stream must be closed and disposed. I don't see the StreamReader even being closed let alone disposed. A `Using...End Using block will close and dispose of objects even if there is an error.
I just used a text file I happened to have to test.
Private strTxtOrderNum As String = "host"
Private Sub ReadFile()
Using sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
ListBox1.Items.Add(sr.ReadLine)
Loop
End Using
End Sub
Private Sub WriteFile()
Dim strSelectedItem = ComboBox1.Text
Using swVar As IO.StreamWriter = IO.File.AppendText(strTxtOrderNum & ".txt")
swVar.WriteLine(strSelectedItem)
End Using
End Sub

How do I import HTML files to be displayed in a VB.NET WebBrowser

So far I've gotten this from piecing together OpenDialog tutorials and HTML editor/writer tutorials but I just picked this up today, to do this specific thing so my prior knowledge of VB and general software development is EXTREMELY low. :P
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1 Click
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName > "" Then
WebBrowser1.DocumentText =
End sub
Use URL propery of WebBrowser Control also always use filter to select desired format only
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "Html or Pdf-Files(*.html; *.pdf))|*.html;*.pdf"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim cfilenam As String = OpenFileDialog1.FileName()
If cfilenam.Trim().Length > 0 Then
Me.WebBrowser1.Url = New Uri(cfilenam)
Me.WebBrowser1.Refresh()
End If
End If
End Sub

How to set download location with FolderBrowserDialog

I am trying to use FolderBrowserDialog to select the location where a file will be downloaded. This is the code for selecting a folder.
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Now I just need to know how to take directory that appears in the textbox and have the file get downloaded to there. Here is the code for the download.
My.Computer.Network.DownloadFile("http://download1516.mediafire.com/wtzr4h1b37zg/ptzcffq933e87c8/sword_custom.png", "C:\Users\Administrator")
What would I need to replace "C:\Users\Administrator" with?
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
Dim URL As String = "http://download1516.mediafire.com/wtzr4h1b37zg/ptzcffq933e87c8/sword_custom.png"
Dim SaveFile As String = "sword_custom.png"
With FolderBrowserDialog1
If .ShowDialog = DialogResult.OK Then
TextBox1.Text = .SelectedPath
My.Computer.Network.DownloadFile(URL, IO.Path.Combine(.SelectedPath, SaveFile))
End If
End With
End Sub

Pulling information from the Page Source

Ok so I have been have alot of trouble with the
Web browser control on a few applications I am working on.
All of them share the same issue. I want to have the application navigate s webpage a read the write the text in the page source to a variable. I also need to be able to save the files afterwards.
Some Source code:
Public Class Form4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
MyFolderBrowser.Description = "Select the Folder"
MyFolderBrowser.ShowNewFolderButton = False
Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
If dlgResult = Windows.Forms.DialogResult.OK Then
TextBox1.Text = MyFolderBrowser.SelectedPath
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" Then
MessageBox.Show("You have to select a directory!")
Else
WebBrowser1.Navigate("www.realmofthemadgod.com/version.txt")
System.Threading.Thread.Sleep(3000)
Dim PageSource As String = WebBrowser1.Document.Body.InnerText
WebBrowser1.Navigate("http://www.realmofthemadgod.com/AssembleeGameClient" & PageSource & ".swf")
End If
End Sub
End Class
The first thing I am having an issue with is that it never waits for the webpage to load before pulling the Document text. I have tried many different ways to from different solutions people have posted to get around this. Oddly it always seems to work if I do it a second time.
And I want to save the final resulting webpage as an swf to the selected directory if Button2 is clicked.
Thanks for any help I have been looking for this everywhere
Welcome to the dark art of web scraping. First off, I would recommend using WebClient instead of WebBrowser as it has discrete methods for downloading data from web sites. It looks like your version.txt only contains the data you want (and no extraneous html) so we can download it directly. If you needed to parse html I would use HtmlAgilityPack. Untested code to get you started:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" Then
MessageBox.Show("You have to select a directory!")
Else
Using wc as New WebClient()
Dim version = wc.DownloadString("www.realmofthemadgod.com/version.txt")
Dim swf = "http://www.realmofthemadgod.com/AssembleeGameClient" + version + ".swf"
wc.DownloadFile(swf,"c:\temp\myswf.swf")
End Using
End If
End Sub