Loading a pdf file in Visual Basic Windows form? - vb.net

I have seen tutorials on how to open pdf files into the windows form, but they show how to open it by creating a button that will find it from the directory.
I want the pdf file to be displayed already, as soon as the user loads that Window form.
I have installed the Adobe pdf reader in the component toolbox, and
put the following code with the load function of the form:
Public Class Adding_Worksheet
Private Sub Adding_Worksheet_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Call AxAcroPDF1.LoadFile(0, "D:\Mixed_Addition_1.pdf")
End Sub
End Class
I have adapted this from when I added an swf file, which had no problems in loading. I know the zero is incorrect above, but i am not sure what to write there. the additional code i had for the swf file was AxShockwaveFlash1.Play(). would i need to do something like AxAcroPDF1.Load()?

If all you want to do is display a PDF and nothing else, why not use a System.Windows.Forms.WebBrowser control, and make the URL "D:\Mixed_Addition_1.pdf" ?

Maybe he didn't want to use a web browser to open a PDF file, Right click item in toolbox, when designer is active, select choose items, select COM components and select Adobe. then add code to new form. Printer will be active, you can disable "Toolbar" in properties tab
Imports AxAcroPDFLib
Imports AcroPDFLib
Public Class TripSheet
'
'AxAcroPDF1
'
Public Sub New()
' This call is required by the designer.
InitializeComponent()
AxAcroPDF1.Location.Equals(AxAcroPDF1.LoadFile("C:\path\file.pdf"))
AxAcroPDF1.LoadFile("C:\path\file.pdf")
' Add any initialization after the InitializeComponent() call.
End Sub

Related

WinForms Startup Event not being handled

