an array of buttons or indeed other objects

gunslinger

Member
Joined
Dec 19, 2005
Messages
5
Programming Experience
1-3
because ms in their great wisdom got rid of control arrays, my simple graphic keyboard (developed for touchscreens that i often use) needs to be recoded completely when it comes to .net.

so, rant over. here is a small portion of what I am trying to do. my projects generally go to industrial sites (pharmaceutical, chemical etc.) so I have to provide absolute guaruntees that data entered is valid. so depending on the circumstances I may want to disable the alphabetic buttons on my graphic keyboard, or the numeric ones or the other keys (!"£$%^:<. etc).

I have a windows form with loads of buttons on. The buttons share a common event handler.

so I have the following code (as a snippet)
VB.NET:
'private
'an array that holds the buttons that I have deemed as alphabetic)
Private alphabeticCharacters() As Button = {bttn13, bttn14, bttn15, bttn16, bttn17, bttn18, bttn19, bttn20, bttn21, bttn22, bttn25, bttn26, bttn27, bttn28, bttn29, bttn30, bttn31, bttn32, bttn33, bttn38, bttn39, bttn40, bttn41, bttn42, bttn43, bttn44}

'public
'a variable that is initially false but can be set later
 Dim disableAlphabeticCharacters As Boolean = False

'now the code for disabling lots of buttons from my array.

For i As Integer = 0 To alphabeticCharacters.Length
            'create a temporary button object.
            Dim myButton As New Button
            'point the temporary button object to the relavant slot in the
            'alphabeticCharacter array.
            myButton = alphabeticCharacters(i)
            'set the status of the temporary button.
            myButton.enabled = not disableAlphabeticCharacters
 Next
I keep getting an error when I run my code and then load tye keyboard form. the error is "Object reference not set to an instance of an object" and when I debug myButton is set to nothing.

Anyone have any ideas, or I am I using the syntax incorrectly?

gunslinger
 
solution

VB.NET:
#Region "Public Variable Declarations"
    'variables that determine which keys are allowed to be pressed.
    Public allowDecimalPoint As Boolean = True
    Public disableAlphabeticCharacters As Boolean = False
    Public disableNumericCharacters As Boolean = False
    Public disableOtherCharacters As Boolean = False
#End Region

    'buttons that have alphabetic characters.
    Private alphabeticCharacters() As Button

    'buttons that have numeric characters.
    Private numericCharacters() As Button

    'buttons that have other characters (!,",*,:, etc.)
    Private otherCharacters() As Button


    Private Sub frmKeyboard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'set up button arrays.
        setupButtonArrays()
        'set up the keys allowed.
        setupAllowedKeys()

    End Sub
Here is the main change, before I declared the array as a public variable. I have done the same again except this time I don't specify the contents of the array. This was the problem as I was adding the buttons to the array, but they are not created until the form is loaded, so now I add the buttons to the array after the form has loaded when the button has an actual instance.

VB.NET:
'****************************************************************
    ' procedure name: setupButtonArrays
    ' description        : adds the buttons to a relavant button array depending on
    '                         : whether the button is a character, numeric or other button.
    '****************************************************************
    Private Sub setupButtonArrays()

        'set up the alphabetic character array with the buttons that are alphabetic.
        alphabeticCharacters = New Button() {bttn13, bttn14, bttn15, bttn16, bttn17, _
                                             bttn18, bttn19, bttn20, bttn21, bttn22, _
                                             bttn25, bttn26, bttn27, bttn28, bttn29, _
                                             bttn30, bttn31, bttn32, bttn33, bttn38, _
                                             bttn39, bttn40, bttn41, bttn42, bttn43, _
                                             bttn44}
        'set up the numeric character array with the buttons that are numeric.
        numericCharacters = New Button() {bttn1, bttn2, bttn3, bttn4, bttn5, _
                                          bttn6, bttn7, bttn8, bttn9, bttn10}
        'set up the other character array with the buttons that are not alphabetic or
        'numeric.
        otherCharacters = New Button() {bttn0, bttn11, bttn12, bttn23, bttn24, _
                                        bttn34, bttn35, bttn36, bttn37, bttn45, _
                                        bttn46, bttn47}

    End Sub

   '**********************************************************************
    ' procedure name : setupAllowedKeys
    ' description         : loops through the arrays that hold the different types of
    '                         : button and sets the enabled status of them, depending on what
    '                         : buttons are allowed to be pressed.
    '**********************************************************************
    Private Sub setupAllowedKeys()

        'set up the alphabetic buttons status'.
        For i As Integer = 0 To alphabeticCharacters.Length - 1
            'set the status of button i.
            alphabeticCharacters(i).Enabled = Not disableAlphabeticCharacters
        Next

        'set up the numeric buttons status'.
        For i As Integer = 0 To numericCharacters.Length - 1
            'set the status of  button i.
            numericCharacters(i).Enabled = Not disableNumericCharacters
        Next

        'set up the other buttons status'.
        For i As Integer = 0 To otherCharacters.Length - 1
            'set the status of button i.
            otherCharacters(i).Enabled = Not disableOtherCharacters
        Next

    End Sub


gunslinger
 
Microsoft ditched ControlArrays because they did not fit the OO nature of VB.NET. There is I-don't-know-how-much literature dedicated to this subject on MSDN, so it's not like they've just dumped you with no guidance. My advice is to assume that VB.NET is a new language and just consider it a bonus if something turns out to work the same way as it did in VB6.
 
Back
Top