Solved

Why can I not use the Unload statement in a ComboBox's Click event?

Posted on 2004-04-02
23
1,567 Views
Last Modified: 2013-12-25
Synopsis:

I have a "preview screen" that displays a list of "options" when the user chooses an "item" from the "item list".

Each "item" will have a different number of "options" to display in the "preview screen".

Each "option" is displayed as a Label Control array control element in the "preview screen".


Problem:

The "preview screen" displays the appropriate "options" the first time the user chooses an "item" from the "item list".

But, when the user chooses another "item" from the "item list", the Label Control array control elements dynamically created from the previous "item" chosen must be Unloaded so to allow the "preview screen" display a fresh list of "options".

When the application attempts to Unload a Label Control array control element (i.e., Unload lblControl(1) ), the application returns a Run-time error '365' which states "Unable to unload within this context".


Question:

Apparently the Unload statement cannot be executed from code that is called from a Combo Box's Click event.

Why?!? I am just Unloading a control and not the form.

I know I will have to use the tag property now, but this just sucks.

Why does the Run-Time Error 365 exist if I am just Unloading a control?!?!
0
Comment
Question by:crsublette
  • 11
  • 5
  • 3
  • +1
23 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 10743925
Hi crsublette:
Would you be able to post some of your code?
Can you also clarify if this is VB6 or VB.NET?

Dabas
0
 
LVL 39

Expert Comment

by:appari
ID: 10745433
you cannot use unload control statement from few events. the list is here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vb98/html/vbmsgldCantUnloadHere.asp


0
 

Author Comment

by:crsublette
ID: 10745694
>Mr. Dabas: Would you be able to post some of your code?
Huh? Ok.

Load lblControl(1) 'Creates a new Label control array control element.

Unload lblControl(1) 'Removes the Label control array control element that was created above.

I think what you meant to ask was, "Can you explain your synopsis?"

For discusion sake, the user has three "items" in the "item list". User clicks "item1", and a Label control array control element is created in the "preview screen" with the caption of "option1". Then, the user clicks "item2", the application Unloads the previously created control element(s) (this throws the RE 365 because the unload is used in the context of a Combo Box Click event), and creates a new control element in the "preview screen" with the caption of "option2" (this is not executed due to the mentioned run time error). Each "item" can contain multiple "options". This is why I need to remove all previously loaded control elements before I create new elements because the number of "options" for each "item" in the "item list" is not static.

>Mr. Dabas: Can you also clarify if this is VB6 or VB.NET?
VB6

>Mr. Appari: you cannot use unload control statement from few events. the list is here
What are you saying exactly? That I can't READ?!? I figured that one out when I clicked "Help" when the run-time error initially occured.

>Mr. Crsublette: I have fixed the issue using the Tag property, but that doesn't answer my question. I am just unloading a control and not a form. Why does the Run-Time Error 365 exist if I am just Unloading a control? Cause my five or so lines of code solution with tag property would be instantly cut to about one line of code if I used thed Unload statement.
0
 
LVL 39

Accepted Solution

by:
appari earned 250 total points
ID: 10745761

the behaviour was by design. so we cant do anything but to think of alternative way.

the restriction that unload acontrol is not a valid statement in the events specified in the link. so you have to continue withwhatever you are doing using tag property.
0
 

Author Comment

by:crsublette
ID: 10747917
If it is due to poor design, not a problem, but if your answer is "just becuase it says so", I am not willing to accept it for now.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 10747951
The workaround to this is to add a timer to your form that you enable and set it to a really short period (like 50) in the click event of the combo (and you disable it in the Timer event).
0
 
LVL 39

Expert Comment

by:appari
ID: 10749631
i am not saying your application design is poor. its Microsoft's restriction and i was talking about Microsoft's design.

i am not forcing to accept my comment as answer. as you were asking the reason "why  i can't use unload control within click event" and i provided the link which is from microsoft and it says you can't do that at certain places.
0
 

Author Comment

by:crsublette
ID: 10750367
>Mr. Appari: If your answer is "just because it says so", I am not willing to accept that for an answer *for now*.

My apologies for the lack of clarity. I was also talking about Microsoft's design.

"If the RE is due to Microsoft's poor design, not a problem."

Kind of funny how you saw that statement referring to *my coding*. Who knows? Maybe it was your subconscious answering? Just kidding.


QUESTION: Why does the Run-Time Error 365 exist if I am just Unloading a control?!?! Explain.

ANSWER: None.

I appreciate everyone's postings, but my question is still left unanswered.

