Results 1 to 25 of 25

Thread: Control Arrays

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Control Arrays

    Hi,

    A common misconception is that Control Arrays have disappeared from VB.NET. They haven't. What has disappeared is the Index Property of a control and the ability to create the array automatically in design view.

    MSDN Help will give several work-arounds, including use of the Tag property but the simplest way is to use an Array of Controls. The following creates ten textboxes in code and stores a reference to each one in an array.

    VB Code:
    1. Dim arrTextBox(0) As TextBox  ' (Prepare array to receive textBoxes.
    2.                                                  '  Give this the necesary scope)
    3.  
    4.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    5.  
    6.         Dim txtTemp As TextBox       '   ( Reserve variable for new textbox
    7.                                                 '   Local scope only  OK)
    8.         Dim iCount As Integer              (Local scope only)
    9.  
    10.         For iCount = 0 To 9
    11.             ReDim Preserve arrTextBox(iCount)  '(Expand array to receive
    12.                                                                  ' next textbox
    13.             txtTemp = New TextBox   ' ( Create instance of new textbox)
    14.             txtTemp.Name = "txtBox" & iCount.ToString  '(Name textboxes with
    15.                                                                         'sequential numbering)
    16.             txtTemp.Width = 40
    17.             txtTemp.Location = New Point(15, iCount * 20)
    18.             Me.Controls.Add(txtTemp)           '(Add new textbox to form
    19.             arrTextBox(iCount) = txtTemp      '(Place reference to new textbox
    20.                                                           'in appropriate element of array)
    21.  
    22. '(The next line adds the handle of the new textbox to the appropriate event)
    23.  
    24.             AddHandler txtTemp.TextChanged, AddressOf TextChangedEvent 
    25.         Next
    26. End Sub
    27.  
    28.  
    29.      Private Sub TextChangedEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
    30.         MessageBox.Show(CType(sender, TextBox).Name & "  Text was changed")
    31.     End Sub

    You can now refer to each TextBox by the array subscript ( index no.)

    This is better than the VB6 Control Array because you can mix any type of object by replacing

    Dim txtTemp As TextBox
    Dim arrTextBox() As TextBox
    txtTemp = New TextBox

    with

    Dim txtTemp As Object
    Dim arrTextBox() As Object
    txtTemp = New Object

    You could then widen the possibilities by using a 2 dimension array, allocating the elements of the first dimension to hold different types or groups of objects, although those elements would only hold numeric values of course.


    EDIT; Handler for Text_Changed event added, see Wossname's comment.
    Last edited by taxes; Mar 11th, 2005 at 06:02 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Control Arrays

    However, you have to add further code in order to make controls in the control array respond to events.
    I don't live here any more.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by wossname
    However, you have to add further code in order to make controls in the control array respond to events.

    Hi, My apologies. I should have selected the full coding when I cut it from a programme.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Re: Control Arrays

    Okay.... so where is the rest of the code?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by epixelman
    Okay.... so where is the rest of the code?

    It's all there.

    You have an array of TextBoxes together with an event handler which responds to any text change in any one of them. If you want to handle other events the example should be easy to follow.

    What is your specific problem?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    Member
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    55

    Re: Control Arrays

    Thanks for the explanation Taxes, but is it possible to add a handler to the control array itself?

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by trezise
    Thanks for the explanation Taxes, but is it possible to add a handler to the control array itself?
    No. But what is your problem? You have one eventhandler to handle the same event (e.g. TextChanged) which handles ALL the textboxes. If you are creating the array in the designer, then you have to add the handle for each textbox, but if you create them in code, the appropriate handle is added each time you create the TextBox
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Control Arrays

    Hmm. I guess I can understand the want for this, if this is a style you're used to. However, never having had a "control Array" I don't like it. I'm not saying you suck or your code sucks or it's dumb or whatever, but for me, who took one look at VB5 and said "Screw VB" until I saw VB.NET, It just doesn't sit in my belly too well. If I had to do this, I would probably use an arraylist. Ever since the first day I used an arraylist, the word "ReDim" makes me ill.

    To me, Arrays are just poor for any kind of dynamic resizing in vb.net. Also, ReDim doesn't work with all dimensions of multi-dimensional arrays. (not cool to me) Honestly, I really don't see the need to have the array of controls.
    Use of the tag property, as you mentioned, is a more efficient method in my opinion. You've already got a collection of controls you can enumerate through (Me.Controls) and using the tag property can easily identify them as part of a group.

    i.e,

    VB Code:
    1. For i As Integer = 1 to 10
    2.     WithEvents T as new TextBox
    3.     T.Location = New Point(0,i*10)
    4.     T.Size = New Size(200,25)
    5.     T.Tag = "GUITextBoxes" 'or whatever.
    6.     Me.Controls.Add(T)
    7. Next i
    8.  
    9. 'and
    10.  
    11. For Each Ctl As Control In Me.Controls
    12.     If TypeOf(Ctl) Is TextBox AndAlso DirectCast(Ctl,TextBox).Tag = "GUITextBoxes" Then
    13.        DirectCast(Ctl,TextBox).Text = "I'm a dynamically Created TextBox"
    14.     End If
    15. Next Ctl

    So, you can still enumerate through them easily, still add handlers (using the same method you did by casting Sender) and If you want to add another one later, it's no big deal - just add the same tag property, and your current enumeration routines still work.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  9. #9
    Member
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    55

    Re: Control Arrays

    Firstly Taxes, I just didn't want 'textTemp' - I would much rather just use a text box array (purely for simplicity). However, I may have to use 'txtTemp' if I can't add a handle to the array. Other than that your explanation works perfectly for me.

    And you're right conipto, it's just the style I'm used to. I see your point, but using control arrays makes more sense to me... even though it may be less practical or whatever.

  10. #10
    Member
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    55

    Re: Control Arrays

    What is the easiest way to obtain the index of a control in the event handler (using this method)?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Control Arrays

    Quote Originally Posted by trezise
    What is the easiest way to obtain the index of a control in the event handler (using this method)?
    VB Code:
    1. Array.IndexOf(Me.arrTextBox, sender)
    Note that you don't even have to cast the sender, given that it IS the same object regardless of the type of the reference through which it is accessed.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by jmcilhinney
    VB Code:
    1. Array.IndexOf(Me.arrTextBox, sender)
    Note that you don't even have to cast the sender, given that it IS the same object regardless of the type of the reference through which it is accessed.

    Hi,

    A minor typo there, should be

    "given that it IS the same object TYPE "
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Hi Conipto,

    I wouldn't dream of imposing my primitive ideas on anyone. I made the original post beause there were so many repetitive questions being asked on the subject.

    When you said

    "To me, Arrays are just poor for any kind of dynamic resizing in vb.net. Also, ReDim doesn't work with all dimensions of multi-dimensional arrays. (not cool to me) Honestly, I really don't see the need to have the array of controls.
    Use of the tag property, as you mentioned, is a more efficient method in my opinion. You've already got a collection of controls you can enumerate through (Me.Controls) and using the tag property can easily identify them as part of a group."

    Unless you use an array you cannot make a direct location reference to the object. All the other methods you list require you to iterate through the collection checking the relative property value (name or tag).

    BTW you can use Redim on all dimensions of multidimensional arrays. You can use Redim Preserve on the final dimension only, but when considering Arrays of Controls you would rarely, if ever, need a multidemsional array

    Anyway, it is no big deal. I use arrays a lot in this connection as it is so simple. Each to his/her own
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    Member
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    55

    Re: Control Arrays

    Thanks jmcilhinney.

    Still getting used to .NET... How do you handle events such as GotFocus and LostFocus for an array such as this (rather than using the Click event)?

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by trezise
    Thanks jmcilhinney.

    Still getting used to .NET... How do you handle events such as GotFocus and LostFocus for an array such as this (rather than using the Click event)?
    Exactly the same way. Have you tried it? The event will be fired when each control itself changes focus.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16
    Member
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    55

    Re: Control Arrays

    Ohhh hahaha it's just that for some reason my VB doesn't predict the syntaxes of GotFocus and LostFocus (so I thought they weren't available)... It recognises them though

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by trezise
    Ohhh hahaha it's just that for some reason my VB doesn't predict the syntaxes of GotFocus and LostFocus (so I thought they weren't available)... It recognises them though
    Hi,

    That can happen.

    There is a post somewhere on how to make the intellisense show all the events. Try a search.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Control Arrays

    Quote Originally Posted by taxes
    Hi,

    A minor typo there, should be

    "given that it IS the same object TYPE "
    Actually, that's no typo. I meant exactly what I posted. We're dealing with a reference type, so it doesn't matter how many different variables you use or what their type, they still refer to the same object, which is "Is" in VB.NET syntax.
    VB Code:
    1. Dim myControl As Control = CType(sender, Control)
    2. Dim myTextBoxBase As TextBoxBase = CType(sender, TextBoxBase)
    3. Dim myTextBox As TextBox = CType(sender, TextBox)
    4.  
    5. MessageBox.Show((sender Is myControl AndAlso sender Is myTextBoxBase AndAlso sender Is myTextBox AndAlso sender Is Me.arrTextBox(Array.IndexOf(Me.arrTextBox, sender))).ToString())
    Try that code and you'll find that it is True that all those references ARE the same object.

    Also, don't use the GotFocus and LostFocus events except under the very specific circumstances detailed in the help topics. Use Enter and Leave instead.

  19. #19
    Member
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    55

    Re: Control Arrays

    Quote Originally Posted by jmcilhinney
    Also, don't use the GotFocus and LostFocus events except under the very specific circumstances detailed in the help topics. Use Enter and Leave instead.
    OMG! I didn't read that last post and started a thread about an error I was getting with GotFocus and LostFocus...

    http://www.vbforums.com/showthread.php?t=382839

    Anyway thanks, Enter and Leave works perfectly

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by trezise
    Firstly Taxes, I just didn't want 'textTemp' - I would much rather just use a text box array (purely for simplicity). However, I may have to use 'txtTemp' if I can't add a handle to the array. Other than that your explanation works perfectly for me.

    .
    Sorry, I missed that post. textTemp is just a temporary placeholder used to create an instance of a TextBox which is then renamed. When the loop is finished there is no textbox remaining called TextTemp.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  21. #21

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,963

    Re: Control Arrays

    Quote Originally Posted by jmcilhinney
    VB Code:
    1. Array.IndexOf(Me.arrTextBox, sender)
    Note that you don't even have to cast the sender, given that it IS the same object regardless of the type of the reference through which it is accessed.
    My apologies Slight misunderstanding here on my part. I thought that if an Object array was used (with mixed objects) you would need to indicate the Object Type but you don't.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  22. #22
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Control Arrays

    Is there a way i could store images in a picturebox array? Could u show me how to do that? (im kinda new to vb.net, and trying to figure out if i want to try vb.net, or stick with vb6)
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Control Arrays

    Quote Originally Posted by Gamemaster1494 View Post
    Is there a way i could store images in a picturebox array?
    That doesn't make sense. By definition, a PictureBox array stores PictureBoxes. If you want to store Images then you would use an Image array.

  24. #24
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: Control Arrays

    ok..... im just trying to think of a way to do bitblt again...... =( I wish the ppl that made VB.net would have kept everything the same, and just added extras, and enhansed stuff, not change everything. =(
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Control Arrays

    Quote Originally Posted by Gamemaster1494 View Post
    ok..... im just trying to think of a way to do bitblt again...... =( I wish the ppl that made VB.net would have kept everything the same, and just added extras, and enhansed stuff, not change everything. =(
    I don't really see what this has to do with the topic of this thread. Please keep posts to CodeBank threads limited to just discussion of that specific topic.

    You can use the BitBlt API in VB.NET and there are plenty of examples around.

    The .NET platform aimed to do more than just add new features to VB. If you had your wish then we'd still have DLL Hell, for one thing.

    If you want more specific information then I suggest that you start a new thread in the VB.NET forum.

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