Usercontrol and loosing data

Posted on 2002-04-12
Medium Priority
Last Modified: 2010-05-02
I've made my own usercontrol but when i change the name
the control is "grayed out" and a number of properties is reset (for instance "Caption").

Any idea?
Question by:dgb
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
  • 5
  • 4
  • 2
  • +2

Expert Comment

ID: 6936189
can you show us code?
LVL 70

Expert Comment

by:Éric Moreau
ID: 6936623
is your control project open in the same instance of VB as your test project? Be sure that all window (code and designer) related to your control are closed. Even with this precaution, there is a small bug that sometimes occurs and the controls remains grayed in the tool box. When this occurs, simply open the designer of your control and reclose it.

Author Comment

ID: 6936664
It is a lot of code and some other objects that make up the usercontrol

When we have a project in wich we want to use the usercontrol we simply add the usercontrol.

We don't have the controlwindow open and it is not that the control is grayed out in the toolbox but on the form
where we are using the control.
After the option Update Usercontrol (right mouseclick on form)
the grayed out part is solved but we've also lost some property values.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 70

Expert Comment

by:Éric Moreau
ID: 6936676
Have you used the wizard to build your control? There are some events that you need to handle into your control (like ReadProperties and WriteProperties).
LVL 15

Expert Comment

ID: 6936751
Yes, it's: Right-click on the form and select "Update User Controls"

That will *recompile* the control, and restart it in run-mode.  Yes, you are in design mode, but control is 'running'.

Don't expect to see any properties you set previously:

Like you have a form running with an empty textbox, and you type "Hello" in textbox, now if you recompile and restart the project, you won't see that "Hello" (unless you save it to registry)

The same is with property pages - you are in design mode, but pages really are 'running'
LVL 15

Expert Comment

ID: 6936782
or, from one PAQ:

Open Form1, right click on it, and select menu "Update UserControls"
This will update temporary CLSID for your control/class.

When you change code in your control, do not expect it will always use
existing data you entered into property page for your control on Form1.
Sometimes "Update UserControls" will do the job (compiling to pi code for VB
IDE, temporary registering, etc.), but if you are adding some properties or
changing arguments for your class methods, the best bet is to:
- first remove control from form1
- add your code in OCX project
- add control to form1 and set properties

You can compare your control with regular vb project.

Regular project (let’s say it’s a form with one textbox)

Textbox has some initial text property (.Text=” ”)
You press Run (this will compile your project to pi code), and in Run mode you CHANGE Text property
Now you stop Running your project, change some code, and press Run. Do you expect you’ll see data you
entered in previous Run? No, your data will be reset (.Text=” ”) and you’ll have to enter it again.

OCX project

Note that real job VB is doing when you work in design mode with 2 projects, is *very* complex (e.g.
your property page is *running* and is in design mode at the same time). When you change code in your
control, VB will have to recompile your control – it will do this secretly when you open form with your
control. After recompiling, it will restart your control – it will run InitProperties, etc.
Do not expect your data you entered before, will be always preserved.

Author Comment

ID: 6939945
emoreau :
Yes i have used the wizard.

ameba :
The thing is that there are no property pages open when i first place the usercontrol on the form and nothing happends but when i change the name the control is grayed out.

In some cases it works fine i can place controls and change names without any property values lost but in other cases i can not.
LVL 15

Expert Comment

ID: 6940091
>The thing is that there are no property pages open

I mentioned property pages only to explain run/design duality.

>when i change the name the control is grayed out

If control is grayed, it means it must be 'recompiled'.  You probably changed the name of something inside user control.  If you change the name of usercontrol placed on the form, it shouldn't require 'recompile'.

>In some cases it works fine

If you don't change something that will change signature of some control method, it may work it out without complete recompile.

Many things are happening 'behind the scene' during design time in VB, and that is fantastic.

VB.NET still does not have that level of magic.
There is no "Edit and Continue" feature in it.
To change anything in the code, you must stop running and recompile...

Author Comment

ID: 6944356
Correct me if i am wrong but changing the name of a usercontrol on a form does not change anything wich would result in recompiling, so why thinks it needs to recompile.

I also checked the init of the usercontrol and only variables are changed not the objects within the usercontrol.
LVL 15

Accepted Solution

ameba earned 300 total points
ID: 6945042
I agree, it shouldn't recompile.  Maybe it doesn't work the best possible way, or maybe the way they implemented it doesn't allow that.

I just tested it and if I only change name, it doesn't 'gray out'.
If I just open usercontrol designer, it grays out - without making any change.  Maybe they can do it better next time ...
LVL 16

Expert Comment

ID: 7754991

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- points to ameba
Please leave any comments here within the
next seven days.

Author Comment

ID: 7785650
Allthough i didn't get an anser to my question, other then
microsoft should do it better next time i will give the
points to ameba for the effort he made.

Thanks for the effort, let us hope microsoft will do it
better next time......
LVL 15

Expert Comment

ID: 7786021
B?  That reminds me why I answer vb questions at different site now.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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
Course of the Month10 days, 3 hours left to enroll

762 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