Can't access Textbox1.text from a Module

K

Ken Soenen

The code below illustrates my problem which is: I'm trying to access the
TEXT from TextBox1 which is on Form1. Line "aa = Form1.TextBox1.Text"
produces the error--Reference to a non-shared member requires an object
reference". Being new to VB.NET, this doesn't say much to me, nor does the
Show Task Help for this particular msg. Just one example would make all the
difference in the world. Can somebody point me in the right direction??

thanks,
ken



Module Module1

Sub test1()

Dim aa As String

aa = Form1.TextBox1.Text

End Sub

End Module
 
G

Guest

Ken,

One option is to place the test1 subprocedure in the form that contains the
textbox. You could then refer directly to the textbox.

Another option is to have the test1 subprocedure accept a textbox as an
argument:

Sub test1(ByVal myTextbox As Textbox)

aa = myTextbox.Text

Then, when you call the sub, send it the textbox.

Kerry Moorman
 
C

Chris

Ken said:
The code below illustrates my problem which is: I'm trying to access the
TEXT from TextBox1 which is on Form1. Line "aa = Form1.TextBox1.Text"
produces the error--Reference to a non-shared member requires an object
reference". Being new to VB.NET, this doesn't say much to me, nor does the
Show Task Help for this particular msg. Just one example would make all the
difference in the world. Can somebody point me in the right direction??

thanks,
ken



Module Module1

Sub test1()

Dim aa As String

aa = Form1.TextBox1.Text

End Sub

End Module

It is telling you that you never instatiated the Form1 object.

Module Module1

Sub test1()

Dim aa As String
dim Frm as new Form1
aa = Frm.TextBox1.Text

End Sub

End Module

This would work but I doubt it is exactly what you are looking to do.
If you give more info about what you are trying to accomplish then we
could help you more.

Chris
 
K

Ken Soenen

Kerry,

Thanks for the help. After 40 years of programming in the older languages(C,
Fortran, Basic, Ada, Jovial..), this new stuff is all smoke, mirrors, and
voodoo. I've used Visual Basic quite a bit in Excel. It is pretty straight
forword. But not in VB.NET!!

Anyway, your 1st option works. That is the way I originally had it set up
before I decided to put it in a Module(since it seemed that it was just
cluttering up the Form). The second option, I never would have come up with.
It works also. I'm still wrestling with Chris' response about instantiation.
I have to get back to him. Again thanks for the help.

ken
 
K

Ken Soenen

Chris,
The above was just a drop back position to try to understand something basic
before I tackled my real problem. Which was:

From a button on the Form, I was calling Test1(endtime). "endtime" is a
string "globally" defined in the Form1. I needed to modify this "endtime" in
test1() and before returning I wanted to set Timer1.Enabled and
Timer1.Interval (as I just wrote this I realized I could have avoided this
whole situation by setting these properties in the Form1 after the return
from Test1. What a BOZO! But then again I wouldn't have learned what I
did.). To get back to the situation. Referencing these 2 properties in Test1
caused the error msg. So, actually I did what you suggested (before I cried
WOLF) as in creating a new instance of Form1 in Test1. So, this made the
compiler happy. NOW the problem was, back in the FORM1, the code was still
using the INITIAL value of the "Global" variable "endtime" instead of the
modified value that I passed back to the Form. It appears that I was
changing "endtime" in the NEW Instance of Form1 and NOT in Form1. To Make a
long story short, in order to make the thing work(that is in the BOZO mode)
was to instantiate the new form in the FORM1: Public Shared fForm1 As New
Form1. Thanks for your input. You gave me enough so I could look in the
right place and I learned tons (I think).

thanks,
ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top