Click to See Complete Forum and Search --> : Control Arrays?
nolc
May 22nd, 2002, 02:32 PM
I've created a user control and I'm having trouble with creating an array of these user controls at runtime.
This is what I'm looking to do......
My application as three different views. If it is View1, it goes out to a database and grabs the caption properties and hidden properties from table tblView1 and loads the properties in for each new control I have for this view. Any ideas?
Control Array Changes in Visual Basic .NET
See Also
Event Changes in Visual Basic .NET | Control Changes in Visual Basic .NET |
Setting the Tab Order on Windows Forms
In Visual Basic 6.0, control arrays could be used to specify a group of
controls that shared a set of events. The controls had to be of the same
type, and they had to have the same name.
In Visual Basic .NET, control arrays are no longer supported. Changes to the
event model make control arrays unnecessary. Just as control arrays in
Visual Basic 6.0 could share events, the event model in Visual Basic .NET
allows any event handler to handle events from multiple controls. In effect,
this allows you to create groups of controls of disparate types that share
the same events.
For example, you might add two Button controls (Button1 and Button2) and a
CheckBox control (CheckBox1) to a form, then create an event handler to
handle the Click event for all three controls:
Private Sub MixedControls_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click, CheckBox1.Click
Another feature of Visual Basic 6.0 control arrays was the ability to
reference a control by its Index property. Although Visual Basic .NET
controls do not have an Index property, you can duplicate the functionality
using another common property such as the TabIndex or Tag property.
For example, you might set the TabIndex property for a group of controls
using the new visual tab ordering capabilities of Windows Forms, and then
use the TabIndex in a Select Case statement:
Private Sub MixedControls_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click, CheckBox1.Click
Select Case sender.TabIndex
Case 0
MsgBox("Button 1")
Case 1
MsgBox("Button 2")
Case 2
MsgBox("CheckBox 1")
End Select
End Sub
Use build-in help - that helps :)
nolc
May 22nd, 2002, 03:45 PM
OK...that helps.
BUT....what I'm having trouble doing is creating an unspecified amount of controls. Lets say nBtn = 6, how do I programmatically add these six controls?
I know you can....
Dim btn1 as new Button()
Dim btn2 as new Button()
Dim btn3 as new Button()
Dim btn4 as new Button()
Dim btn5 as new Button()
Dim btn6 as new Button()
then add them, but it won't always be that I'm adding 6 controls. Let's say next time I want to add 3 nBtn controls. Is there a way to go "For i = 1 to n....create 3 controls"?
??????????????????????????????
for i = 1 to n
dim btn(i) as new Button()
btn(i).Text = "Button " & i
next
for i = 1 to n
Contols.Add(btn(i))
next
??????????????????????????????
Thanks again!
Akim
May 22nd, 2002, 04:01 PM
Try this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 10
Dim btn As New Button()
Me.Controls.Add(btn)
btn.Show()
btn.Left = i * btn.Width
btn.Name = i
btn.Text = btn.Name
Next
End Sub
Got the idea?
nolc
May 22nd, 2002, 04:07 PM
Ok..Ok..looks good!
So...then if I wanted to create an array I would just set btn.Tag = i?
HUGE :D THANKS!!
Akim
May 22nd, 2002, 04:21 PM
Lets modify the code a little:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 10
Dim btn As New Button()
Me.Controls.Add(btn)
btn.Show()
btn.Left = i * btn.Width
btn.Name = "but" & i
btn.Text = btn.Name
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Name = "but1" Then
MessageBox.Show("You got me!")
Exit Sub
End If
Next
MessageBox.Show("Did you click Button1?")
End Sub
Honestly, I've never played with that in .NET :cool:
nolc
May 23rd, 2002, 08:58 AM
Do you have any ideas how to get all the User Controls to share functionality?....I'm still having problems. Also, I can make a reference to my User Control, but I cannot find it in the Toolbox and/or added to during Design time. Any idears?
MY CODE:
' works fine
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, y As Integer
y = 0
For i = 1 To 7
Dim userCtl As New ctlChkTxtDrop()
Me.Controls.Add(userCtl)
userCtl.Show()
userCtl.Location = New Point(0, y)
userCtl.Name = i
userCtl.ChkTxtDropCaption = "User Control #" & i
userCtl.Tag = userCtl.Text
y = y + 30
Next
End Sub
Akim
May 24th, 2002, 07:35 AM
I was a bit busy yesterday :)
I used a button instead of your control.
Replace Dim userCtl As New Button()
with Dim userCtl As New ctlChkTxtDrop().
And userCtl.Text = "User Control #" & i with userCtl.ChkTxtDropCaption = "User Control #" & i
That should work.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, y As Integer
y = 0
For i = 1 To 7
Dim userCtl As New Button()
Me.Controls.Add(userCtl)
userCtl.Show()
userCtl.Location = New Point(0, y)
userCtl.Name = i
userCtl.Text = "User Control #" & i
userCtl.Tag = userCtl.Text
y = y + 30
AddHandler userCtl.Click, AddressOf UserCtl_Clicks 'Add an Even Handle
Next
End Sub
Sub UserCtl_Clicks(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Click from " & sender.text)
End Sub
nolc
May 24th, 2002, 08:35 AM
Thanks Akim.
This is what I came up with yesterday, too. BUT once I find out what control has been clicked I would like to find the following ctlChkTxtDrop Control's Locations. My control is a Checkbox with a textbox underneath it with Visible = False & my control Height = 24. If the Checkbox Checked value = True...make the Textbox visible, expands the Control Height = 48 and move the following ctlChkTxtDrop controls Y cordinates down 30. Any ideas?
It's basically like a Search Engine. If ctlChkTxtDrop.Checked = True expand the Searchable Field below the CheckBox.
Public Class Form2
Inherits System.Windows.Forms.Form
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Dim intIndex, Ycord As Integer
Dim objControl As ctlChkTxtDrop
'Dim objControl As Button
Ycord = 0
For intIndex = 1 To 5
Dim i As Integer = intIndex
objControl = New ctlChkTxtDrop()
'objControl = New Button()
objControl.Name = "userCtl" & i & "of5"
objControl.ChkTxtDropCaption = "User Control #" & i
objControl.Tag = i '- 1
objControl.Location = New Point(0, Ycord)
objControl.ChkTxtDropBackColor = Color.Blue
AddHandler objControl.SizeChanged, AddressOf ctlChkTxtDrop_Click
'AddHandler objControl.Click, AddressOf Button_OnClick
Controls.Add(objControl)
Ycord = Ycord + 30
Next
End Sub
Private Sub ctlChkTxtDrop_Click(ByVal sender As Object, ByVal e As EventArgs)
Select Case CType(sender, ctlChkTxtDrop).Name
Case Is = "userCtl1"
MsgBox("ctlChkTxtDrop1_Click")
Case Is = "userCtl2"
MsgBox("ctlChkTxtDrop2_Click")
Case Is = "userCtl3"
MsgBox("ctlChkTxtDrop3_Click")
Case Is = "userCtl4"
MsgBox("ctlChkTxtDrop4_Click")
Case Is = "userCtl5"
MsgBox("ctlChkTxtDrop5_Click")
End Select
End Sub
Thanks in advanced! ;)
Akim
May 24th, 2002, 09:13 AM
I'll stick with my example (I don't have your control):
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, y As Integer
y = 0
For i = 1 To 7
Dim userCtl As New Button()
Me.Controls.Add(userCtl)
userCtl.Show()
userCtl.Location = New Point(0, y)
userCtl.Name = i
userCtl.Text = "User Control #" & i
userCtl.Tag = i
y = y + 30
AddHandler userCtl.Click, AddressOf UserCtl_Clicks 'Add an Even Handle
Next
End Sub
Sub UserCtl_Clicks(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strMess As String
Dim userCtl As Button
For Each userCtl In Me.Controls
If userCtl.Tag = sender.tag + 1 Then
MessageBox.Show("Click from " & sender.text & vbCrLf & "Next Control coords: " & userCtl.Left & ";" & userCtl.Top)
End If
Next
End Sub
Is that what you want to achieve?
nolc
May 24th, 2002, 09:32 AM
Kind of....but it won't work if I have other controls on the form. BUT I could put these controls in a Panel or Groupbox. I will also need the other button control's Y cordinates below the one clicked, so I can move the following button controls down 30.
I think the best way of maybe doing this is storing the Y cords in an array in the Form Load section. Agree?
Thanks again.....
Akim
May 24th, 2002, 10:19 AM
How does the following sound?
'Declare Private colButtons As New Collection() - Form level declaration
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, y As Integer
y = 0
For i = 1 To 7
Dim userCtl As New Button()
Me.Controls.Add(userCtl)
userCtl.Show()
userCtl.Location = New Point(0, y)
userCtl.Name = i
userCtl.Text = "User Control #" & i
userCtl.Tag = i
y = y + 30
colButtons.Add(userCtl, i)
AddHandler userCtl.Click, AddressOf UserCtl_Clicks 'Add an Even Handle
Next
End Sub
Sub UserCtl_Clicks(ByVal sender As System.Object, ByVal e As System.EventArgs)
If sender.tag < colButtons.Count Then
MessageBox.Show("Click from " & sender.text & vbCrLf & "Next Control coords: " & colButtons.Item(sender.tag + 1).Left & ";" & colButtons.Item(sender.tag + 1).Top)
End If
End Sub
In this case you can access to any control easily.
nolc
May 24th, 2002, 10:27 AM
That will work! ;) Thanks for all your help!
Akim
May 24th, 2002, 10:44 AM
You're welcome :)
Also, I just found that one (you might want to take a look):
From MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchcreatingcontrolarraysinvisualbasicnetvisualcnet.asp)
URL is too long, so just delete "%20" and "<br>" from your address line to get to the right page (you'll see what I mean)
nolc
May 24th, 2002, 10:57 AM
Hey Akim,
Forget the Private Message....I was able to get to the correct URL once I deleted the %20 and <br>.
thanks again my friend!:D
Akim
May 24th, 2002, 10:59 AM
Too late :)
Good luck.
John G Duffy
May 25th, 2002, 09:05 AM
Here is another source from MSDN. Its 21 pages long
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchcreatingcontrolarraysinvisualbasicnetvisualcnet.asp
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.