How do I write to a textbox in a form from within a class

G

Guest

I have a form with one button, Button1, and a Textbox, Textbox1

I have a class, class1 as follows.

Public Class Class1
Public DeForm As Object
Sub doit()
DeForm.Textbox1.text = "It works"
End Sub
End Class

My button code is as follows

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim XX As New Class1
XX.DeForm = Me
XX.doit()
End Sub

Here is what I want to happen..

When I press Button1, Class1 is instanciated as XX.
Then i send a reference of my form to XX in object variable DeForm .
Now when DoIt is executed, I would like the test string "it works" to be
written in the textbox1 on me form, that is named Form1.

When it runs I get this error message.

"Additional information: Public member 'Textbox1' on type 'Form1' not found."

on this line.

DeForm.Textbox1.text = "It works"

Can someone help?

thanks

Hamil.
 
K

Ken Dopierala Jr.

Hi Hamil,

That is because Textbox1 isn't Public so Class1 can't refer to it. Try
this:

Public Class Class1
Public DeTextbox As Object
Sub doit()
DeTextbox.Text = "It works"
End Sub
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim XX As New Class1
XX.DeTextbox = Textbox1
XX.doit()
End Sub

Good luck! Ken.
 
J

Jeff Johnson [MVP:VB]

Here is what I want to happen..

When I press Button1, Class1 is instanciated as XX.
Then i send a reference of my form to XX in object variable DeForm .
Now when DoIt is executed, I would like the test string "it works" to be
written in the textbox1 on me form, that is named Form1.

When it runs I get this error message.

"Additional information: Public member 'Textbox1' on type 'Form1' not
found."

on this line.

DeForm.Textbox1.text = "It works"

Can someone help?

You can change the Modifiers property to Friend.
 
G

Guest

Ken What you suggest works! Could you explain the problem in a bit more
detail. Your example is similar to what I wrote except that I tried to pass
the entire form object and you passed only the TestBox1 object.

Is there a way to pass the entire form so if I have a number of textboxes in
the form I can pass the entire form by setting one object variable?

I'm new to VB.net so excuse me if I am not saying things exactly correct.
 
C

Cor Ligthert

Hamil,

It is jumping in the blind however
\\\
Sub doit()
For Each ctr As Control In DeForm.Controls
If ctr.Text = "TextBox1" Then
ctr.Text = "It works"
End If
Next
End Sub
///
I hope this helps?

Cor
 
K

Ken Dopierala Jr.

Hi,

Here is another way to do it:

Public Class Class1
Public DeForm As Form1 'The class name of your form.
Sub doit()
DeForm.Textbox1.Text = "It works"
End Sub
End Class

Then in your Button1 Click event change your code back to the original:

Dim XX As New Class1
XX.DeForm = Me
XX.doit()

This will let you access all the controls on the form. The problem was that
the controls are declared as Friend, so in order to access them you need to
declare the DeForm as Form1 instead of just a generic object. You do not
want to modify the Friend modifier in the Form itself to read public because
that messes things up. For example I tried that in VB.Net 2002 and it would
remove the controls from my form so I had to recreate them again. Good
luck! Ken.
 
C

Cor Ligthert

Ken,

Problem is that in your solution the class is not real reusable, (what it is
in my solution as well not much because that textbox needs forever the name
"textbox1")

However when it is used to transport information from one form class to the
other, than this is a solution.

Not that you do not know that however for the ones who read this thread
later.

Cor
 
J

Jeff Johnson [MVP: VB]

Could you explain. Modifiers of what??

The control you want to access from the class. In the Properties window of
the IDE (with the form designer window open), select the control and then
find the Modifiers property.
 
D

DenBorg

hamil said:
Ken What you suggest works! Could you explain the problem in a bit more
detail. Your example is similar to what I wrote except that I tried to pass
the entire form object and you passed only the TestBox1 object.

Is there a way to pass the entire form so if I have a number of textboxes in
the form I can pass the entire form by setting one object variable?

I'm new to VB.net so excuse me if I am not saying things exactly correct.

The following should help clear things up a bit. When you design a
class, you can control the level of access for each class member.
Class members are class variables, properties, events, methods (subs
and functions), etc.

You can specify whether a class member is:
1) Public
2) Private
3) Protected
4) Friend
5) Protected Friend

This list is known as "modifiers", because they modify the member's
level of access.

For example:

Class MyClass
Private PrivateVar As Integer
Public PublicVar As Integer
Protected ProtectedVar As Integer
Friend FriendVar As Integer
End Class

Class MyOtherClass
Public Sub DoThis(mc As MyClass)
mc.PublicVar = 1 '<--- OK because it is Public
mc.PrivateVar = 1 '<-- illegal because this is private to
MyClass
mc.ProtectedVar = '<-- illegal because this is private to
MyClass and its derived classes
mc.FriendVar = 1 '<-- OK as long as MyOtherClass is in same
project/assembly as MyClass
End Sub
End Class

"Public" means that the member can be accessed from anywhere ...
inside or outside the class, and inside or outside the
project/assembly. This is the one end of the spectrum. In the example
above, MyOtherClass can access MyClass.PublicVar because it is Public.

"Private" means that the member can only be access from within the
class itself. This is the opposite end of the spectrum from Public. In
the example above, MyOtherClass cannot access MyClass.PrivateVar
because PrivateVar is private to MyClass; only code inside MyClass is
allowed to access PrivateVar.

"Protected" is the same as 'private', except that derived classes can
also access the member. Consider the following class:

