We help IT Professionals succeed at work.

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

crsublette
crsublette asked
on
1,642 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?!?!
Comment
Watch Question

CERTIFIED EXPERT

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

Dabas

Commented:
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


Author

Commented:
>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.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
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).

Commented:
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.

Author

Commented:
>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>

Author

Commented:
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.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
Have you tried my Timer workaround?

Author

Commented:
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>
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thus, the restriction was implemented for "kicks"?
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
For what?

Author

Commented:
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>
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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.

Author

Commented:
>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."

Author

Commented:
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.

Author

Commented:
My apologies. I will distribute the points by or before Sunday, April 18.

Author

Commented:
or sometime =)

I want to review each posting before I distribute the points. When I have the free time, I will distribute then.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.