Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

Writing text in a textbox

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.
  • 4
  • 3
  • 2
  • +2
1 Solution
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"

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.
TimCotteeHead of Software ServicesCommented:
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"
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

TimCotteeHead of Software ServicesCommented:
caraf_g, you beat me to it!
_alphaAuthor Commented:
Edited text of question.
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

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.

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.
_alphaAuthor Commented:
Edited text of question.
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.

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

No can do.

A control needs to have a parent.
_alphaAuthor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now