How can i export or compile a form that i make with code within a running application in VB.NET?
I'm making a simple form making application, i've got almost everything covered, with creating the new form and adding controls and stuff.
Example: create a new form with clicking a button:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim NewForm As New Form
NewForm.Name = "Form1"
NewForm.Text = "Form1"
NewForm.Width = 300
NewForm.Height = 300
NewForm.Show()
End Sub
How do i export or compile the new form to a standalone exe?
Any help is greatly appreciated.
Create your own file format (XML?) that describes the contents of the form.
Then, create a separate application that reads these files and display a form based on the file that it read.
If you want to ship single EXEs, you could embed the file as a resource in the launcher application and modify that resource to save custom EXEs.
Alternatively, you could use Roslyn or Mono Cecil or Expression trees to actually compile that code at runtime into a new EXE; that may be harder.
Related
I've went to publish my first vb.net windows form and was surprised the format I meticulously combed over became all skewed after publishing.
Why did the controls change to older style '3d' and the overall ratio of the form stretched in the horizontal? My calendar date pickers also changed format.
Is there a way to make the published form look exactly like the release version, down to the pixel?
For anyone experiencing appearance issues, I can share that in my case, this seems to be related to my project starting first in visual studio 2019 and then I changed over VS 2022. I am happy to report that as soon as I copied my code over to a clean project in 2022, everything works great! The published copy looks completely identical to the release/debug version and I didn’t need to play with any settings in the Main Sub!
Thank you djv for working with me!
You can control visual styles before you create your UI thread, but VB.Net will enable visual styles for you in Sub Main, which is inaccessible to you! You can get around this by creating your own Sub Main, but you also need to choose this new method as the entry point to your application.
' in a new class file, I called Program.vb
Public Module Program
<STAThread>
Public Sub Main()
Application.VisualStyleState = VisualStyles.VisualStyleState.NonClientAreaEnabled
Application.Run(New Form1())
End Sub
End Module
In the project properties, click Startup object, choose Program
My Form1 has two TextBoxes. Here's the code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TextBox1.BorderStyle = BorderStyle.FixedSingle
Me.TextBox2.BorderStyle = BorderStyle.Fixed3D
Me.FormBorderStyle = FormBorderStyle.FixedSingle
End Sub
End Class
and here is the result
Note with Application.EnableVisualStyles() instead, I'm getting all single borders.
Hope some setting of visual styles does the trick
I am having trouble figuring out how to run code on application startup. The scenario is, I need to check if a user setting exists, if it does not, then I need to open a company configuration form. I tried creating a module to store application level events, but get an error:
BC31418 Visual Basic AND VB.NET in modules must specify a 'WithEvents' variable qualified with a single identifier.
Module ApplicationEvents
Private Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
If My.Settings.xmlpath = "" Then
Dim f As New CompanySetup()
f.ShowDialog()
f.Dispose()
End If
End Sub
End Module
I really do not want to put the IF statement into the default form mybase.load handler, since this application is in early stages, and the default form may change, leading me to have to move this code around. Any help on this error, or suggestions of how to get the IF statement to occur on application startup without tying it into a sub on the default form would be greatly appreciated.
Get rid of that module. Open the Application page of the project properties and click the View Application Events button. That will open the proper code file in which to create a Startup event handler. You can do that using the second and third drop-downs in the Navigation Bar at the top of the code window.
I am used to creating (Excel) VBA user form programs in a MVC style, and have recently been looking at VB.Net a bit more.
In general, I would create a MVC style user form application in VBA by creating an 'Initialize' function which creates an instance of a 'Controller' module and utilizes a 'LaunchProgram' method contained within this controller. This method would create an instance of the model and view used by the application, and then present the view to the user.
For example,
Public Sub Launch()
With New Controller
.Present
End With
Debug.Print "Execution ended"
End Sub
and then in the Controller (class) module I would have code such as
Private WithEvents m_View As View
Private m_Model As Model
Public Sub Present()
Set m_View = New View
Set m_Model = New Model
m_View.Show vbModal
End Sub
My issue with VB.Net is that there is no obvious way to create an equivalent of the 'Launcher' sub, which controls the execution of the program, and instead just presents a default instance of the main form to the user upon running the program. I have attempted to replicate the desired architecture by creating a main form which is (supposed to be) immediately hidden upon starting the program. This hidden form (should) then create specific instances of all other forms, controlling how user input is processed and returned in accordance with the general MVC methodology.
However, this doesn't work since the VB.Net code...
Public mSetupForm As SetupForm
Public mOutputForm As OutputForm
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Debug.Print("Program initialized")
mSetupForm = New SetupForm
mOutputForm = New OutputForm
Me.Hide()
mSetupForm.Show()
End Sub
...does not hide Form1.
By default, VB.NET applications use the mysterious Windows Application Framework which means that the build process adds an entry point for you which automatically runs whichever form you use as the main form. You can access these settings via Project Properties > Application > Windows application framework properties.
If you don't want to use the framework, un-check the Enable application framework check box and then select that you want to use Sub Main as your Startup object. Then you can create a module with a Public Sub Main method which will be the entry point to the application.
In the main, if you want to show a form, just call Application.Run and pass it the instance of the form you want to show (you can call it multiple times in succession if you want to).
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.
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