Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control Arrays

Status
Not open for further replies.

Developerman2000

Programmer
Dec 9, 2002
10
GB
Someone please HELP!!!!!!!!!!!!!!!!!!!!!!

i am starting a new project in VB.NET which has 100+ button objects, I want to array them so they r easier to control but don't seem to the best way to go about it.!!!

Any ideas out there

 
Hmmm - there has been an interesing change in VB.net from VB6 regarding this. Personally, I like it.

If you've only got a few buttons which you want to share the event, then simply create a sub like this

Code:
    Private Sub HandleSomething(ByVal sender As Object, ByVal e As EventArgs) [b]handles Button1.click, button2.click[/b]
        Dim btn As Button = CType(sender, Button)
        MessageBox.Show(btn.Text)
    End Sub

It is the handles clause which is replacing the concept of the control array here. Also, not in the code how I convert the sender object to get the button which fired the event.

If you have 100+ buttons, as you say, then you might elect to do this programmatically. For example,

Code:
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ctl As Control
        For Each ctl In Me.Controls
            If TypeOf ctl Is Button Then
                AddHandler ctl.Click, AddressOf HandleSomething
            End If
        Next
    End Sub

If you use this approach, you don't need the handles clause in the first sub.

This will make the event handle all the button click events on the form. You might want to filter them so that only buttons whose name contains a certain string are added, etc. Remember, also, that the index property is gone. But you still have the tag property, or you could do something clever with the name of the button, if the ordinal position of the button which was clicked is important (for example, in a grid)

Using this approach, you could even create the buttons programatically at run time, and then assign the event handler. See the designer code (assuming you are using VS.net) to get you started on creating controls programatically.

Does this help?

Mark [openup]
 
Thanks for that, what I am trying to do is a fully functional VB.net keyboard but never really used VB.Net b4 only in the last few months so perhaps I should stick to VB6 as the code examples you showed were helpful but still seems a step to far.

maybe if u could give me an example of few button controls in an array printing to a textbox(I hope I am not asking for to much).

many thanks

Developerman2000
 
Hello. No problem. Personally, I see VB.net as such an improvement over VB6 that I would stick with .net if I were you.

Here is what you want, I think

Create a new windows form in VS.net, and replace all the code (including the designer generated code) with this

I know there seems to be a lot, but once you collapse the designer region, it's not that much

I've put in the remarks all the cool things about .net I thought of while coding this up

Enjoy

Code:
Option Strict On ' a good idea - if you like Option Explicit in VB6 you'll love this
Public Class frmKeyboard
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnA As System.Windows.Forms.Button
    Friend WithEvents btnB As System.Windows.Forms.Button
    Friend WithEvents btnClose As System.Windows.Forms.Button
    Friend WithEvents txtOutput As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnA = New System.Windows.Forms.Button()
        Me.btnB = New System.Windows.Forms.Button()
        Me.btnClose = New System.Windows.Forms.Button()
        Me.txtOutput = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'btnA
        '
        Me.btnA.Location = New System.Drawing.Point(32, 48)
        Me.btnA.Name = &quot;btnA&quot;
        Me.btnA.TabIndex = 0
        Me.btnA.Tag = &quot;Key&quot;
        Me.btnA.Text = &quot;&A&quot;
        '
        'btnB
        '
        Me.btnB.Location = New System.Drawing.Point(112, 48)
        Me.btnB.Name = &quot;btnB&quot;
        Me.btnB.TabIndex = 1
        Me.btnB.Tag = &quot;Key&quot;
        Me.btnB.Text = &quot;&B&quot;
        '
        'btnClose
        '
        Me.btnClose.Location = New System.Drawing.Point(80, 232)
        Me.btnClose.Name = &quot;btnClose&quot;
        Me.btnClose.TabIndex = 2
        Me.btnClose.Text = &quot;&Close&quot;
        '
        'txtOutput
        '
        Me.txtOutput.Location = New System.Drawing.Point(32, 88)
        Me.txtOutput.Multiline = True
        Me.txtOutput.Name = &quot;txtOutput&quot;
        Me.txtOutput.Size = New System.Drawing.Size(200, 104)
        Me.txtOutput.TabIndex = 3
        Me.txtOutput.Text = &quot;&quot;
        '
        'frmKeyboard
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtOutput, Me.btnClose, Me.btnB, Me.btnA})
        Me.Name = &quot;frmKeyboard&quot;
        Me.Text = &quot;frmKeyboard&quot;
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmKeyboard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'put the click event for each button which has a tag of &quot;Key&quot; into our handler
        Dim ctl As Control
        For Each ctl In Me.Controls
            'there's a lot of .net stuff here 
            'AndAlso - the part after will not be executed unless it is true - 
            'in VB6 both parts are executed regardless and this would cause an error if the first was not true - you'd need a nested if to avoid this
            If Not ctl.Tag Is Nothing AndAlso ctl.Tag.ToString.ToLower = &quot;Key&quot;.ToLower Then
                'in .net, even a string literal is an object and has methods.
                AddHandler ctl.Click, AddressOf KeyClick
            End If
        Next
        'I'm putting another event in here for the B key just to show that in .net, an event can be handled by two different subs
        'the one caveat is that you don't know which will fire first, but hey!
        AddHandler btnB.Click, AddressOf ShowMessage
    End Sub

    Private Sub KeyClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim btn As Button
        Try 'in .net, we get structured error handling
            btn = CType(sender, Button) 'in .net, we get inheritance - button inherits from control - impossible in vb6
        Catch
            Throw New Exception(&quot;You've assigned a non button control to the KeyClick event&quot;) 'inline constructors for classes - both impossible in VB6
        End Try
        'we have to chop off the & from the text of the button (the & is for the accelerator key)
        'there are lots of ways to do this, easiest is to use the char datatype (a ByValue datatype) (not present in VB6)
        Dim c As String = btn.Text.Chars(1)

        txtOutput.Text &= c 'shorthand concatenation &= - we get this in .net too
    End Sub

    Private Sub ShowMessage(ByVal sender As Object, ByVal e As EventArgs)
        MessageBox.Show(&quot;You pressed button &quot; & sender.ToString) 'every object has a ToString method
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Dispose() 'destructors - again, not present in VB6
    End Sub
End Class
Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top