Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Writing text in a textbox

Posted on 2000-05-02
Medium Priority
Last Modified: 2010-05-02
I have a usercontrol with a textbox on it. In the same project also is a
form. Now I want to write values which are created on the form to the
textbox on the usercontrol. Now the problem is, the form don´t know the
usercontrol or the textbox on it. Please write me a solution for this problem.

The form is in the same project. I already know how to add properties to a usercontrol. But this is not
the problem here. For example:
The Usercontrol has the name "ctlVisual" how could a form "talk" to the usercontrol. I don´t think
on the form on which the usercontrol is placed by the programmer...

I think these anwers are all ok,  but my usercontrol is not on the Form.
The form is not a second project on which the control is droped. If the control
is placed on the form i can get access by the properties, ok.
For example:
Instance_of_control.Property = "something"
But the control should comunicate with the form without being placed on it.
It is part of the usercontrol-project. Maybe Events can solve this problem.
What Do you think.
Question by:_alpha
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
LVL 10

Expert Comment

ID: 2768864
The form *must* know the user control. It need not know the text box.

In your user control code:

Public Property Let MyTextBoxesText(strNewValue As String)

'...usual user control property code
TheTextBoxOnTheControl.Text = strNewValue

End Property
Public Property Get MyTextBoxesText() As String

'...usual user control property code
MyTextBoxesText =TheTextBoxOnTheControl.Text

End Property

Put one of your user controls on the form

To set the text box's text, for example

YourUserControl1.MyTextBoxesText = "hello world"

LVL 10

Expert Comment

ID: 2768869
The "MyTextBoxesText" property will also be available in the properties window in design mode.

Use the property bag

Check out the ActiveX Control Interface Wizard. It makes it very easy to start with a new control if you want to propagate some properties of embedded controls to the outside world. Well worth investigating. It also provides a lot of the standard code, e.g. the code needed to use the property bag properly... use it for inspiration and to learn coding UserControls quickly.

Don't rely too much on it after the initial stages of coding a user control.
LVL 43

Expert Comment

ID: 2768874
ON the add-ins menu should be the activex control interface wizard, you need to add a property "Text" which is mapped to the .Text property of the textbox contained within your control. Then when you place it on your form, the UserControl will be something like

UserControl11, you can set the text using:

UserControl11.Text = "Hello This is Text"
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 43

Expert Comment

ID: 2768875
caraf_g, you beat me to it!

Author Comment

ID: 2768944
Edited text of question.

Expert Comment

ID: 2768963
You don't need to do this!
You simply make the textbox the size you want,
and set the textbox's property:
to TRUE.
and set the textbox's property:
to 2 - Verticle.

That's all.

Now to get the "form" to know the text of the textbox,
add a property (set and get):
(Be in the code view, and press the menu:
 Tools/Add Procedure
 In the dialog that opens, choose the "Propery" radio,
 and in the textbox enter: Text.

Now you usercontrol (frame) has a property called: "text".
In the GET, you return the text from the textbox.
I will give you an example using "MYTEXT" as the property, change it to TEXT in your real app.
Public Property Get MYTEXT() As Variant
  MYTEXT = myTextbox ' short for: myTextbox.text
End Property
and in the LET (when you set the prop's value) have the textbox reflect this:
Public Property Let MYTEXT(ByVal vNewValue As Variant)
  myTextbox = MYTEXT ' same as myTextbox.text =...
End Property

Last but not least when the control initializes,
(first time around)
you want it to have the textbox set to the property:
mouse to the top left corner of the code view.
choose the usercontrol (or whatever name you changed it to):
 and on the right top corner choose the "initialize"
event (it's probably the default):
Now you should have written in your VB:
Private Sub UserControl_Initialize()
End Sub

That's it.
Youre next question will be:
How do I change the usercontrol's name,
and how do I change the project's name,
so I get what I want in the registry....

Good luck, Moshe
LVL 10

Expert Comment

ID: 2769222

Interesting comment:

"NO NO NO, you don't need to do this, do *THIS* instead"
After which you basically repeat what I was saying in different words.


Expert Comment

ID: 2769233
Hi Alpha,

if I understand you right, your big problem is that you have two forms in a project. One form has the usercontrol with the textbox, but you want the other form to communicate with the user control.

First of all, everybody was right about you have ot create a property so that the text in the textbox is accessible outside the usercontrol.

To make the other form get or set the property, simply use the name of the form on which the usercontrol is. For example, if the form is named CtrlCont, the control is Ctrl1, and the property is Text, then use CtrlCont.Ctrl1.Text="Whatsoever...". Note this will not work if you load the form with the New keyword; if that's the case then you should use the name of your varibale to which the form is loaded instead of the name of the form (which is really a class name).

Hope this is what you were looking for.

Author Comment

ID: 2771021
Edited text of question.

Accepted Solution

4P earned 400 total points
ID: 2772068
OK, I see. So your form in the UserControl project, and you want it to send data back to the control itself, and because this is the UserControl project, the control is not yet placed on any form.

Here is the solution:

You still need the property as discussed before.

Add a Bas module to your UserControl project, and declare a Public variable in it as Object, for example:
Public CtrlObject as Object.

Then, in the Initialize event of your UserControl, put:
Set CtrlObject=Me

In the Terminate event, put:
Set CtrlObject=Nothing

From that on your form can use the CtrlObject to set any properties of the control.

LVL 10

Expert Comment

ID: 2772450
"But the control should comunicate with the form without being placed on it."

No can do.

A control needs to have a parent.

Author Comment

ID: 2776343
Thanks Pèter,

thats what I have been looking for. I know my english is not perfect, so I sometime have problems to explain my problems.

Thank You for your efforts.


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question