Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping algorhithm putting controls in funky places

Status
Not open for further replies.

JESTAR

Programmer
Feb 13, 2002
213
GB
Strange one this. (continuation from other of my threads, search appears to be dead).

Code:
Public NextVal As Integer
Public DeletedTags(20) As String

Dim i As Integer    'loop counter for storing to the array
    Dim j As Integer    'loop counter for deleting every tag
    Dim k As Integer  'loop counter for adding tags back to form
    Dim AmountDeleted As Integer

    

    If listTags.SelCount > 0 Then
    
    ' store all unselected items in an array
        For i = listTags.ListCount - 1 To 0 Step -1
            If listTags.Selected(i) = False Then
                DeletedTags(AmountDeleted) = listTags.List(i)
                AmountDeleted = AmountDeleted + 1
            End If
        Next i
        
    
    ' wipe out every tag the user has created
        For j = Form1.lTag().Count - 1 To 10 Step -1
            Unload Form1.lTag(Form1.lTag().Count - 1)
        Next j
        
    
    ' add all the tags stored in the array back on to the form
[blue]
        For k = 0 To AmountDeleted Step 1
        NextVal = Form1.lTag().Count
        
           With Form1.lTag(k)
                .Container = Form1.frameTags
                .Top = Form1.lTag(NextVal - 1).Top + 360
                .Left = 120
               .Visible = True
               .Caption = DeletedTags(k)
               .Width = 1815
           End With

       Next k
[/blue]
    End If

What I'm trying to do:

on the main form (Form1) are buttons which the user can add more buttons to. This is from the Remove Buttons form. My psuedo code is:

- store unselected tags in an array (multiselect listbox)
- wipe out all tags the user has created (index>10)
- recreate the tags not selected from the array

It's the readding the buttons bit that's going wrong. It should loop through the array, adding a new button eachtime, but the buttons are appearing all over the place - 1 will appear after the last button where it should, then the rest appear over top to (.top = 0 etc.).

Thanks for any help..
 
It looks like your variable 'NextVal' isn't changing in your loop. Maybe you need to change this value in your loop or maybe change this
.Top = Form1.lTag(NextVal - 1).Top + 360
to this
.Top = Form1.lTag(NextVal - k).Top + 360

Otherwise NextVal - 1 will always evaluate to the same number.

Hope this helps.
 
Hmm it's still putting them all over the place. It usually puts the first one in the right place, but then on the following loops it just goes back to 0, placing them over the top of the other buttons.

I think I know what it is...
I'm creating the new control with an index of 0 because I'm using k...
 
Ok I've got to this:
Code:
For k = 0 To AmountDeleted Step 1
        
        NextVal = Form1.lTag().Count - 1
            With Form1.lTag(NextVal + 1)
                
                [red].Container = Form1.frameTags[/red]
                .Top = Form1.lTag(NextVal - 1).Top + 360
                .Left = 120
                .Visible = True
                .Caption = DeletedTags(k)
                .Width = 1815
           End With

       Next k

Getting a "Control Array Element 10 doesn't exist". Any ideas on that? Is it because the button doesn't exist until it's passed through the loop completely once. If so, how do I get around this and still define these attributes?
 
Hurrah I have it working, I wasn't loading the new index before dealing with it. I'll FAQ this soon. Just gotta sort a problem of it adding an extra blank button for some reason.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top