473,839 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.

Nov 21 '05 #1
19 1986
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim XX As New Class1
XX.DeTextbox = Textbox1
XX.doit()
End Sub

Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"hamil" <ha***@discussi ons.microsoft.c om> wrote in message
news:D3******** *************** ***********@mic rosoft.com...
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.

Nov 21 '05 #2

"hamil" <ha***@discussi ons.microsoft.c om> wrote in message
news:D3******** *************** ***********@mic rosoft.com...
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.
Nov 21 '05 #3


"Jeff Johnson [MVP:VB]" wrote:

"hamil" <ha***@discussi ons.microsoft.c om> wrote in message
news:D3******** *************** ***********@mic rosoft.com...
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.

Could you explain. Modifiers of what??

Hamil.

Nov 21 '05 #4

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.
"Ken Dopierala Jr." wrote:
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim XX As New Class1
XX.DeTextbox = Textbox1
XX.doit()
End Sub

Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"hamil" <ha***@discussi ons.microsoft.c om> wrote in message
news:D3******** *************** ***********@mic rosoft.com...
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.


Nov 21 '05 #5
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

"hamil" <ha***@discussi ons.microsoft.c om>
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.

Nov 21 '05 #6
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"hamil" <ha***@discussi ons.microsoft.c om> wrote in message
news:BC******** *************** ***********@mic rosoft.com...

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.
"Ken Dopierala Jr." wrote:
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim XX As New Class1
XX.DeTextbox = Textbox1
XX.doit()
End Sub

Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"hamil" <ha***@discussi ons.microsoft.c om> wrote in message
news:D3******** *************** ***********@mic rosoft.com...
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(B yVal sender As System.Object, ByVal e As System.EventArg s) 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.


Nov 21 '05 #7
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

Nov 21 '05 #8

"hamil" <ha***@discussi ons.microsoft.c om> wrote in message
news:C2******** *************** ***********@mic rosoft.com...
You can change the Modifiers property to Friend.

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.
Nov 21 '05 #9
"hamil" <ha***@discussi ons.microsoft.c om> wrote in message news:<BC******* *************** ************@mi crosoft.com>...
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.PublicV ar 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.Private Var
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?
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2988
by: Gidi | last post by:
Hi, For the last week, i'm looking for a way to make a TextBox always write in English (No matter what the OS default language is). i asked here few times but the answers i got didn't help me. i search in google and found a way with changing the CultureInfo but still didn't work on a TextBox. i'm sure there's a way to do that, but i don't know what's the way. I'm desperate, if some one knows the answer, i will be very thankful to know...
28
4382
by: kfrost | last post by:
I know this is probably simple but I have a C# form and the class for the form is called sbaSynch. I have a textbox name txtServerName. I'm creating a class to manipulate XML functions so I added a class to project and it's named XmlApi(). In the XmlAPI() class I have simple code as following XmlAPI() { string str = "Some Text";
3
1337
by: The One | last post by:
Have created a form to pop up with 2 option I then wish to write the text that is in the option button chose back to the original form using the code below but it gives me an exception error so could any please help me. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frm As Form1 Dim text As String
2
2344
by: Steve | last post by:
Hello, If I instantiate a class object from a form I would like to be able to write to a label on the calling form something like "hello from class object" from within a subroutine inside the class object. Is this possible?
2
19879
by: Mamatha | last post by:
Hi I want to add an icon to the textbox's text. I don't know how to display icon in textbox in VB.NET. If any one knows please let me know. Thanks in advance. Mamatha
3
2190
by: DotNetNewbie | last post by:
I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days. I am having trouble getting one of the exercises to work at the end of day 4. Exercises: 1. Create a new multiple-form application that displays two forms when it starts, each with a text box. Add code to each text box’s TextChanged event (this is the
3
3095
by: JoelH | last post by:
This is really weird... I'm using Visual Basic 2005 Express I have a form - Form1 - with a control called txtStatusBox I have a module attached to the solution for storing some other code. This links to an Outlook MAPI folder (variable ContItems), that waits for new items to arrive. When a new message is delivered, the event is fired. However, when it tries to change the text in a textbox on Form1, the box remains unchanged:
0
3591
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this OleDbParameterCollection" whenI try to write a new record. Delete and Modify work fine its just the add record function causes the error.
5
2368
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
I'm sorry, but I've read your code a couple of times and just don't see where the Form1 is initialized. Form1 also sounds like a class name, and this would be how you could do some form operations in vb6, but not in .Net. This would explain why the messagebox works, as it is not instantiated, but called statically as you did in your code. I think you need this where your MsgBox is called: dim ui as New Form1() ui.txt_rec.Text =...
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10648
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.