Maybe this question requires the knowledge from a person who likes getting dirty on the low-level with VB6.

<Increased points to 300>
0
 

Author Comment

by:crsublette
ID: 10750382
Sorry, if I come off "on the edge", but I am just trying to have fun here.

There is a reason for that restriction.

Why does the Run-Time Error 365 exist if I am just Unloading a control?!?! Explain the restriction.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 10752244
Have you tried my Timer workaround?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:crsublette
ID: 10752786
Mr. Emoreau: Your workaround is appreciated and is valid, but the workaround is a non-answer to my question.

If you wonder how I will allocate the points, since I did not think of your workaround, I will split the points between you (with 25% of the points), Mr. Emoreau,  and the person who can give a more exact answer in contrast to Mr. Appari (with 75% of the points). If no person can give a more exact answer, Mr. Appari will receive the 75%.

I do appreciate everyone's postings.

<Points Increased to 400>
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
ID: 10752803
This is a limitation of VB. The link provided by Appari confirm it. If MS acknowledge it, you won't find anything else!
0
 

Author Comment

by:crsublette
ID: 10758052
Thus, the restriction was implemented for "kicks"?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 10758060
For what?
0
 

Author Comment

by:crsublette
ID: 10760715
You know "kicks" or just did it for no reason. Let me clarify.

Thus, the restriction was implemented for no reason?

Mr. Appari just restated the restriction (RE365) and not explained it.

>>Mr. Emoreau: If MS acknowledge it...

Are you saying that MS developers can code whatever they want into any MS product they choose for no reason?
Don't answer this. Lets try to stay on topic here.

Question: Why does the Run-Time Error 365 exist if I am just Unloading a control?!?! Explain the restriction.

Mr. Appari's answer is a restatement of the restriction.

Maybe this question requires the knowledge from a person who likes getting dirty on the low-level with VB6. I will distribute the points on April 16 if then the question is unanswered.

<Increased points to 500>
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 10762643
>>Question: Why does the Run-Time Error 365 exist if I am just Unloading a control?!?! Explain the restriction.

What if you try to remove the control you are currently handling the event? That's probably why some events cannot handled the Unload statement.

We are offering you valid workaround because the language does not allow it. MS will definitively not fix that. I don't even beleive they will produce another SP for VB6.
0
 

Author Comment

by:crsublette
ID: 10777254
>Mr Appari's Link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vb98/html/vbmsgldCantUnloadHere.asp

>Mr Emoreau: "What if you try to remove the control you are currently handling [in] the event? That's probably why some events cannot [be] handled [with] the Unload statement."

This will fix the problem. In the context I provided, the cause of RE365 is a "There is an Unload statement in the Change, Click, or DropDown events of a ComboBox." But, I am not looking for a solution. I am looking for a reason to the existence of RE365 with respect to my question. Your second sentence, Mr. Emoreau, restated the RE365 sentence I quoted.

>>Mr. Emoreau: "We are offering you [a] valid workaround "
I never asked for a workaround, but I allocate points because they are useful. But, a workaround is not the appropriate answer in the context of my question.

>>Mr. Emoreau: "because the language does not allow it."
Apparently, otherwise, RE365 would not exist.

>MS will definitively not fix that. I don't even beleive they will produce another SP for VB6.
I am not asking for a fix or SP. I am asking for a reason to the existence of RE365.

>MSDN RE365 text: "In some situations you are not allowed to unload a form or a control on a form."
Why in just "some situations"? Is it due to a memory constraint? Is it due to a OS constraint? Is it due to the call stack space? Or did Microsoft create RE365 for "kicks"?

I am currently pursuing research for preparation to be a MCSD in VB .NET and SQL Server 2000. I am just curious if questions with the premise like the one I am asking are answered. The premise that either we have to accept MS documents regarding a language as truth or we can pursue the question to the low-level to obtain a more elaborate and accurate answer than what MS documents provide.

Let me place more clarification on the question.

>>Question: "Why does the Run-Time Error 365 exist if I am just Unloading a control?!?! Explain the reason to the existence of RE365."
0
 

Author Comment

by:crsublette
ID: 10777356
Awesome. Mr. Emoreau, you are who I want to become. I have viewed your profile and I believe your next posting might answer my question. Otherwise, I believe my question might be unclear.
0
 

Author Comment

by:crsublette
ID: 10847692
My apologies. I will distribute the points by or before Sunday, April 18.
0
 

Author Comment

by:crsublette
ID: 10883195
or sometime =)

I want to review each posting before I distribute the points. When I have the free time, I will distribute then.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now