Calling Experts in Visual Basic!

Started by Gold Man, November 01, 2013, 05:26:13 PM

Previous topic - Next topic
Hey guys, I seem to have a problem with my Visual Basic program. Ok, so I'm creating a sort of Login type program (this is sort of for fun and learning on my end, it's not a project for my Computer Science course :)), however I have a problem. Once the information is entered in the login, I want Form1 to close, and Form2 to open. However, I'm stuck with either both Form1 and Form2 closing altogether, or having to hide Form1, which doesn't fully close the program once Form2 is closed, meaning I have to either stop the debug in Visual Basic Studio 2010, or use Task Manager to close it. Here's the code from my program down below:

NOTE: This is in the Private Sub of Button1_Click. And the words for the login are just placeholders currently. :P


If MaskedTextBox1.Text = "Except" _
           And MaskedTextBox2.Text = "cool" Then
            MsgBox("Welcome to the system!")
            Form2.Show()
            Me.Close()
        ElseIf MaskedTextBox1.Text = "Epic" _
            And MaskedTextBox2.Text = "nice" Then
            MsgBox("Welcome to the system!")
            Form2.Show()
            Me.Close()
        Else
            MsgBox("Sorry, wrong password.")
        End If

November 01, 2013, 06:22:57 PM #1 Last Edit: November 01, 2013, 06:30:58 PM by tirpider
I'm assuming me.close() does not shut form1 down.


Can you close form1 with Form1.Close()  ?

-edit
And if I remember right, doesn't closing the startup form end the application in VB?
If that's the problem here, you may have to launch an initial hidden form so you can show, hide, and close other forms freely.

(not an expert in VB at all, I just poked at it long enough to find out I couldn't use it for the project I was on.)

Quote from: tirpider on November 01, 2013, 06:22:57 PM
I'm assuming me.close() does not shut form1 down.


Can you close form1 with Form1.Close()  ?

Me.Close actually shuts down both Forms in the program. I've tried Form1.Close, but Visual Basic Studio doesn't accept it, and instead requests I use Me.Close. I don't know why it will close both Forms, and not just Form1, that's what I don't understand.

November 01, 2013, 06:41:12 PM #3 Last Edit: November 01, 2013, 06:51:38 PM by tirpider
I bet it shuts them both down because the Form1 is the startup object.

This guy wanted to shut down a parent form and keep the child one going.  3 posts in is another link to an msdn article about changing the startup opject from a form to a sub.
http://social.msdn.microsoft.com/Forums/en-US/c3abcaa6-a284-485d-85ed-b83c6204ca9e/how-to-close-parent-form?forum=Vsexpressvb

I love the total lack of example code on Microsoft sites.


-edit
And as usual, stack overflow has an answer thats direct and to the point instead of technical posturing.

http://stackoverflow.com/questions/18601326/how-to-properly-close-form1-after-showing-form2


(stackoverflow.com is a really good place for programming Q and A. I just discovered them a couple of months ago and they always seem to have something.)

November 01, 2013, 06:52:48 PM #4 Last Edit: November 01, 2013, 07:00:04 PM by {Alpha}Gold Man
Quote from: tirpider on November 01, 2013, 06:41:12 PM
I bet it shuts them both down because the Form1 is the startup object.

This guy wanted to shut down a parent form and keep the child one going.  3 posts in is another link to an msdn article about changing the startup opject from a form to a sub.
http://social.msdn.microsoft.com/Forums/en-US/c3abcaa6-a284-485d-85ed-b83c6204ca9e/how-to-close-parent-form?forum=Vsexpressvb

I love the total lack of example code on Microsoft sites.

Ah, now I see how it works, you need to have the shutdown be when the program's final window closes. I knew there was an easier way behind it! :slap: Thanks for the help, tirpider, it works now! :D


GM, coding?.. No way!

No but seriously, glad to see you into that kind of stuff. It can be lots of addicting fun }: D

Quote from: -RepublicCommando- on November 01, 2013, 10:52:08 PM
GM, coding?.. No way!

No but seriously, glad to see you into that kind of stuff. It can be lots of addicting fun }: D

Haha, I kinda though that someone would catch that. ;)

But yeah, it is very interesting. I'm actually trying to work a login system in VB, just as an experiment for myself. So I know how to close the login form (Form1), and keep Form2 open, now I need to figure out how I could make the program allow someone to sign up, and not need a pre-programmed password and username to login. Although I think I'l look up an answer for that before I post the question here. :P

November 02, 2013, 09:26:09 AM #8 Last Edit: November 02, 2013, 09:31:20 AM by -RepublicCommando-
I'll try not to talk your ear off with the concept answer (because I'd just give the answer to you in code form if I knew the constants and functions exact names, I've never touched VB, but all scripts are basicly the same)
--
If you don't want a pre-programmed password, the password has to be stored in a variable
( x = string("Password") ), if you wanted to replace it, then you would use registering to override the variable. Then down the line, if the user hasn't registered ( x=0 then Msg("You've not registered yet!") (This is the concept;

Case Button1 = clicked
x = 0 //reset
then x = variable_that_password_editbox_stores_here// Write whatever is in the password edit field to the x variable for use.