Class MySimiliarClass
Inherits MyClass
Public Sub DoSomething()
Me.ProtectedVar = 1 '<--- OK because member is protected
(accessible by class and derived classes only)
Me.PublicVar = 1 '<--- OK because member is public
Me.PrivateVar = 1 '<-- illegal because member is private to
MyClass
Me.FriendVar = 1 '<-- OK as long as MySimiliarClass is in
same project/assembly as MyClass
End Sub
End Class

"Friend" is much like 'public' in that any code within the same
project/assembly can access Friend members. However, code outside the
project/assembly cannot access these members.

"Protected Friend" combines 'Protected' and 'Friend', such that
Protected Friend members can be accessed by any code inside the same
project/assembly and also by any derived classes that are defined in a
different project/assembly.


Now, to your TextBox issue ... In your form that contains your Button,
your text box control is defined as a "Private" member. Your form *is*
a class, and the TextBox is a member of your form's class. If you look
at the code generated by the form designer, you would see something
like the following:

Private TextBox1 As TextBox

Because this textbox member is 'Private', other classes (such as your
Class1 class) cannot access it. Remember, Private members can only be
accessed by code within the class itself. Therefore, your TextBox can
only be accessed by the code in your form.

Now, if you were to use the Properties window and change the TextBox
from 'Private' to 'Friend' or 'Public', then your Class1 could access
the textbox. ('Friend' would be better, because you want to use the
most restrictive modifier that provides the required level of access,
but not more than is needed).

The reason why it worked when passing the TextBox reference to the
routine instead of the form reference, is because in that case you are
not accessing the text box through the form object.

If you change the modifier for all text boxes you need to work with in
this manner from 'Private' to 'Friend', then your Class1 class would
be able to accept a reference to your form and manipulate those text
boxes as needed.

Does that help clarify things a bit?
 
G

Guest

This is real close to what I want, however as Cor points out, the clas is not
reusable. It seems like there should be a way to make my original method
work by making some sort of change, i.e. friend to public like you
discouraged.

In your first reply, you pointed out that I could pass a reference to a
particular Textbox to the class and it works. When I tried to pass the
entire form as an object from which I could pick out the textbox of my choice
it failed.

Any thoughts.
Hamil.
 
K

Ken Dopierala Jr.

Hi,

Change Friend to Public the way Jeff Johnson recommended. I think I was
having problems because I was modifying the code where is says not to modify
it. Using the Modifiers property from the property box is the correct way
to do it. Ken.
 
C

Cor Ligthert

Ken,
Change Friend to Public the way Jeff Johnson recommended. I think I was
having problems because I was modifying the code where is says not to
modify
it. Using the Modifiers property from the property box is the correct way
to do it. Ken.

Why Ken, the textbox1 is unknow to the form class it is in its inherrited
class Form1, however not known in the parentclass Form.

Your answer was the best direct answer in my opinion, however it gives not
much results as long as that not is between two forms.

Cor
 
C

Cor Ligthert

Jef
The control you want to access from the class. In the Properties window of
the IDE (with the form designer window open), select the control and then
find the Modifiers property.
????

Cor
 
C

Cor Ligthert

???
Means that I do not understand this and I am active in this thread,
something I learned from Herfried K. Wagner

Cor
 
K

Ken Dopierala Jr.

Hi Cor,

That's true. Late-binding would be used and you would lose intellisense by
declaring the form variable in the class as object. But now the class would
be universal and you can always check to see what type of form you have and
knowing that use Textbox1 if you know that form contains one. I'd put in
some Try...Catch blocks to make sure I could easily spot bugs where I try to
reference a Textbox1 on a form that didn't have one. But now the class is
reusable without having to know the actual form it works with. Ken.
 
J

Jeff Johnson [MVP:VB]


Okay, perhaps this is a little clearer. I'll go step-by-step, using object
names from the original post:

1. Make sure the designer window for Form1 is open and active.
2. Select Button1, either by clicking on it in the designer window or
selecting it from the dropdown in the Properties window.
3. Find the Modifiers property and change it to Friend. If your Properties
window is in Categorized view (which I personally don't like) then you'll
find it in the Design category.

In case your misunderstanding was simply with the first sentence, it was in
answer to the poster's question of
 
G

Guest

Jeff Johnson said:
Okay, perhaps this is a little clearer. I'll go step-by-step, using object
names from the original post:

1. Make sure the designer window for Form1 is open and active.
2. Select Button1, either by clicking on it in the designer window or
selecting it from the dropdown in the Properties window.
3. Find the Modifiers property and change it to Friend. If your Properties
window is in Categorized view (which I personally don't like) then you'll
find it in the Design category.

In case your misunderstanding was simply with the first sentence, it was in
answer to the poster's question of

First, I want to thank all of you for your help. I have learned a lot in
the discussions. In the above though, I think Jeff means to change the
modifier of the TextBox1 from friend to public (correct me if this is wrong).
This is what I did and it works.

I also found out that in my class I could change the line

Public Deform as object

to

Public DeForm as Form1

where Form1 is the form where my button and textbox resides.
and then the Textbox is visible in the class. So I have lots of ways to do
things. :)
Hamil.
 
C

Cor Ligthert

Hamil,
Public Deform as object

to

Public DeForm as Form1

The last statement which you got from Ken is why it works, not the change
from friend to public, that will help you when the procedure is outside your
project. Inside a project is a Friend the same as Public.

However the first answer from Ken was the best when it has to be a generic
class, but then only with option Strict off.

Just to get no wrong conclussions in this thread.

Cor
 

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