How to Access TextBox in my own class
-
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 FunctionPublic 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 Classnow 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,
-
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 FunctionPublic 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 Classnow 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,
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
-
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
Thxx Sir, but it isnt working eithr :( it is giving hell lot of errors
-
Thxx Sir, but it isnt working eithr :( it is giving hell lot of errors
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