How can we define control array in VB.Net

  • Thread starter Thread starter Sarfaraz Khan
  • Start date Start date
S

Sarfaraz Khan

Hello,
anyone can guid me how we can handle and define control array in vb.net?

Thanks & regards

SARFARAZ KHAN
 
Sarfaraz,

Dim ctrArea As Control() = New Control() {me, Button1, Etc}

I think you are not after that, however than you have to tell more?

Cor
 
Sarfaraz,

If you mean adding controls dynamically to a form, you do it in the code:

Create a control object at the module level (before the Windows Form
Designer generated code):

Private MyLabel as Label

Then, after InitializeComponent, or in the Load event, set the position and
visibility of the control and add it to the Controls collection of the Form:

With MyLabel
.Top = 40
.Left = 40
.Visible = True
End With
Me.Controls.Add(MyLabel)

That's it. Now, to have an ARRAY of controls, create an array at the module
level:

Private MyLabels as Label()

Then in the Load event, execute similar code for each of the objects in the
array:

Dim i as Integer

ReDim MyLabels(9)
For i = 0 To 9
MyLabels(i) = New Label
With MyLabels(i)
.Top = (i * 40) + 40
.Left = 40
.Visible = True
End With
Me.Controls.Add(MyLabels(i))
Next i

In order to change your control later in the form, just call your
module-level object:

MyLabels(5).Text = "Some New Text"

Hope this helps.
 
If you are trying to create a common handler for a series of controls you
can modify the handle statement(s) at the end of each event handler:

For example:
Private Sub TextBox1_TextChanged(ByVal sender as System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged etc.

Dim ltxtBox as TextBox
ltxtBox = CType(sender, TextBox)

Select Case ltxtBox.Name
Case "TextBox1"
'do something
Case "TextBox2"
'do something
End Select

End Sub
 
<URL:http://groups.google.de/groups?selm=#[email protected]
x.gbl>

"In VS.NET 'Whidbey' (2005) control arrays will be supported natively"? I
hadn't heard about that. where might I find info on this particular subject?
Is it as simple as cracking open the scaled-down MSDN that came with the
beta and looking up "control array"? (I don't have the VPC that I'm running
Whidbey in up at the moment, nor even the computer that runs it, or I'd
check myself.)
 
replacement of these code into VB.Net?

Private Sub Command1_Click(index as integer)
Select Case index
Case 0
'Some Code
Case 1
'Some Code

End Select
End Sub


and how i place control on the form that make automaticaly control array
like in VB 6

Thank & Regards
 
replacement of these code into VB.Net?

Private Sub Command1_Click(index as integer)
Select Case index
Case 0
'Some Code
Case 1
'Some Code

End Select
End Sub

Private Sub CommonButtonClickHandler(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles AddButton.Click, DeleteButton.Click[, ...]
If sender Is DeleteButton Then
' Do something
ElseIf sender Is AddButton Then
' Do something else
[etc.]
End If
End Sub
and how i place control on the form that make automaticaly control array
like in VB 6

You don't. .NET has done away with the VB6 concept of control arrays.
 
Sarfaraz,
and how i place control on the form that make automaticaly control array
like in VB 6
As Jeff stated you can not do it the same way, however you can place your
control in every array and than process it like that. A simple one
\\\
Dim ctrArr as new Arraylist
ctrArr.Add(myButton1)
dim buttonname as string = directcast(ctrArr(0),button).name
///

So you can make as much controlarrays as you want.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top