Hi guys,
I want to achieve the following goals:
1,Reading data from Excel file( in this program, the data are X1 X2 X3 X4 and Z that represent the buttons location and text respectively).
2,Using buttonarray to draw series of buttons on the form according to the data input from Excel file. This can be done using a loop.
I followed the MSDN about Creating Control Arrays in Visual Basic. NET. Created a ButtonArray Class and a Form Class. And I also managed to read data from Excel file in an independent program. But I can't merge these two together.
Following is my code:
ButtonArray Class:
Form Class:
when running, an error"Value of type 'buttonarraytry.ButtonArray' cannot be converted to '1-dimensional array of System.Windows.Forms.Control'." comes out in the place of
Basicly, in the buttonarrya class I'm trying to read the data in Excel file and add the buttons to a buttonarray, and in the form1 class I'm trying to Add control array to the form.
Any comments?
Any help will be very very appriciated!! Thanks a lot!
I want to achieve the following goals:
1,Reading data from Excel file( in this program, the data are X1 X2 X3 X4 and Z that represent the buttons location and text respectively).
2,Using buttonarray to draw series of buttons on the form according to the data input from Excel file. This can be done using a loop.
I followed the MSDN about Creating Control Arrays in Visual Basic. NET. Created a ButtonArray Class and a Form Class. And I also managed to read data from Excel file in an independent program. But I can't merge these two together.
Following is my code:
ButtonArray Class:
VB.NET:
[SIZE=2][COLOR=#0000ff][COLOR=#000000]Imports Excel = Microsoft.Office.Interop.Excel
Public Class ButtonArray
Inherits System.Collections.CollectionBase
Private ReadOnly HostForm As System.Windows.Forms.Form
Dim X1 As Integer
Dim X2 As Integer
Dim Y1 As Integer
Dim Y2 As Integer
Dim Z As String
Public Function AddNewButton() As System.Windows.Forms.Button
' Create a new instance of the Button class.
Dim aButton As New System.Windows.Forms.Button()
Dim i As Integer
Dim oXLApp As Excel.Application 'Declare the object variables
Dim oXLBook As Excel.Workbook
Dim oXLSheet As Excel.Worksheet
oXLApp = New Excel.Application 'Create a new instance of Excel
oXLBook = oXLApp.Workbooks.Open("C:\Documents and Settings\intern\My Documents\godblessme.xls")
'Open an existing workbook
oXLSheet = oXLBook.Worksheets(1) 'Work with the first worksheet
For i = 1 To 100
X1 = oXLSheet.Cells.Item(i, 1).Value
Y1 = oXLSheet.Cells.Item(i, 2).Value
X2 = oXLSheet.Cells.Item(i, 3).Value
Y2 = oXLSheet.Cells.Item(i, 4).Value
Z = oXLSheet.Cells.Item(i, 5).Value
Console.WriteLine(Z)
' Set intial properties for the button object.
aButton.Top = X1
aButton.Left = X2
aButton.Width = Y1
aButton.Height = Y2
aButton.Tag = Me.Count
aButton.Text = Z
' Add the button to the collection's internal list.
Me.List.Add(aButton)
' Add the button to the controls collection of the form
' referenced by the HostForm field.
HostForm.Controls.Add(aButton)
Next i
Return aButton
oXLApp.Visible = True 'Show it to the user
oXLSheet = Nothing 'Disconnect from all Excel objects (let the user take over)
oXLBook = Nothing
oXLApp = Nothing
AddHandler aButton.Click, AddressOf ClickHandler
End Function
Public Sub New(ByVal host As System.Windows.Forms.Form)
HostForm = host
Me.AddNewButton()
End Sub
Default Public ReadOnly Property Item(ByVal Index As Integer) As _
System.Windows.Forms.Button
Get
Return CType(Me.List.Item(Index), System.Windows.Forms.Button)
End Get
End Property
Public Sub ClickHandler(ByVal sender As Object, ByVal e As _
System.EventArgs)
MessageBox.Show("you have clicked button " & CType(CType(sender, _
System.Windows.Forms.Button).Tag, String))
End Sub
End Class[/COLOR]
[/COLOR][/SIZE]
Form Class:
VB.NET:
Public Class Form1
' Declare a new ButtonArray object.
Dim MyControlArray As ButtonArray
#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
MyControlArray = New ButtonArray(Me)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.AddRange(MyControlArray)
End Sub
End Class
VB.NET:
Me.Controls.AddRange(MyControlArray) .
Basicly, in the buttonarrya class I'm trying to read the data in Excel file and add the buttons to a buttonarray, and in the form1 class I'm trying to Add control array to the form.
Any comments?
Any help will be very very appriciated!! Thanks a lot!
Last edited: