[Year 12 SofDev] creating event handlers for multiple code created objects

Alex Hopkins a.hopkins at baysidecc.vic.edu.au
Mon Apr 1 17:10:25 EST 2013


Hi Ben,

I'm not sure if I understand what is causing problems, but offer the
following which I hope will deal with the issue you raise. It was coded in
the 2008 version of VB.NET.

Public Class Form1
    Dim cx, cy As Integer 'Variables for width and height of form
    Dim aButton(6) As Button 'An array of buttons
    Dim myLabel As Label

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        InitializeScreen()
        SetupButtons()
        SetupALabel()
    End Sub

    Private Sub InitializeScreen()
        Me.WindowState = FormWindowState.Maximized
        cx = Me.ClientSize.Width
        cy = Me.ClientSize.Height
    End Sub

    Private Sub SetupButtons()
        Dim iButtonWidth, iButtonHeight As Integer 'Variables for the
height and width of each button
        iButtonWidth = 200
        iButtonHeight = 220

        Dim iHorizontalSpacing As Integer 'Variable for the spacing to be
used between each button
        iHorizontalSpacing = 20

        'Variables to hold distance between top of screen (strictly, client
area) and top of the row of buttons,
        'and the distance between the left of screen and the left-most
button
        Dim iHorizontalMargin, iVerticalMargin As Integer
        iHorizontalMargin = (cx - ((aButton.GetUpperBound(0) + 1) *
iButtonWidth) - (aButton.GetUpperBound(0) * iHorizontalSpacing)) / 2
        iVerticalMargin = (cy - iButtonHeight) / 2

        'Instantiate buttons and set properties
        For x As Integer = 0 To aButton.GetUpperBound(0)
            aButton(x) = New Button
            aButton(x).Parent = Me
            aButton(x).Width = iButtonWidth
            aButton(x).Left = iHorizontalMargin + (x * (iButtonWidth +
iHorizontalSpacing))
            aButton(x).Top = iVerticalMargin
            aButton(x).Height = iButtonHeight
            aButton(x).BackColor = Color.LightBlue
            AddHandler (aButton(x).GotFocus), AddressOf Button_GotFocus
            AddHandler (aButton(x).LostFocus), AddressOf Button_LostFocus
            'We will use the tag to allow us a quick way of reporting which
of the buttons has focus
            'For an explanation of why use of the tag property runs counter
to OOP principles, see, e.g.
            'Matt Tagliaferri's "Visual Basic .NET! I Didn't Know You Could
Do That..." pp19-20)
            aButton(x).Tag = CStr(x)
        Next
    End Sub

    Private Sub Button_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
        Dim ctrl As Control = CType(sender, Control)
        ctrl.BackColor = Color.Yellow
        myLabel.Text = "Button " & ctrl.Tag & " has the focus."
    End Sub

    Private Sub Button_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs)
        Dim ctrl As Control = CType(sender, Control)
        ctrl.BackColor = Color.LightBlue
    End Sub

    Private Sub SetupALabel()
        myLabel = New Label
        myLabel.Parent = Me
        myLabel.Width = 150
        myLabel.Left = (cx - myLabel.Width) / 2
        myLabel.BackColor = Color.BlanchedAlmond
        myLabel.Top = 200
        myLabel.TextAlign = ContentAlignment.MiddleCenter
    End Sub

End Class



Best regards,

Alex Hopkins



On 28 March 2013 14:52, Ben Hines <b.hines at ccg.vic.edu.au> wrote:

>  Hi guys (happy end of term 1).****
>
> ** **
>
> I keep running into the issues of not being able to create an event
> handler for an array of code created objects.****
>
> ** **
>
> For example, I use a for loop to create a whole heap of picture boxes that
> I assign to an array.****
>
> ** **
>
> For some reason when I try to connect those picture boxes in the for loop
> to an event handler, the even never runs.****
>
> ** **
>
> However, if I just do it to a single object it works fine…****
>
> ** **
>
> Has anyone ever come across this (I use VB.net)****
>
> ** **
>
> Thanks****
>
> ** **
>
> Ben Hines****
>
>  *Ben Hines
> *Mathematics and ICT Teacher
> Senior Campus
> (03)5241 1577
>
> [image: Christian College Geelong]
>
> * *Please consider the environment before printing this email.
>
>
> This e-mail is intended for the use of the named individual or entity and
> may contain confidential and privileged information. Any dissemination,
> distribution or copying by anyone other than the intended recipient of this
> e-mail is strictly prohibited. If this e-mail has been received in error,
> then please notify Christian College or the author of this email
> immediately and destroy the original message. We have made every attempt to
> ensure this e-mail message is free from computer viruses however the
> attached files are provided on the basis that the user assumes all
> responsibility for use of the material transmitted. Views, opinions, etc.
> expressed reflect those of the author and not Christian College nor its
> associated companies and campuses which includes Eden Quality Services Pty
> Ltd.
>
> ------------------------------
> Message protected by MailGuard: e-mail anti-virus, anti-spam and content
> filtering.
> http://www.mailguard.com.au
>
>
>
> _______________________________________________
> http://www.edulists.com.au - FAQ, Subscribe, Unsubscribe
> IT Software Development Mailing List kindly supported by
> http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment
> Authority and
> http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html
> http://www.vitta.org.au  - VITTA Victorian Information Technology
> Teachers Association Inc
> http://www.swinburne.edu.au/ict/schools - Swinburne University
>



-- 
Dr Alex Hopkins

Bayside Christian College
120-128 Robinsons Rd
Langwarrin South, VIC 3911
Ph: 03 5971 6700
Fx: 03 5971 3810
Em: a.hopkins at baysidecc.vic.edu.au
Wb: www.baysidecc.vic.edu.au
"Unity and Maturity in Christ"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20130401/261958a6/attachment.html 


More information about the sofdev mailing list