Beginner (Form and Module)

Enknown

Newcomer
Joined
Apr 30, 2003
Messages
4
Location
Sweden
I am totally new with VB.NET...

I have a question here,
I first created a Form (Form1) and plant a TextBox (TextBox1) on it.
Then I created a Module (Module1), in this Module I have Sub Main()
Now I want to retrive the value from TextBox1, how would I do that?

In VB6 I would just do *something* like this:

Code:
Sub Main()
  Dim sTest as String
  sTest = Form1.TextBox1.Text
End Sub

But How would I do It in VB.NET?
 
Visual Basic:
Sub Main()
  'Set frm1 to your form1
  Dim frm1 as Form1
  Dim sTest as String
  sTest = Form1.TextBox1.Text
End Sub
In .net you need to dim each form agen.
//edit: sry if the code was incorect, did nog have a comp with vb.net near at that time
 
Last edited:
Er, that is partially correct. You need to create a new variable to
hold a new instance of the form, and then show the form using
Application.Run() (only in Sub Main, that is).

Visual Basic:
Public Shared Sub Main()
  Dim frm1 As New Form1() ' A new instance of Form 1
  Dim sTest As String

  Application.Run(frm1) ' The code stays here until frm1 is closed

  sTest = frm1.TextBox1.Text
End Sub ' Now the program ends
 
I have absolutely no idea what you're trying to do, but I can tell you right now that that code will not do it.

1) You should not ever ever ever use modules, due to the fact that they eliminate the concept of OOP -- always use Shared procedures inside classes.
Visual Basic:
'in MyClass.vb or whatever
Public Shared Sub Main()

2) You need to instantiate the instance of your form:
Visual Basic:
Dim frm1 As [b]New[/b] Form1

3) If you are using Sub Main(), you must manually start the main window message loop, using Application.Run(). After lines, put
Visual Basic:
Application.Run(frm1)
and the program will successfully start.

4) That last line,
Visual Basic:
sTest = Form1.TextBox1.Text
should be this:
Visual Basic:
sTest = [b]frm1[/b].TextBox1.Text
because you need to use the instance of the class, not the class itself.

At that point, it should execute... Once the program closes, the last sText line will execute.
 
Back
Top