Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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

 

But How would I do It in VB.NET?

  • *Experts*
Posted

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

 

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

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

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.

'in MyClass.vb or whatever
Public Shared Sub Main()

 

2) You need to instantiate the instance of your form:

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

Application.Run(frm1)

and the program will successfully start.

 

4) That last line,

sTest = Form1.TextBox1.Text

should be this:

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...