Results 1 to 9 of 9

Thread: [RESOLVED] Type 'label', 'textBox', 'button' is not defined

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    54

    Resolved [RESOLVED] Type 'label', 'textBox', 'button' is not defined

    I am a beginner at Visual Basic and Im using the following to help me learn:

    1. Visual Basic 2005 Express Edition;
    2. Deitel's Visual Basic.NET second edition; and
    3. Learn Visual Studios videos .

    I keep getting the following errors when I try to run a class from Deitel's book (see below) :

    Type 'label' is not defined
    Type 'textBox' is not defined
    Type 'button' is not defined

    I have 3 questions:
    1. How do I run a class? The code below has no main and Visual 2005 does not 'generate' any additional code for me.
    2. Which references do I have to add to get rid of the above mentioned error messages? And why should I have to constantly be adding these references when I think Visual 2005 should automatically do this for me?
    3. I save this project as a Class cause it did not work as a Windows project. Was this correct?
    Any assistance will be appreciated. Thank you in advance.

    Code:
    ' Fig. 6.19: Factorial.vb
    ' Calculating factorials using recursion
    
    Public Class Class1
    
    Inherits System.Windows.Forms.Form
    
    Friend WithEvents lblEnter As label ' intellisence does not recognize Label
    Friend WithEvents lblFactorial As label ' intellisence does not recognize Label
    
    Friend WithEvents txtInput As TextBox 'intellisence does not recognize TextBox
    Friend WithEvents txtDisplay As textBox 'intellisence does not recognize TextBox
    
    Friend WithEvents cmdCalculate As Button ' intellisence does not recognize Button
    
    
    ' Visual Studio .NET generated code (Visual Basic 2005 does not generate any code)
    
    Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalculate.Click
    
    Dim value As Integer = Convert.ToInt32(txtInput.text)
    Dim i As Integer
    Dim output As String
    
    txtDisplay.Text = ""
    
    For i = 0 To value
    txtDisplay.Text &= i & "! = " & Factorial(i) & vbCrLf
    Next
    
    End Sub ' cmdCalculate_Click
    
    ' recursively generates factorial of number
    Function Factorial(ByVal number As Long) As Long
    
    If number <= 1 Then
    Return 1
    
    Else
    Return number * Factorial(number - 1)
    End If
    
    End Function
    
    
    End Class

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Type 'label', 'textBox', 'button' is not defined

    They are showing you the code the was autogenerated by the form's designer.
    Just start a normal windows project and add those five items to the form.
    Then, add the rest of the code, i.e.

    Code:
    Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalculate.Click
    
    Dim value As Integer = Convert.ToInt32(txtInput.text)
    Dim i As Integer
    Dim output As String
    
    txtDisplay.Text = ""
    
    For i = 0 To value
    txtDisplay.Text &= i & "! = " & Factorial(i) & vbCrLf
    Next
    
    End Sub ' cmdCalculate_Click
    
    ' recursively generates factorial of number
    Function Factorial(ByVal number As Long) As Long
    
    If number <= 1 Then
    Return 1
    
    Else
    Return number * Factorial(number - 1)
    End If
    
    End Function
    Last edited by FourBlades; Apr 12th, 2008 at 06:31 PM.

  3. #3
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Type 'label', 'textBox', 'button' is not defined

    The following code was only part of the code from the actual form that is generated automatically for you when you drop those five controls on the designer surface:

    Code:
    Public Class Class1
    
    Inherits System.Windows.Forms.Form
    
    Friend WithEvents lblEnter As label ' intellisence does not recognize Label
    Friend WithEvents lblFactorial As label ' intellisence does not recognize Label
    
    Friend WithEvents txtInput As TextBox 'intellisence does not recognize TextBox
    Friend WithEvents txtDisplay As textBox 'intellisence does not recognize TextBox
    
    Friend WithEvents cmdCalculate As Button ' intellisence does not recognize Button
    Your were supposed to realize that and deal with the partial code as an example...

    What you should do is what I posted above.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Type 'label', 'textBox', 'button' is not defined

    remove these 5 lines

    vb Code:
    1. Friend WithEvents lblEnter As label ' intellisence does not recognize Label
    2. Friend WithEvents lblFactorial As label ' intellisence does not recognize Label
    3.  
    4. Friend WithEvents txtInput As TextBox 'intellisence does not recognize TextBox
    5. Friend WithEvents txtDisplay As textBox 'intellisence does not recognize TextBox
    6.  
    7. Friend WithEvents cmdCalculate As Button ' intellisence does not recognize Button

    then add the controls to your form:
    2 labels, named lblEnter + lblFactorial
    2 textboxes, named txtInput + txtDisplay
    + a button, named cmdCalculate

  5. #5
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Type 'label', 'textBox', 'button' is not defined

    No, start a whole new Windows project, add those five controls, add the additional code shown in post #2 and then run the project.
    Post back with any further questions.

  6. #6
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Type 'label', 'textBox', 'button' is not defined

    You can do it the way you were first describing, but it's the hard way to do it. The following code (and more) is written for you as a convenience and a time saving tool when you drop controls onto the design surface of a form in a Windows project.

    Code:
    Inherits System.Windows.Forms.Form
    
    Friend WithEvents lblEnter As label ' intellisence does not recognize Label
    Friend WithEvents lblFactorial As label ' intellisence does not recognize Label
    
    Friend WithEvents txtInput As TextBox 'intellisence does not recognize TextBox
    Friend WithEvents txtDisplay As textBox 'intellisence does not recognize TextBox
    
    Friend WithEvents cmdCalculate As Button ' intellisence does not recognize Button

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Type 'label', 'textBox', 'button' is not defined

    yes. do as fourblades suggested in post #5

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Type 'label', 'textBox', 'button' is not defined

    When you start a new Windows Application project stop immediately after creating the new project, go to Solution Explorer, click on the "Show All Files" icon, expand the Form1.vb treenode, Double click on the Form1.Designer.vb file and observe the code.
    Next add the five controls in the example to the Form1.vb [Design] tab (the Form).
    Next go back to the Form1.Designer.vb tab and look at the code again.
    You then understand what they were showing you in their code example a little better.
    The Designer file is the time saver and convenience code generator i mentioned above. It creates the other half of your Form1 class.
    Note the second line of that file:
    "Partial Class Form1"
    Next look up Partial classes on MSDN

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    54

    Re: Type 'label', 'textBox', 'button' is not defined

    Yes it worked! Thank you FourBlades and Paul for your quick and very helpful advice.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim value As Integer = Convert.ToInt32(txtInput.text)
            Dim i As Integer
            Dim output As String
    
            txtDisplay.Text = ""
    
            For i = 0 To value
                txtDisplay.Text &= i & "! = " & Factorial(i) & vbCrLf
            Next
    
        End Sub 
    
        ' recursively generates factorial of number
        Function Factorial(ByVal number As Long) As Long
    
            If number <= 1 Then
                Return 1
    
            Else
                Return number * Factorial(number - 1)
            End If
    
        End Function
    
    
    End Class

Posting Permissions

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



Click Here to Expand Forum to Full Width