I don't know if you use the global. prefix in VB, but you might need that for the X variable.

November 02, 2013, 12:51:31 PM #9 Last Edit: November 02, 2013, 03:11:24 PM by {Alpha}Gold Man
Quote from: -RepublicCommando- on November 02, 2013, 09:26:09 AM
I'll try not to talk your ear off with the concept answer (because I'd just give the answer to you in code form if I knew the constants and functions exact names, I've never touched VB, but all scripts are basicly the same)
--
If you don't want a pre-programmed password, the password has to be stored in a variable
( x = string("Password") ), if you wanted to replace it, then you would use registering to override the variable. Then down the line, if the user hasn't registered ( x=0 then Msg("You've not registered yet!") (This is the concept;

Case Button1 = clicked
x = 0 //reset
then x = variable_that_password_editbox_stores_here// Write whatever is in the password edit field to the x variable for use.

I don't know if you use the global. prefix in VB, but you might need that for the X variable.

Well, I did some work on the Login program. (Found a whole other tutorial that was for an Advanced Login, which let you create an account and then login, however it would only exist for the previous user). I tried doing the coding, but the MySettings in Visual Studio and Visual Basic 2010 Express are different somewhat. I tried using My.Settings.username and My.Settings.password, however they aren't accepted in Visual Basic 2010.

The thing I want to have happen is that the program will store a users' username and password in a sort of database, however I think I might need to add something to the program in order for a database to work on it. The thing I tried via the video tutorial would work, however there's no password or username in the MySettings of Visual Basic 2010 Express.

To cut it short, I need a substitute for My.Settings.username and My.Settings.password if I want it to at least work somewhat. The database system I'l have to look at later.

If anyone using Visual Basic 2010 wants to try it out, here's the coding for Form1 and Form2:

Form1
[spoiler]
Button1_Click
[spoiler]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox("Fill the form.", MsgBoxStyle.Critical, "Fill the form.")
            Me.Button1.Enabled = False
        ElseIf TextBox2.Text = TextBox3.Text Then
            Me.Button1.Enabled = True
            MsgBox("Your account has been created!", MsgBoxStyle.Information, "Congratulations")
            Form2.Show()
            Me.Hide()
            My.Settings.password = TextBox2.Text
            My.Settings.username = TextBox1.Text
            My.Settings.Save()
            My.Settings.Reload()
        Else
        End If
        End

        If Not TextBox2.Text = TextBox3.Text Then
            MsgBox("Password does not match!", MsgBoxStyle.Critical, "Error")
            TextBox2.Text = String.Empty
            TextBox3.Text = String.Empty
        End If



    End Sub

[/spoiler]

Button3_Click
[spoiler]

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form2.Show()
        Me.Hide()

    End Sub

[/spoiler]

TextBox1_Text Changed
[spoiler]

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox2.Text = "" Then
            Button1.Enabled = False
        Else : Me.Button1.Enabled = True
        End If
    End Sub

[/spoiler]

TextBox2_Text Changed
[spoiler]

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If TextBox2.Text = TextBox3.Text Then
            Me.Button1.Enabled = True
        ElseIf TextBox2.Text = "" Then
            Button1.Enabled = False
        End If
    End Sub

[/spoiler]

TextBox3_Text Changed
[spoiler]

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        If TextBox3.Text = TextBox2.Text Then
            Me.Button1.Enabled = True
        ElseIf TextBox3.Text = "" Then
            Button1.Enabled = False
        End If
    End Sub

[/spoiler]

Form1_Load
[spoiler]

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Enabled = False
    End Sub

[/spoiler]

[/spoiler]

Form2
[spoiler]
Button1_Click
[spoiler]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not TextBox1.Text = My.Settings.username Or Not TextBox2.Text = My.Settings.password Or TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("Error! Wrong username and/or password!", MsgBoxStyle.Critical, "Error")
        ElseIf TextBox1.Text = My.Settings.username And TextBox2.Text = My.Settings.password Then
            MsgBox("Welcome!" + My.Settings.username, MsgBoxStyle.Information, "Welcome")
            Form3.Show()
            Me.Hide()
        End If
    End Sub

[/spoiler]

Button3_Click
[spoiler]

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form1.Show()
        Me.Hide()
    End Sub

[/spoiler]

Form2_Load
[spoiler]

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        My.Settings.Reload()
        My.Settings.Save()
    End Sub

[/spoiler]

[/spoiler]

And if anyone wants to download and try Visual Basic 2010 Express, they're welcome to download it here:

http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express (Click on Visual Basic 2010 Express)

EDIT: Aaaaand I found another tutorial that made it all the more easier to understand. I even managed to input the database! And ranks! (Credit for the tutorial/coding goes to maxter1999 on YouTube). So now I know how to work out the Login system successfully, I just need to combine the Login program and it's database with another form, which hopefully shouldn't be too hard to manage. :P

Also, should I put up my own thread on Visual Basic? I think I might decide to make a whole little thread about my progression through it (or if Led is gracious enough, my own sub-forum? :D) I'l admit the last one is wishful thinking, but I think I'l put up my own thread for Visual Basic (if you guys allow it).