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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control Array

Status
Not open for further replies.

UKmedia

Programmer
Nov 2, 2002
90
Morning All,

I have a problem with a control array I am setting up to place some buttons on a form but the information like colour, caption, tag reads from a database.

Code:
        Dim strIndex As Short
        Dim rs As New ADODB.Recordset()

        rs.Open("SELECT * FROM tblButtons", conn)

        If rs.EOF Then
        Else
            Do While Not rs.EOF

                rs.MoveFirst()
                strIndex = rs.Fields("ButtonID").Value
                btnButtons(strIndex) = New System.Windows.Forms.Button()
                btnButtons(strIndex).Text() = rs.Fields("Description").Value
                btnButtons(strIndex).Tag() = rs.Fields("Price").Value
                btnButtons(strIndex).BackColor() = rs.Fields("Colour").Value
                btnButtons(strIndex).Size() = New Size(75, 58)
                AddHandler btnButtons(strIndex).Click, AddressOf Me.btnButtons_Click
                rs.MoveNext()
            Loop
        End If

        'Add control array to the form superclass
        Me.Controls.AddRange(btnButtons)

As you can see from the code this is how I asm doing it, proberly not the best way but it does kinda work but gets stuck on

btnButtons(strIndex) = New System.Windows.Forms.Button()

says NullReferenceException was unhandled

any ideas?

UKmedia productions
 
Where do you define the array of buttons???

Code:
Dim btnButtons() As System.Windows.Forms.Button

This is missing from your example....



As a side note, I would recommend you also look into ADO.NET to access you data instead of the ADODB connections.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
I agree with you on the ado.net instead of adodb but I have been trying for about 3 weeks to get my head around it still with no joy lol.

UKmedia productions
 
Just wanted to throw the ADO.Net out there....glad you are working towards it. It will come.

Did you just add the array definition as I posted it?? That might not work. I was using a generic example.

You will need to define a size for the array. If you know it will always be the same size, then you can use something like:

Code:
Dim btnButtons(9) As Button

That is ten buttons. If you don't know the array size, you will have to either create an array that is larger and just have some extra empty items, or you can work with it dynamically.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
lol, Yeah the ado.net thing is harder than i though tbh.

I can read a database but not insert/update data.

This is as far as I have got lol

Code:
Dim DR As System.Data.SqlClient.SqlDataReader
        Dim strSQL As String = "SELECT * FROM tblUsers"
        Dim SqlComm As New System.Data.SqlClient.SqlCommand(strSQL, SqlConn)

        DR = SqlComm.ExecuteReader

        txtUsername.Text = DR.Item("UserName")

        DR.Close()
        SqlComm.Dispose()
        DR = Nothing
        SqlComm = Nothing
[\code]

The thing is I know vb6 inside out but this .net is like learning a whole new language again basilcally.

I will try that now


UKmedia productions
[URL unfurl="true"]http://www.uk-media.com[/URL]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top