Results 1 to 11 of 11

Thread: [RESOLVED] How to change the index of control array element in VB6?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    29

    Resolved [RESOLVED] How to change the index of control array element in VB6?

    Is it possible to change the index of the control array element in VB6?

    For example
    1. I created control array with 5 elements
    2. and after that I unloaded first 4 elements.

    Can I somehow change index of the element #5 to 3?

    Since I unloaded element #3 the index "3" is available. But I need somehow to change index of the element #5 to 3. To be able to refer to former element TextBox (5) as TextBox (3).

    Thank you for suggestions!

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: How to change the index of control array element in VB6?

    Why not just unload #5 instead of unloading #3?

    Aside for that you could load #3 again then remove #5 but that would be a bit odd. Why are you unloading them and why do you feel the need to change 5 to 3?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    29

    Re: How to change the index of control array element in VB6?

    Because control #5 contains some data and #3 does not. I need to continue to use the control #5 but with index 3. Index "3" is available, but I can not change the index of the element #5.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: How to change the index of control array element in VB6?

    But why do you need to access the contents of #5 as index #3?

    If it is critical to your logic, then you should probably have a separate array of indexes that you use to indirectly index into your control array.
    I do something like that in a number of cases, especially if I'm moving controls or grouping them together in different hierarchies, and adding and removing.
    In my case, I don't actually add or remove controls (unless I've run out). When a control is "removed" from the hierarchy, I simply add it to a list (a pool) of available controls and hide it and I remove it from my active list. The Active list is usually a linked list in my case, but you could also just shuffle the index numbers down in your array to "close the gap" of any controls removed from your hierarchical list.

    So, for your example you loaded five controls, so they would be indexes 0 to 4 (or if you use 0 as just a template, then possibly 1 to 5 for you active controls).
    In any case if you really remove 3, and want to use the index 3 to access control #5, then put the number 5 (or 4 if zero based) in place of where index 3 was in your indirect array.

    Much easier to control your own indirect indexes to the control array, than to manipulate the control array indexes directly.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: How to change the index of control array element in VB6?

    I am not aware of a way to change the index of an array element at runtime. There really should be no need to do so anyway.

    One option would be to copy the data from #5 to #3, another may be to clone #5 as #3 but my advice would be to think about your design a bit more and remove the need to try and reindex your array

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    29

    Re: How to change the index of control array element in VB6?

    Thank you for suggestion. I also thought about this option, but wanted to check first if update of the control array indexes is possible, before creation of additional array.

    Does your statement
    Quote Originally Posted by passel View Post
    Much easier to control your own indirect indexes to the control array, than to manipulate the control array indexes directly.
    mean that there is a difficult way of manipulation of the "control array indexes directly"? Or actually it is not an option?

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to change the index of control array element in VB6?

    I think your thought process is flawed ... you have an array:
    1 2 3 4 5

    and you have then full of something... then you remove the first three items
    [1] [2] [3] 4 5

    now you want to change the index of #5 so that it is 3 instead:
    [1] [2] [3] 4 3

    which is the wrong process... what you want to do is move the element at index 5 to index 3 - you're NOT manipulating the index...

    since it's all reference, it should be possible to do this:
    set tb(3) = tb(5)


    now what you have is
    [1] [2] 3 4 5
    where indexes 3 and 5 both point to the same thing... again, we have NOT manipulated the indexes...
    now, if you want element 5 cleared, set it to nothing - DO NOT unload it... unloading it would cause the control itself to unload, losing it at index 3 as well.
    set tb(5) = nothing


    what you should end up with is:
    [1] [2] 3 4 [5]

    I don't have VB6 installed, so I can't check it... but in theory, that should work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: How to change the index of control array element in VB6?

    Quote Originally Posted by ma14 View Post
    ...
    Does your statement

    mean that there is a difficult way of manipulation of the "control array indexes directly"? Or actually it is not an option?
    Not in the way that you want.
    I meant if for some reason you didn't have index 3 loaded, and you wanted textbox at index 5 to be index 3, then you could load the index 3 control, use the Move method to move it and size it to be where index 5 is, copy the text from 5 to 3, then unload 5, and make 3 visible.
    So, you're making control index 3 a copy of 5 then removing 5, so your control indexes are what you want, but at a complicated cost, and perhaps with side effects that you don't want (e.g. cursor position moved, text scrolled, active selection canceled, etc).

    I would never manipulate the control array directly myself, just "references" to the controls in the array, {not real references, as techgnome suggests may be possible, but pseudo references by way of indirect array of indexes}.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    29

    Re: How to change the index of control array element in VB6?

    Dear passel, techgnome and DataMiser,
    Thank you very much for your attention to this problem!

    Since you can not suggest the way of re-assigning of index of element to new one, it means that VB simple does not support it. What is OK, since it supports many other great things.

    I will keep your suggestions in mind and will implement another way of dealing with my problem.

    THANKS!!
    Last edited by ma14; Mar 1st, 2016 at 01:19 PM.

  10. #10
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: How to change the index of control array element in VB6?

    Quote Originally Posted by ma14 View Post
    Dear passel, techgnome and DataMiser,
    Thank you very much for your attention to this problem!

    Since you can not suggest the way of re-assigning of index of element to new one, it means that VB simple does not support it. What is OK, since it supports many other great things.

    I will keep your suggestions in mind and will implement another way of dealing with my problem.
    Another way of dealing with that, would be to use Controls.Add / Controls.Remove
    (with specially designed StringKeys for your Control-Names)...

    So, instead of using (e.g. in case of TextBoxes):
    Code:
      Load TextBoxArr(SomeIndex)
    you could do something like that (using two small helper-functions):
    Code:
    Option Explicit 'Into a Form
    
    Private MyTxtArr As New Collection, i As Long
    
    Private Sub Form_Load()
      Caption = "Click the Form"
      ScaleMode = vbPixels
      For i = 1 To 10
        LoadTextBox 10, 10 + (MyTxtArr.Count + 1) * 25, 200, 22
      Next
    End Sub
    
    Private Sub Form_Click()
      RemoveTextBox 3
      
      LoadTextBox 10, 10 + (MyTxtArr.Count + 1) * 25, 200, 22
    End Sub
     
    Private Function LoadTextBox(x, y, dx, dy) As VB.TextBox
    Static Idx As Currency
           Idx = Idx + 1
       Set LoadTextBox = Controls.Add("VB.TextBox", "Txt_" & Idx)
           LoadTextBox.Visible = True
           LoadTextBox.Text = Idx
           LoadTextBox.Move x, y, dx, dy
      MyTxtArr.Add LoadTextBox
    End Function
    
    Private Sub RemoveTextBox(IndexOneBased)
       Controls.Remove MyTxtArr(IndexOneBased).Name
       MyTxtArr.Remove IndexOneBased
       For i = 1 To MyTxtArr.Count
         MyTxtArr(i).Move 10, 10 + i * 25, 200, 22
       Next
    End Sub
    HTH

    Olaf

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    29

    Re: How to change the index of control array element in VB6?

    Thank you, Olaf!

Tags for this Thread

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