(VB.NET) Why doesn't my login system work? - vb.net

OK. I have a code, and it should work, it DOES close the login form, but doesn't open the menu form.
If username.Text = "lolman8776" Then
If password.Text = "#########PASSWORD HIDDEN FROM THE INTERNET#########" Then
Form2.Show()
Me.Close()
End If
End If
What I don't understand, is that this code has no syntax errors and SHOULD WORK!
It shows form2 and THEN it closes itself, but Form2 never shows up.
I'm running VB.Net 2013 Community, as it was a free download. (I also registered it so it wasn't trial).
I have tried removing Me.Close() from Form1 and putting a line of code into Form2 that would close Form1:
Form1.Close()
But, still nothing. I do not know why ANY METHOD won't work. I've searched high and low for an awnser, but to no success. Does anyone have a solution?

According to your comment: "I tried not even hiding/closing form1, but just showing Form2 will cause the app to break."
I believe your issue lies around not instansiating your Form. The Show() method is not static and cannot be called directly on Form2. It must be called on an instance of your Form. For example:
Dim myForm As New Form2()
myForm.Show()
Please refer to this for a little more information.

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

Hard Time Closing Forms

Alright, so I am writing a program in VB that has 2 forms. These forms are to rotate via button click and be able to close the entire program no matter what form is being shown. As of now, what is happening is, that if I launch the program and click X to close, it will close. However, if I click the button to show Form2, the close button X doesn't close the program and the program keeps running. Also consider that if I change from Form1 to Form2, then back to Form1, Form1 no longer has the ability to close the program like it does before the button click. Any help? This the code I am using in each for, minus the close commands. I feel like the command for closing the forms needs to be outside of the Private Sub for the Button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Switches to Daily
Dim FirstForm As New Form1
FirstForm.Show()
Me.Hide()
'TODO: Close Program on X
End Sub
me.Close() ' Will close the form.
application.Exit ' Will close the application.
Read through .NET End vs Form.Close() vs Application.Exit Cleaner way to close one's app for a better description for this. Some good learning.

how to re-display form2 in hide mode from Form3?

form2.Hide() [ currently at Form 3 ]
form1.Close()
(different) form1.Close()
is there anyway, by any chance i can bring up the Form2 that is in Hide mode? from From 3? I have been looking around.
i had try to do Form2.Show(). However, the system inform me " Reference to a non-shared member requires an object reference ".
This project i am doing, I use pass information from 1 form to another.
From Form1 - 3 passing info the information have no prob. The problem now is that from Form3 i would like to re-display the Form2 (which is in Hide mode). but it do not allow me to do so. so far i see is that i can only type the code Form2.ActiveForm.Show(). am sorry for my long message. did really hope for help cause i can't find solution on the net. could be i fail to ask to make a hit on google
the answer so far can be found on this link given. please read through the commend for your details.
what is the different in form2.Close() and Form3.ActiveForm.Close() in VB
Not sure if its the best solution but if I understand you right.
If you were to have a "Globals" Module and add form2 as a variable in there:
Public Form2 As New Form2
You would then be able to control it from other forms without it becoming invalid e.g.
Global.Form2.Hide
Global.Form2.Show
And even modify the controls and values inside it from other forms:
Global.Form2.ThisLabel.Text = "TEXT"
This means form3 can have full control over your "form2" even while its hidden.

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.

MessageBox persistence in VB.net

I'm using MessageBox to give some information to the user, but when such a box pops up, I want it to block access to the main window. So, until the user has clicked "OK", they should not be able to click (or even focus on) the window that's below it.
Does anybody know how to do this? I've noticed that MessageBox has very few functions, so maybe I'll even have to use a different object for this.
A quick and dirty solution could be a dialog form (Project->Add Windows Form->Dialog). This gives you visual extensibility of your MessageBox.
You can call your MessageBox's ShowDialog method and it will of course block access to the parent window.
There are of course other methods.
Example:
' This is your Dialog you created by going Project->Add Windows Form->Dialog
Public Class MessageBoxDialog
Public Overloads Sub ShowDialog(ByVal message As String)
Me.txtMessage.Text = message
ShowDialog()
End Sub
End Class
' This is the form you want to call the message box on
Private Sub btnShowMsgBox_Click(sender As System.Object, e As System.EventArgs) _
Handles btnShowMsgBox.Click
Dim messageBox As New MessageBoxDialog
messageBox.ShowDialog("This is another way to show a MsgBox but allows greater extensibility.")
End Sub
Just after I posted here, my friend came up with the answer. By calling MessageBox.Show(mf, "text") where mf is the main form, mf will be disabled as long as the OK button has not been clicked. I suppose this question was a bit silly to post after all, but I hope it might help others if they're stuck with the same problem.