• Simple Code not working?

    Home » Forums » Developers, developers, developers » DevOps Lounge » Simple Code not working?

    Author
    Topic
    #464671

    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

    Viewing 4 reply threads
    Author
    Replies
    • #1190418

      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

    • #1190799

      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.

    • #1190859

      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.

    • #1190975

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

    • #1196471

      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 

      ). If this is displaying the old name, you will have this issue.

      Oh the pain, the pain of it all!

      Ciao

    Viewing 4 reply threads
    Reply To: Simple Code not working?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: