-
Mar 9th, 2005, 06:35 AM
#1
Thread Starter
PowerPoster
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:
Dim arrTextBox(0) As TextBox ' (Prepare array to receive textBoxes.
' Give this the necesary scope)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim txtTemp As TextBox ' ( Reserve variable for new textbox
' Local scope only OK)
Dim iCount As Integer (Local scope only)
For iCount = 0 To 9
ReDim Preserve arrTextBox(iCount) '(Expand array to receive
' next textbox
txtTemp = New TextBox ' ( Create instance of new textbox)
txtTemp.Name = "txtBox" & iCount.ToString '(Name textboxes with
'sequential numbering)
txtTemp.Width = 40
txtTemp.Location = New Point(15, iCount * 20)
Me.Controls.Add(txtTemp) '(Add new textbox to form
arrTextBox(iCount) = txtTemp '(Place reference to new textbox
'in appropriate element of array)
'(The next line adds the handle of the new textbox to the appropriate event)
AddHandler txtTemp.TextChanged, AddressOf TextChangedEvent
Next
End Sub
Private Sub TextChangedEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show(CType(sender, TextBox).Name & " Text was changed")
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.
-
Mar 10th, 2005, 07:33 AM
#2
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.
-
Mar 10th, 2005, 10:52 AM
#3
Thread Starter
PowerPoster
Re: Control Arrays
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.
-
Aug 5th, 2005, 04:09 PM
#4
Fanatic Member
Re: Control Arrays
Okay.... so where is the rest of the code?
-
Aug 15th, 2005, 05:33 AM
#5
Thread Starter
PowerPoster
Re: Control Arrays
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.
-
Jan 18th, 2006, 08:25 AM
#6
Member
Re: Control Arrays
Thanks for the explanation Taxes, but is it possible to add a handler to the control array itself?
-
Jan 18th, 2006, 10:13 AM
#7
Thread Starter
PowerPoster
Re: Control Arrays
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.
-
Jan 18th, 2006, 01:39 PM
#8
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:
For i As Integer = 1 to 10
WithEvents T as new TextBox
T.Location = New Point(0,i*10)
T.Size = New Size(200,25)
T.Tag = "GUITextBoxes" 'or whatever.
Me.Controls.Add(T)
Next i
'and
For Each Ctl As Control In Me.Controls
If TypeOf(Ctl) Is TextBox AndAlso DirectCast(Ctl,TextBox).Tag = "GUITextBoxes" Then
DirectCast(Ctl,TextBox).Text = "I'm a dynamically Created TextBox"
End If
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
-
Jan 19th, 2006, 06:13 AM
#9
Member
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.
-
Jan 20th, 2006, 10:53 AM
#10
Member
Re: Control Arrays
What is the easiest way to obtain the index of a control in the event handler (using this method)?
-
Jan 20th, 2006, 11:57 PM
#11
Re: Control Arrays
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:
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.
-
Jan 21st, 2006, 01:08 AM
#12
Thread Starter
PowerPoster
Re: Control Arrays
Originally Posted by jmcilhinney
VB Code:
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.
-
Jan 21st, 2006, 01:30 AM
#13
Thread Starter
PowerPoster
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.
-
Jan 21st, 2006, 02:44 AM
#14
Member
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)?
-
Jan 21st, 2006, 03:22 AM
#15
Thread Starter
PowerPoster
Re: Control Arrays
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.
-
Jan 21st, 2006, 03:39 AM
#16
Member
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
-
Jan 21st, 2006, 03:53 AM
#17
Thread Starter
PowerPoster
Re: Control Arrays
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.
-
Jan 21st, 2006, 08:23 AM
#18
Re: Control Arrays
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:
Dim myControl As Control = CType(sender, Control)
Dim myTextBoxBase As TextBoxBase = CType(sender, TextBoxBase)
Dim myTextBox As TextBox = CType(sender, TextBox)
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.
-
Jan 21st, 2006, 11:36 AM
#19
Member
Re: Control Arrays
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
-
Jan 21st, 2006, 04:24 PM
#20
Thread Starter
PowerPoster
Re: Control Arrays
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.
-
Jan 21st, 2006, 04:26 PM
#21
Thread Starter
PowerPoster
Re: Control Arrays
Originally Posted by jmcilhinney
VB Code:
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.
-
Jun 15th, 2010, 10:13 PM
#22
Fanatic Member
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)
-
Jun 15th, 2010, 10:38 PM
#23
Re: Control Arrays
Originally Posted by Gamemaster1494
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.
-
Jun 15th, 2010, 10:41 PM
#24
Fanatic Member
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. =(
-
Jun 15th, 2010, 10:46 PM
#25
Re: Control Arrays
Originally Posted by Gamemaster1494
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|