I have a Windows Forms Application in .NET 5 with Application Framework activated and the startup object set to (my) MainForm.
Using "View Application Events" in the application's properties, I auto-generated the ApplicationEvents.vb file and with the given controls auto-generated a method to do something on Startup (as I understand before even the MainForm loads) - but nothing in this method gets run, not even breakpoints are triggered.
I would assume an auto-generated sub in an auto-generated file designed for this should work and every other event handling sub does, just not Startup's.
This is my ApplicationEvents.vb (without the auto-generated comment):
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Debug.Print("Test")
MsgBox("Test")
End Sub
End Class
End Namespace
Background:
I'm trying to enable high DPI scaling for my application.
For this I've tried using the app.manifest file (opened by "View Windows Settings" in the application's properties), but no combination of tags I found in the docs worked (I'm running the required version of Windows 10 of cause).
The same for the app.config file, but I expected this because it's a .NET Framework feature to use it for that.
So I landed on Application.SetHighDpiMode(HighDpiMode). This worked when put into the MainForm's Loadevent, but it only did what was intended when the form was already loaded and it was then put on a scaled up screen / the screen was scaled up while it was already loaded.
If it was started on an already scaled screen, it looked jumbled.
So I figured that enabling it only on the form loading is just a bit too late and it should be run asap, so I landed on Startup.
The startup object has to be Sub Main for the Startup event to work, not MainForm (or any form at all).
(Thanks to #Hans Passant for the tip.)
If you want to change which one is the main form later on you have to do the following:
Close your project in Visual Studio.
Open your project's folder in explorer.
In it, open folder My Project.
Open Application.myapp with any text editor.
Change the form between the <MainForm> tags to your (new) main forms name.
Save and close.
Open ApplicationDesigner.vb with any text editor.
Find the following line and change YourMainFormsName to your (new) main forms name:
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.YourProjectsName.YourMainFormsName
End Sub
Save and close.
Open your project again and start it up. The startup form should have changed.
This way you can keep the application framework and don't have to write your own Sub Main.

How to enable selecting new e-mails when Outlook add-in is open?

I'm creating an add-in for Outlook (Desktop Version, not Online) using Visual Studio 2017. The main intent for the application is to extract attachments and upload to specified SAP records. This could typically be notifications, PurchaseOrders etc.
The add-in works by opening a form where you can select from options like zipping the files, delete files from e-mail, upload the e-mail it self etc.
My challenge is that the add-in works for the e-mails selected when you open the add-in. But to select new e-mails you'll need to close the form, select new e-mails and then re-open/run the add-in. Basically the add-in form is acting as a dialog box and the Outlook explorer window is in-active. Is there a setting or any thing that will allow me to select new e-mails when the form/dialog box is open or is this somethinig I'll have to live with?
Thanks for your support.
BR
Fnk
Figured it out in the end. Found the answer in one of my books: "Sams Teach Yourself: Visual Basics 2015". (Apparently in 24 hours if you can plough through 600 pages in that time).
Anyway, I was looking in the Load Form and not the Show Form code. Somehow I'd used frm.ShowDialog() rather than frm.Show(). Apparently the ShowDialog() method opens the form as Modal while Show() opens as non-Modal.
As a result the code for the Ribbon button now is as follows:
Imports Microsoft.Office.Tools.Ribbon
Public Class Ribbon1
Private Sub Ribbon1_Load(ByVal sender As System.Object, _
ByVal e As RibbonUIEventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) _
Handles Button1.Click
Dim frm As MainForm 'Define a form object
frm = New MainForm()
'frm.ShowDialog() 'Method opens form object as Modal
frm.Show() 'Method opens form object as non-Modal
End Sub
End Class
I hope this may help someone else on the same experience level as myself.

Visual basic. Change default startup form in code, depending on value of variable

I have been working on a Visual Basic project in Visual Studio and have encountered a problem.
I understand that the Startup form property in the Application page of the Project Designer can be changed to a default form, however what I require is a way to do this through code in ApplicationEvents.vb depending on the value of a variable within application settings.
The goal is that if a user completes a form then a value is assigned to a variable, e.g. variable username = "xxx". If this value is true, then the default startup is a login form (as the user has already registered), and if it is false then the user is taken to a register form.
I appreciate that I could use another form to determine this, however this seems like I would be squandering the capabilities of ApplicationEvents and not using it correctly (I also want to avoid the inevitable flicker of a blank form as it decides).
I know that the default form is stored in Application.myapp, however with the final publication of the .exe this file will (presumably) not be exported with it, so I want to avoid writing directly to it. I have also read into the windowsformsapplicationbase.mainform property, however cannot figure out how to use it?
Here is a example piece of code from ApplicationEvents.vb to demonstrate my question.
If String.IsNullOrEmpty(My.Settings.username) Then
MsgBox("You have not registered")
'set register as default form
Else
MsgBox("You have registered")
'set login as default form
End If
Usually, if you need that much control over what happens at start-up, you just want to disable the a application framework. To do so, just un-check the Enable application framework check-box in the Application tab of the My Project settings designer window. Once you un-check that, you will be able to change the Startup object to Sub Main. Then you can add a new module with a Main method, like this:
Module Module1
Public Sub Main()
Application.EnableVisualStyles()
If String.IsNullOrEmpty(My.Settings.username) Then
Application.Run(New RegisterForm())
Else
Application.Run(New LoginForm())
End If
End Sub
End Module
Be aware, however--by disabling the application framework, you will loose the other automatic functionality that it provides, such as ApplicationEvents. If you want to use the application framework, you can accomplish the same thing by simply setting the MyApplication.MainForm property in the MyApplication.Startup event:
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
If String.IsNullOrEmpty(My.Settings.username) Then
Me.MainForm = New RegisterForm()
Else
Me.MainForm = New LoginForm()
End If
End Sub
End Class
Alternatively, you could always show the same form, but then have the form contain nothing but a single UserControl. Then you can simply switch which UserControl is displayed depending upon the settings. The user-controls would need to include all of the controls that would have otherwise been placed on the two different forms.

VB using me.close before opening new form

Hi Guys i'm new to Visual Basic Coding, and i can't seem to get where's my mistake on my coding, i'm trying to create a button that opens a new form while closing the current form.
i have two forms, form 1 is MainForm, form 2 is SearchForm
Whenever i use this code:
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
MainForm.Close()
SearchForm.Show()
End Sub
End Class
it will generate an error and says i need to replace MainForm.Close() into Me.Close()
When i Use this
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
Me.Close()
SearchForm.Show()
End Sub
End Class
It closes both Forms and it doesn't leave any Form Open. Kindly direct me to the proper path, thanks in advance.
You need to Hide the form rather than closing it. Since it's your main form, when it closes, the application exits.
Standard UI guidelines are to leave the main form open, and open search form on top of that. If you need to block the main form, while search criteria are selected, use .ShowDialog, instead of just .Show.
.NET WinForms programming pattern kind of implies that you never close your main form. If you deviate from this approach, you are guaranteed to encounter all sorts of layout and display issues. So please don't. You can .Hide the main form, if it needs to go to system tray or run in background.

Name 'AdobeReader' is not declared vb.net

I'm trying to follow a tutorial but I keep getting the warning:
Name 'AdobeReader' is not declared
Can sombody tell me what is wrong? The tutorial is found here, and I'm trying out Answer 2 from that page, which says:
Here is another way to read pdfs.
Right click on any of the tabs in your toolbox and click choose
items.(Just make sure you have a version of adobe reader installed
before starting.) When the dialog box pops up click the 'com
components' tab then pick tha 'adobe pdf reader' com component. Click
ok and you should now see a 'adobe pdf reader' control in your
toolbox. Click this and drag it to your vb form.(you may need to
resize it, usually appears very small). Now in order to read any pdf
just alter the 'src' property of the control either in code or in the
designer. when you run the program you should be able to view the pdf
doc in the vb form with most of the options you get in adobe reader.
Here is how you would alter the 'src' property in code.
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
AdobeReader.src = "C:\mydoc.pdf"
End Sub
If I´m getting your Question right, you just want to open a PDF-File.
Simply call:
Process.Start("Path/To/MyPdfFile.pdf")