Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Code Newbie > Code Newbie News

Reply
 
LinkBack Thread Tools Display Modes
Old 03-07-2006, 07:53 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,529
sde is on a distinguished road
New Article: Mimicking VB6 Control Arrays in VB.NET

Geoff Alexander, aka PUN1SH3R, has contributed a VB.NET article demonstrating how to mimick a control array since MS has decided to take that out of VB.NET.

You can check out the article here.
sde is offline   Reply With Quote
Old 05-08-2006, 11:24 AM   #2 (permalink)
tvqpriqphh
Registered User
 
Join Date: May 2006
Posts: 4
tvqpriqphh is on a distinguished road
Thumbs down

The code sample doesn't describe a control array, rather "programmatic addressing". This code snippet does exactly the same thing (in a less rediculous fashion):

HTML Code:
select case txtwhotext.text
    case "1"
        textbox1.text = "1"
    case "2"
        textbox2.text = "2"
    case "3"
        textbox3.text = "3"
    case "4"
        textbox4.text = "4"
end select
if you want an array of controls, try something like this:

HTML Code:
' this is a class/form instance variable
' use nothing as a place holder for element zero in the zero based array
dim ca as control() = new control() {nothing, textbox1, textbox2, _
textbox3, textbox4}

sub dosomething()
    ' get the user specified index
    dim index as integer = cint(textwhotext.text)

    ' put the user specified message into that textbox
    ca(index).text = textwhotext.text

end sub
note that this really only gets you halfway home -- you now have a way to translate an integer index into a control object reference -- how do you convert a control object reference into an integer index?

HTML Code:
function getindex(c as control) as integer
    for i as integer = 1 to ca.length-1
        if c is ca(i) then
            return i
        end if
    next
    throw new applicationexception("control reference not found")
end function
now we can have "control array" based events like this (having to manually bind events makes maintaining this code a bit ugly)
HTML Code:
sub click_handler(sender as object, ea as eventargs) handles _
textbox1.click, textbox2.click, textbox3.click, textbox4.click
    dim i as integer = getindex(sender)
    ca(i).text = "you clicked me"

end sub
note that .net sort of obsoletes the need for control arrays, because you could just have easily defined click_handler as follows:

HTML Code:
sub load
    addhandler textbox1.click, addressof (click_handler)
    addhandler textbox2.click, addressof (click_handler)
    addhandler textbox3.click, addressof (click_handler)
    addhandler textbox4.click, addressof (click_handler)

end sub

sub click_handler(sender as object, ea as eventargs)
    ctype(sender, control).text = "you clicked me"

end sub
tvqpriqphh is offline   Reply With Quote
Old 08-08-2007, 12:08 AM   #3 (permalink)
CardsFan
Recruit
 
Join Date: Aug 2007
Posts: 1
CardsFan is on a distinguished road
Quote:
Originally Posted by sde View Post
Geoff Alexander, aka PUN1SH3R, has contributed a VB.NET article demonstrating how to mimick a control array since MS has decided to take that out of VB.NET.

You can check out the article here.
This code works GREAT. I modified it to work in a group box with an array of pictureboxes.

I have been looking all over the web for something simple to do this and either they say it can't be done or you get some convoluted code. This is a nice simple way that I can't believe no one else has posted.

There does need to be 1 minor change - in the 2nd FOR Next loop, it should be Controls.Count - 1 (Count is number of controls, but array starts at zero)

My modified code
Code:
For x As Integer = 7 To 1 Step -1
            For c As Integer = 0 To gbFanny.Controls.Count - 1
                If TypeOf (gbFanny.Controls(c)) Is PictureBox Then
                    Dim pbname As String = "pbFanny" & x.ToString
                    If gbFanny.Controls.Item(c).Name = pbname Then
                        Dim pb As PictureBox = gbFanny.Controls(c)
                        pb.Image = arCards(x)
                        'Exit For
                    End If
                End If
            Next
        Next
I think the previous poster misunderstood the point of the code. Of course you wouldn't do that for 4 simple text boxes but it's a great example of how to use control arrays - especially useful for Group Boxes that for some reason are unusually hard to manage.
CardsFan is offline   Reply With Quote
Old 09-07-2008, 03:20 PM   #4 (permalink)
james4560
Recruit
 
Join Date: Sep 2008
Posts: 1
james4560 is on a distinguished road
Control Array vb.net 2008
james4560 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:16 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting