Yusuf Hermanto

Hello all,

i have question about the array control on vb.net 2005
How to create the array control like on vb.2005 and we create it on design not on code.

for example if i use the vb6 :
if we have two object or more and have the same of name its will creating the index for otomatically.
And this idea do try on vb.net 2005 but it not create index control for otomatically.

How to handdle this problem



Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

spotty

In .net forget control arrays - they arent supported and are unlikely to make a comeback.

You have a controls collection which means that you can search the collection for items of a specfic type and/or name.  

The changes in the framework mean that you can now hookup events code to a number of different control events - whereas the old control array meant you could hookup code only to that control array or would have to write a separate method and call it from a number of different control events.

An advantage of the .NET approach is that you can have collections of mixed control types - something control arrays didnt permit as they had to be of the same type. 

 

 

Controls are added to the form controls collection which allows you to iterate through.  So in this examples for Buttons named Button1, Button2, Button3  ....   I'll set the text on the buttons and add the event handler up to add the text to a textbox.   No need for control arrays

Public Class Form1
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.TextBox1.Text = Me.TextBox1.Text & CType(sender, Button).Text
        Me.TextBox1.Focus()
        Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each c As Control In Me.Controls
            If TypeOf (c) Is Button And c.Name.StartsWith("Button") Then
                Dim s As Integer = CType(c.Name.Replace("Button", ""), Integer)
                c.Text = s.ToString
                AddHandler c.Click, AddressOf Button_Click
            End If
        Next
    End Sub
End Class

 

If I wanted to simply set the values and hook up the event handlers at runtime then I can do the following.  

Public Class Form1
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        Me.TextBox1.Text = Me.TextBox1.Text & CType(sender, Button).Text
        Me.TextBox1.Focus()
        Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each c As Control In Me.Controls
            If TypeOf (c) Is Button And c.Name.StartsWith("Button") Then
                Dim s As Integer = CType(c.Name.Replace("Button", ""), Integer)
                c.Text = s.ToString               
            End If
        Next
    End Sub
End Class

 

 

 hope this will be helpful to you.





Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

Yusuf Hermanto

Ok. Thank you very mutch for your answer.

i was tryed for your code, and it very good.
And i have question again about to handdel event for the buttons.

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Me.TextBox1.Text = Me.TextBox1.Text & CType(sender, Button).Text
Me.TextBox1.Focus()
Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
End Sub


How to detecting that the Button1 is clicking
and why we must identification the buttons name on form_Load





Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

spotty

It detects that this code is hooked up to the following events by the handles clause- so in this case it shows button1.click, button2.click and button3.click


Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        Me.TextBox1.Text = Me.TextBox1.Text & CType(sender, Button).Text
        Me.TextBox1.Focus()
        Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
End Sub


The example was just using the form load to show iterating through the controls collection to set items on the buttons - you dont need to do this if you set everything at design time,   but you may set things up at design or at runtime or mixture of the two.

To hookup the event handler at design time look at the keyword handles in the help, to setup the event handler at runtime look at addhandler method.    The controls on the form are simply a collection which can be iterated over to get the required controls - or referenced by name.   Me.Controls("Button1")

 The best way to learn is to play around with very simple code and understand what each item does,  the help provides much more information on the details of each method/keyword along with samples.

So these examples are to show ways of achieving things which you should be able to apply to what you are trying to achieve.

 

 






Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

Yusuf Hermanto

ok thank.

i have code on vb 6 like this :
i have 2 button, have name cmd

Private Sub cmd_Click(Index As Integer)
select case index
case 0
'it the button 0
case 1
'it the button 1
end select
End Sub

and than how if my code on this code :

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Me.TextBox1.Text = Me.TextBox1.Text & CType(sender, Button).Text
Me.TextBox1.Focus()
Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
End Sub








Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

spotty


Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim s As String = CType(sender, Control).Name
'which Control triggered the click event
MsgBox(s & " was clicked")
End Sub
End Class

The variable s will show you which control caused the Button_Click event to be called. So you can use this in a select case statement if you want using the control name.

Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim s As String = CType(sender, Control).Name
'which Control triggered the click event
MsgBox(s & " was clicked")
Select Case s
Case "Button1"
Case "Button2"
Case "Button3"
End Select
End Sub
End Class





Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

Yusuf Hermanto

Thank you




Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

spotty

Now that youve seen how these work you'll start to realize that instead of only being able to write code in the event for a single control array in VB6. In .NET you can hook up the code to any number of events from any number of different controls.

So the following example hooks up the same method to a button click event, label click event and a checkbox click event. 3 different control types. Something no possible in controls arrays in VB6

Example

Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, CheckBox1.Click, Label1.Click
MsgBox(CType(sender, Control).Name)
End Sub
End Class





Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

Yusuf Hermanto

Ok.

It's the good code.
what it is OOP programming




Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

Squire James

It stands for "Object Oriented Programming". Defining it completely takes a book or two, but basically it is the idea that programs are made more or less entirely of "things" defined so that they function the same way in a variety of situations. Normally, each "thing" is implemented as a Class in C++ and modern .Net languages. The opposite of OOP is procedure-based programming, where everything is thought of as a "process" rather than a "thing".

Hopefully, that is enough to visualize the basic concept of OOP (and I didn't get anything horribly wrong).





Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

spotty

Please try to keep the questions in the post relevent to the Thread title.

If its a completely different question the start a new thread - that way it makes searching for answers easier based upon the title matching the content of the thread.





Re: Visual Basic Express Edition Create Array Control like on vb 6 ?

RobBattaglia

This information helped me a lot. I have one question. I used to use arrays to get around the issue of only being able to place 254 controls on a form in VB6. Is this still the limit in VB 2005, 2008, .net etc If so, how do you get around this issue

Thanks again for posting an answer to this question.

Regards,

Rob