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
Related
I'm coding a project in Visual Basic using Visual Studio 2022 and want to start the program with a particular form. I have currently 8 forms. When I go to Solution Explorer/Properties/Application and select Startup Form, only a form named Form1 is in the dropdown list.
I have tried as much as I'm game as I'm afraid of messing it all up. I'm a new self taught programmer. Hope someone can help with not too much tech jargon.
Leif
I have the same issue. Both "Startup form" and "Enable application framework" are broken in Visual Studio 2022 when using VB.NET, WinForms, and .NET Framework. These both work if you target .NET 6.
My workaround is to close VS 2022, open the solution in VS 2019, set the Startup form, save, close VS 2019, and then re-open the solution in VS 2022.
I think In Visual studio 2022 IDE Designer tool deleted the application.designer.vb file content when startup form need to be change instead of modifying it.
This bug associate with enable application framework.
Create another framework project and make a backup of this file and modify as you want.
Don't stick with IDE...
they also creating IDE HELL (previously DLL l0l)
for example I paste application.designer.vb file content here
Option Strict On
Option Explicit On
Namespace My
Partial Friend Class MyApplication
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Public Sub New()
MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
Me.IsSingleInstance = false
Me.EnableVisualStyles = true
Me.SaveMySettingsOnExit = true
Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses
End Sub
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.WindowsApp1.Form1
End Sub
End Class
End Namespace
=========================
IF want to stick with IDE
Just change the Application.MyApp file and edit MainForm tag
<MainForm> Write form name (without extension) </MainForm>
Write whatever your form name and get it on list....
no need to modify code...
Look in the Main method, there should be an Application.Run(New Form1()) call or similar. You can change Form1 to the form you want to run.
Remember that in the project properties you must set Main as the start object.
With .NET Framework you can create a class (eg. Program) with a static method (Shared in VB) called Main and set this method as startup object in the project properties and, as above mentioned, uncheck Enable application framework flag.
In the Main method you can start your preferred form using Application.Run.
Public Class Program
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(True)
Application.Run(New Form2())
End Sub
End Class
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.
Hi everyone I need some help,
I am having a weird situation every time I try to call a window form I get this error
BC30469 Reference to a non-shared member requires an object reference.
I was originally working on Visual Studio 2010 when the first time contouring this problem so I thought that I may accidentally deleted or edited some code in the declaration of the form witch caused the problem so I closed the solution and created a new one to make sure that the problem is limited the solution not to VS. then I add window form "Form2" then created/added a button1 on Form1 to call Form2.Show()
simple code that should work fine but when I tape Form2.Show() it give that ERROR and red mark Form2
so I uninstalled VS2010 then Reset Windows 10 with option to wipe out all data on windows partition (I now that was extreme but I suspected that maybe the system was infected with some virus "prior action") so after that I checked the system with HitmanPro and found nothing then I installed VS2019 Community and get The some problem I searched on the web but did not found any similar case so here I am hoping that someone will resolve the mystery.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
End Class
Form2 is empty form I didn't make any change on it
Before this problem showing up everything work fine now even old project have the same issue
Thanks
Edit: Add project as simple
https://mega.nz/file/FgoXkCwA#ootxYrXGnR6sQR_Pifjvz617-r_Az1ozXWB49oGxqKU
the project dose not contain any executable file
I usually do something like this when calling a subform:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' using a Using block:
Using form2 as New Form2
form2.ShowDialog(Me)
form2.Close()
End Using
' using a With block
With New Form2
.ShowDialog(Me)
.Close()
End With
End Sub
ShowDialog(Me) keeps the subform open until a DialogResult is provided by the user (OK or Cancel usually).
The problem is happening on 2 of the company's computers and on a client computer, but we can not identify a pattern.
I was able to reproduce the error using a simple program that only opens an OpenFileDialog. The program must be run by the generated executable itself (NOT by Debug) and it is still running in the background even after closing. Below is the code of the program, along with a link to download the project and a video demonstrating the error.
Code:
Public Class Form1
Private ofdAbrir As Windows.Forms.OpenFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ofdAbrir = New Windows.Forms.OpenFileDialog
ofdAbrir.ShowDialog()
ofdAbrir.Dispose()
ofdAbrir = Nothing
End Sub
End Class
As you can see in the code above, I only have one form, so it is not the case that some form remains open and it is also not related to threads running since none is created.
To reproduce the problem, click on Button1, cancel the OpenFileDialog and try to close the form (clicking on X). The form apparently will close, but you will see at task manager that it still running. The big mystery is that this problem does not happens in all computers.
Video: https://drive.google.com/open?id=1sfdVUGQlwYNCQkl1Ht-cJSOb4433sqnT
Project: https://drive.google.com/open?id=1d4oJYUjaaZ9xnRj4CX3HXOQPqwMZmE0V
I couldn't reproduce the problem, but how about using this method:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using ofdAbrir As New Windows.Forms.OpenFileDialog
ofdAbrir.ShowDialog()
MsgBox(ofdAbrir.FileName)
End Using
End Sub
I've seen the video u posted...It is not your application that is running rather it is the vsHost.exe that is running :)...The problem shouldn't occur if u run the app outside visual studio.
If the problem is still there,just disable Enable Diagnostic Tools while debugging from Tools > Options > Debugging > Uncheck Enable Diagnostic Tools while debugging
Also u can disable Visual Studio hosting service from Project > Project Properties > Uncheck Enable the Visual Studio hosting service
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.