Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Dec 2009
    Location
    Belfast
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm totally new to Visual Studio, and I have to create a simple project for class, which has 2 forms. The 1st Form is a Username & Password form that opens a second form when the Username & Password are entered.

    It works, kind of, except the Form that opens is not the Form I've created (called "TheMainForm"). It's just a blank Form.

    I've referenced a VB book, Google, and even the VB for Dummies website, and they all point to the same code:

    Code:
    Dim oForm As TheMainForm
            oForm = New TheMainForm()
            oForm.Show()
            oForm = Nothing
    However, when I type this in, VB keeps saying "TYPE: THE MAIN FORM IS NOT DEFINED"
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
    Dim strUsers, strPassword As String
    Dim oForm As TheMainForm
            oForm = New TheMainForm()
            oForm.Show()
            oForm = Nothing
    
    
    strUsers = txtUsername.Text
    strPassword = txtPassword.Text
    If strUsers = "Jean Brady" And strPassword = "M1" Then
    TheMainForm.Show()
    Else : TheMainForm.Hide()
    End If
    End Sub
    I must be really dumb because I just can't understand why this isn't working when every resource says it should.

    Can anyone tell me what I'm doing wrong?

    Many thanks in advance.

    Kyle

  2. #2
    New Lounger
    Join Date
    Dec 2009
    Location
    Quebec city
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about referencing your form with oForm instead of TheMainForm

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
    Dim strUsers, strPassword As String
    Dim oForm As TheMainForm
    oForm = New TheMainForm()
    oForm.Show()
    oForm = Nothing


    strUsers = txtUsername.Text
    strPassword = txtPassword.Text
    If strUsers = "Jean Brady" And strPassword = "M1" Then
    oForm .Show()
    Else : oForm .Hide()
    End If
    End Sub

    Hope this helps

    Ben

  3. #3
    New Lounger
    Join Date
    Dec 2009
    Location
    Belfast
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your reply.

    I tried changing to the code you suggested, but it didn't make a difference.

    When the Application runs, I just get a blank new form, not 'TheMainForm'.

    And VB still says that TheMainForm is not declared.

    Any further thoughts what's wrong?

    Many thanks again for your help.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Not declared or not defined?

    I have not ventured into the world of .Net, but... if you create a form and it is not automatically recognized as an object in your project, then presumably there is an extra step required to make it available to your procedure. Whether this is through a checkbox or through a declaration of some kind, I don't know.

    Perhaps this more targeted Google search will lead to the answer: type "is not defined" site:microsoft.com.

  5. #5
    New Lounger
    Join Date
    Dec 2009
    Location
    Belfast
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your reply.

    I stumbled across the problem.

    "TheMainForm" used to be called frmmainform, but I renamed it "TheMainForm". It was only when I tried suggestion #3 on the link you sent (Global.System.Object) that VB autocompleted the statement with .frmmainform, and it clicked that VB still saw it with its old name?

    I still can't see anywhere in the Property Dialog where it's referred to as frmmainform, but at least now I know what the error was, and it works OK!

    Thanks for the link and for both your help.

  6. #6
    Lounger
    Join Date
    Apr 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Look at the Solution explorer. I have some (very) weird cases where you can rename the form in the properties, but if don't rename the source file too it will only recognize the old name. This shouldn't happen and I have no idea why it does and why it only happens very rarely.

    Also, try and find the Class definition (it's at the top of the code page and should be
    Code:
    Public Class <formName>
    ). If this is displaying the old name, you will have this issue.

    Oh the pain, the pain of it all!

    Ciao

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •