Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to Access TextBox in my own class

How to Access TextBox in my own class

Scheduled Pinned Locked Moved Visual Basic
helptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rizwan Rathore
    wrote on last edited by
    #1

    Hi I am totally a new commer in VB or VB . Net i m trying to make a simple windows application. that has two textboxes in it. i want to make my own student class with two members Name and gpa. and two methods one to assign the values to the variables and the 2nd one to show them in the textboxes. and i want to access this class in another class by creating its object in it. i am having following problems. i) textboxes r not accessible in my student class. ii)when i added these variables and function in the Form1 class (for the textboxes to b accessible) and created the object of Form1 in my own class i was unable to access the variables and methods of Form1 class. I am giving the code as well.

    Public Class Form1
    Inherits System.Windows.Forms.Form
    Private name As String
    Private gpa As Double
    Public Function assignvalue()
    name = InputBox("Enter the name of students")
    gpa = InputBox("Enter the name of studentsFather")
    End Function

    Public Function showvalue()
        textbox1.Text() = name
        textbox2.Text() = gpa
    End Function
    

    Public Class MyClass
    Dim s1 As New Form1
    s1.assignvalue()
    s1.showvalue()
    End Class
    End Class

    now when i run this program it gives error on s1.assignvalue() and s1.showvalue() D:\VB Projects\WindowsApplication1\Form1.vb(168): Declaration expected. somebody plzz help me out looking forward for help Regards,

    M 1 Reply Last reply
    0
    • R Rizwan Rathore

      Hi I am totally a new commer in VB or VB . Net i m trying to make a simple windows application. that has two textboxes in it. i want to make my own student class with two members Name and gpa. and two methods one to assign the values to the variables and the 2nd one to show them in the textboxes. and i want to access this class in another class by creating its object in it. i am having following problems. i) textboxes r not accessible in my student class. ii)when i added these variables and function in the Form1 class (for the textboxes to b accessible) and created the object of Form1 in my own class i was unable to access the variables and methods of Form1 class. I am giving the code as well.

      Public Class Form1
      Inherits System.Windows.Forms.Form
      Private name As String
      Private gpa As Double
      Public Function assignvalue()
      name = InputBox("Enter the name of students")
      gpa = InputBox("Enter the name of studentsFather")
      End Function

      Public Function showvalue()
          textbox1.Text() = name
          textbox2.Text() = gpa
      End Function
      

      Public Class MyClass
      Dim s1 As New Form1
      s1.assignvalue()
      s1.showvalue()
      End Class
      End Class

      now when i run this program it gives error on s1.assignvalue() and s1.showvalue() D:\VB Projects\WindowsApplication1\Form1.vb(168): Declaration expected. somebody plzz help me out looking forward for help Regards,

      M Offline
      M Offline
      MickYL
      wrote on last edited by
      #2

      Well It would be good to get a better answer than this. Its a kind of scope problem and it seems naming a class myClass is not liked and similar conflict naming a variable name, they are keywords. So I changed it a little bit Public Class Form1 Inherits System.Windows.Forms.Form Dim t1 As TextBox Dim t2 As TextBox Private name1 As String Private gpa As Double Public Function assignvalue() name1 = InputBox("Enter the name of students") gpa = InputBox("Enter the GPA") showvalue() End Function Public Function showvalue() TextBox1.Text() = name1 t1.Text() = gpa End Function Public Class displayme Public Shared Function show() Dim s1 As New Form1 s1.assignvalue() s1.showvalue() s1.Show() Dim t1 = New TextBox Dim t2 = New TextBox s1.Controls.AddRange(New Control() {t1, t2}) t1.Location = New Point(20, 50) t2.Location = New Point(20, 100) End Function End Class Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click displayme.show() End Sub End Class

      R 1 Reply Last reply
      0
      • M MickYL

        Well It would be good to get a better answer than this. Its a kind of scope problem and it seems naming a class myClass is not liked and similar conflict naming a variable name, they are keywords. So I changed it a little bit Public Class Form1 Inherits System.Windows.Forms.Form Dim t1 As TextBox Dim t2 As TextBox Private name1 As String Private gpa As Double Public Function assignvalue() name1 = InputBox("Enter the name of students") gpa = InputBox("Enter the GPA") showvalue() End Function Public Function showvalue() TextBox1.Text() = name1 t1.Text() = gpa End Function Public Class displayme Public Shared Function show() Dim s1 As New Form1 s1.assignvalue() s1.showvalue() s1.Show() Dim t1 = New TextBox Dim t2 = New TextBox s1.Controls.AddRange(New Control() {t1, t2}) t1.Location = New Point(20, 50) t2.Location = New Point(20, 100) End Function End Class Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click displayme.show() End Sub End Class

        R Offline
        R Offline
        Rizwan Rathore
        wrote on last edited by
        #3

        Thxx Sir, but it isnt working eithr :( it is giving hell lot of errors

        M 1 Reply Last reply
        0
        • R Rizwan Rathore

          Thxx Sir, but it isnt working eithr :( it is giving hell lot of errors

          M Offline
          M Offline
          MickYL
          wrote on last edited by
          #4

          Sorry, I think i made it worse. I will look at it, im just learning so it is interesting. Especially the scope part of it. Maybe try separating the classes a bit more, instead of having one inside another. But I am sure we will sort this out. But it works if you keep your class outside the other class. I don't think nested classes are going to work! Public Class Form1 Inherits System.Windows.Forms.Form +windows Generated Code----------- ---------------------------------- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As New aClass Label1.Text = f.myfunction Label1.Text = f.showName End Sub End Class Public Class aClass Private strName As String Private gpa As Double Public Function myfunction() strName = InputBox("Enter the name of students") End Function Public Function showName() Return strName End Function End Class

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups