How can I save application settings in a VB .NET application? - vb.net

I made a projet a month ago in C# where I used application settings to save data. This time, I'm making a project in VB .NET where I need to store data. I have a textbox where the user input a certain key and if he gets it right, it sets a setting to true. Here's the code.
Public Class Form4
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = ("CF4A438C1F68D" Or "7552B2C629D11" Or "9C9C94A73141A" Or "7EE3998DAB3D3" Or "FC4DABC9CA7EA" Or "FDEE4B1F2C113" Or "D773E97B47531" Or "AABBBF181D39A" Or "36F37B39D19A5") Then
Properties.Settings.Default.ActivatedVersion = True
Properties.Settings.Default.Save()
End If
End Sub
End Class
Unfortunately, when I write Properties, it doesn't work. How can I do this?

In VB, you use My.Settings rather than Properties.Settings.Default.
Also, you don't necessarily need to call Save explicitly as it will happen automatically, by default, when the application exits. It won't happen if the application crashes but, if you have done the right thing and handled the UnhandledException event, the application will not crash, even if an unhandled exception is thrown.

Related

VB Forms don't recognize each other in the same project

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).

How can I handle application startup without using default form mybase.load

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.

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.

Cause program crash on purpose in VB.NET

I'd need a way to cause the program to crash on purpose when i click a button. But nothing comes to my mind that would still allow me to compile the program. any code that causes a hard crash for whatever reason. in particular i need it to close and not be able to continue. My beta testers need to test the recovery after crash feature. Thanks!
these things never happen when they should..
How about just throwing an unhandled exception?
Private Sub btnCrash_Click(sender As System.Object, e As System.EventArgs) Handles btnCrash.Click
Throw New System.Exception("The program has crashed catastrophically!")
End Sub
To effectively kill the process use Environment.FailFast() in a button click handler, like this:
Protected Sub buttonCrash_Click(sender As Object, e As EventArgs) Handles buttonCrash.Click
Environment.FailFast()
End Sub
This will not generate any exceptions, etc., it is the same as going to Windows Task Manager and killing the process.
Here is the documentation for Environment.FailFast Method (String)
How about the Environment.FailFast method?
Private Sub btn_click(...)
Environment.FailFast("Failure!")
End Sub