Set textbox1.enabled = false inside a module?

D

David Eadie

G'Day all,

Ok I have form1 and lets say it has a textbox on it called
textbox1.text.

Now in that form1_load sub I have a command that says:

my_custom_set_txt_box_properties.disabletextbox.

Now I have a module called my_custom_set_txt_box_properties and in
that module I have a sub that follows:

sub disabletextbox
textbox1.enabled = false
end sub

Why cant I call that module in the form1_load sub and get it to
disable the textbox?
I am somewhat new to module's at this stage and would greatly
appreciate any help. (And keep you posted on my results ;-) )
Thanks All

Dave.
 
D

Daniel

try this in the module:

sub disabletextbox
dim myform as new form1
myform.textbox1.enabled = false
end sub
 
C

Chris Dunaway

That will just create a new Form1 and not change the textbox on the
existing Form1.

A form is an object just as any other. In order to change the
properties of an object, you must have a reference to that object.

Inside your module sub, you say:

textbox1.enabled = false

Which textbox are you trying to disable? What if you had two forms
each with a textbox called textbox1? How would that sub know which
textbox to disable?

It would be better to write the sub to take a textbox as an argument:

Module MyModule
Public Sub DisableTextBox(tb As TextBox)
tb.Enabled = False
End Sub
End Module

Then in your form load:

MyModule.DisableTextBox(Me.TextBox1)

If you need to manipulate other controls and properties of the form,
you might wish to make a global variable in your module to hold a
reference to your form and then in the form load, assign that variable:

Module MyModule
Public MyFormRef As Form1

End Module

Then in Form Load:

MyModule.MyFormRef = Me


That way any sub in the module has access to the form and it's
properties and controls.

Chris
 
H

hack123

Holy Wow!! Great replies from both. (I was looking at both of the
problems identified Daniel and Chris.)

Many thanks!!

Dave. :)
 
D

Daniel

Doh! Hadn't had my Ready Brek by then. I was looking at it backwards.
Sorry. :-(
 

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