Sara Posted July 21, 2003 Posted July 21, 2003 Hi, I want to define a datagrid array DataGrid[] dg; dg = new DataGrid[5]; It is possible define a datagrid array in form design instace of define this in my code??? (In Visual Basic 6.0 in my form design if I define two datagrid with the same name ... it create automatically a datagrid array). Thanks Sara Quote
bfellwock Posted July 28, 2003 Posted July 28, 2003 Public Class Form1 Inherits System.Windows.Forms.Form 'Form level variable Private myDataGrids As New Collection() Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'First we create some new data grids and put them on the form. 'You can just put them on the form manually instead of 'creating them dynamically as I have done here. 'Now your original question was "I want to define a datagrid array?" 'I suggest putting the grids into a collection. Collections are good 'for holding arrays of objects. 'I then add the grids to the collection right at 'the beginning and access them from that collection throughout the example. 'So, this is a good way to create an array of Data Grids. 'However, having programmed in VB6, I think what you are trying to get at is that 'in VB6 you could create a control array and then access all of the controls events 'i.e. MouseDown; DoubleClick; click; etc. That way you could code your events in one spot 'Well, you can still do this. It is done through "Handles". 'I have added the handles dynamically as shown below, however, if you put 'the grids on the form in design mode, you can add the grids to the same event 'so that you have one event for all of the grids. 'i.e. 'Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ 'Handles DataGrid1.MouseUp, DataGrid2.MouseUp, DataGrid3.MouseUp, DataGrid4.MouseUp 'MsgBox("You Clicked on grid " & Me.ActiveControl.Name()) 'End Sub 'Hope this helps. Let me know if I was way off. 'Create new DataGrids and add them to the collection. myDataGrids.Add(New DataGrid()) myDataGrids.Add(New DataGrid()) myDataGrids.Add(New DataGrid()) myDataGrids.Add(New DataGrid()) 'Set some defaults for the grids. 'You could shorten this code with a for loop myDataGrids(1).Name = "DataGrid1" myDataGrids(2).Name = "DataGrid2" myDataGrids(3).Name = "DataGrid3" myDataGrids(4).Name = "DataGrid4" myDataGrids(1).Size = New Size(300, 300) myDataGrids(2).Size = New Size(300, 300) myDataGrids(3).Size = New Size(300, 300) myDataGrids(4).Size = New Size(300, 300) myDataGrids(4).Location = New Point(100, 100) myDataGrids(3).Location = New Point(75, 75) myDataGrids(2).Location = New Point(50, 50) myDataGrids(1).Location = New Point(25, 25) 'Add them to the windows form. Me.Controls.Add(myDataGrids(4)) Me.Controls.Add(myDataGrids(3)) Me.Controls.Add(myDataGrids(2)) Me.Controls.Add(myDataGrids(1)) 'Add handlers so that our grids use the myDGMouseUp_MouseUp event. AddHandler CType(myDataGrids(1), DataGrid).MouseUp, AddressOf myDGMouseUp_MouseUp AddHandler CType(myDataGrids(2), DataGrid).MouseUp, AddressOf myDGMouseUp_MouseUp AddHandler CType(myDataGrids(3), DataGrid).MouseUp, AddressOf myDGMouseUp_MouseUp AddHandler CType(myDataGrids(4), DataGrid).MouseUp, AddressOf myDGMouseUp_MouseUp End Sub Private Sub myDGMouseUp_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) MsgBox("You Clicked on grid " & Me.ActiveControl.Name()) End Sub End Class Quote
*Experts* Volte Posted July 28, 2003 *Experts* Posted July 28, 2003 Control arrays are not supported by the Windows Forms Designer. The only way to do it is through code, or many separate controls with one designer (as shown above). Note that the sender parameter of the handler says what control raised the event. So, MsgBox("You clicked on grid " & DirectCast(sender, DataGrid).Name)is a better way (your way won't work properly for events that don't change the focus. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.