CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    13

    Unhappy unload controls at runtime

    i used a label named lbl in my main form with the index value of 0. i loaded a number of labels with the same name but different indices(control array concept) during runtime. now i am using a timer that will unload the run-time loaded labels using a for loop to iterate through the number of controls in the control array lbl. but the major problem that has come out is that the vb compiler is unable to unload them as once one of the controls from the control array is unloaded, the index values are not decrementing by themselves from within the code written under the timer subroutine. when i tried to use a piece of code to solve it, the vbcompiler said that it it was unable to assign a value to the index property of the label as it was a read-only property. how do i decrement the index values by using code from within the timer subroutine. somebody plzzz help.....

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: unload controls at runtime

    Perhaps this example will help clarify some things for you. Let us know how it goes.
    Attached Files Attached Files
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Jul 2005
    Posts
    13

    Re: unload controls at runtime

    thanx for your help, Wizbang,but this is not what i asked for. What you did was to build a form containing 1 label (label1(0)) and 2 command buttons at design-time, then you loaded a number of labels by clicking the 1st command button using a for loop at run time. On Clicking the second command button, all the labels(except the one which was introduced at design time) were unloaded at a time using a for loop.
    what i asked for was-

    i load the labels the way you did.the problem is with unloading. suppose i have got a text box that asks for a string. now on clicking the 2nd command button, if the string value entered matches any of the labels(all the labels that were loaded at run time)caption (this check is done using a for-next loop), then that particular label gets unloaded.What remains in the control array are the remaining labels whose captions didn't match with the string entered.
    the problem arises when
    -in the first run suppose the label with index 2 has a caption that matches the string value entered. then label1(2) gets unloaded from the control array.
    -when again the command button2 is clicked and i enter a string, the for-next loop for matching the string with the caption starts again. this time the loop is unable to proceed as the control array element with index 2 is already unloaded and the index value has not been decremented.
    when i tried to write down a simple piece of code that can decrement the index values of the elements in the control array(starting from the elememt that is next to the unloaded element), vbcompiler generates a compilation error saying index value being a read-only property cannot be changed.

    got it...
    plzz help...

    deb

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: unload controls at runtime

    The problem here is you are looping through the Array to unload the Labels. This has a drawback as you have already mentioned in your post. The other way of doing it would be to loop through the Controls Collection of your Form. This is not related to the Index so you could Drop any label(except the one that is created at design time).. Here is a simple example

    Open a New Project. Add two command buttons, a Label with index '0' and a text box.. Copy paste the following code
    Code:
    Private Sub Command1_Click()
    	'Load the Labels
    	Dim iCounter As Long
    	For iCounter = Label1.Count To 5
    	  Load Label1(iCounter)
    	  Label1(iCounter).Left = Label1(iCounter - 1).Left + Label1(iCounter - 1).Width
    	  Label1(iCounter).Visible = 1
    	  Label1(iCounter).Caption = "Label" + Trim(Str(iCounter + 1))
    	Next
    End Sub
    Private Sub Command2_Click()
    	Dim ctTemp As Control
    	'Loop through the controls collection
    	For Each ctTemp In Me.Controls
    		'Check the typeof control
    		If TypeOf ctTemp Is Label Then
    			'if it is label then check the caption against the text box
    			If ctTemp.Caption = Text1.Text Then
    				'Remember you cannot unload a label that was created during design time
    				'unload the label control using unload statement
    				Unload ctTemp
    			End If
    		End If
    	Next
    End Sub
    Now run the project and click on Command1. This will create 5 other labels on the Form. Enter Label2 in the text box and Click Command2. This will unload the label with caption "Label2". Now enter Label4 in the Textbox and click the Command2 button. Again the Label with caption "Label4" will be unloaded.

    hope this helps.

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: unload controls at runtime

    Ah, ok. Now it is much clearer what you wanted to do, and the problems you encountered. The method suggested by vb_the_best looks quite suitable to your purpose.

    As you found out, you cannot change the index, nor do the array indexes reorder themselves when an element is unloaded.

    The only other method which comes to mind at the moment is to loop through the array from one index above the one you want to delete up to the last index, and copying the caption from each one down by one index. Then delete the last index. In this way, your array UBound will always match the number of labels you actually have.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Jul 2005
    Posts
    13

    Re: unload controls at runtime

    thanx both wizbang n vb_the_best. i tried out both the methods n they both came in handy. thnx again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured