Using forms as varibles

Fion

Member
Joined
Mar 16, 2006
Messages
9
Programming Experience
Beginner
Hi all, I have hit a small snag in a program I am building, and I was hoping someone here could help.
I have a MDI type program going, where then you can open assorted child forms for different functionality. Pretty standard really. My issue is this:
I have a child form open, and I double-click on a datagridview, to open a new form with detailed information about the item in the datagridview that was selected.
I want to set up the second form (the detail form) to take the information from the form that called it (the source form). I would like to have a variable in the second form that represents the source form, so that I could use the details form with multipule sources.
I have gotten it so that when the source form calls the detail form, it also passes it'sself to an internal variable in the details form. The problem is, what I go to use the datagridview information with the variable, it dosen't like that, becuase the variable does not become the form until runtime.
Here is the calling code from the source form:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] MachineDataGridView_CellContentDoubleClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], _[/SIZE]
 [SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.DataGridViewCellEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] MachineDataGridView.CellContentDoubleClick
   Details.MdiParent = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ParentForm
   Main.m_ChildFormNumber += 1
[/SIZE][SIZE=2]  Details.OrigF = [/SIZE][SIZE=2][COLOR=#0000ff]Me
[/COLOR][/SIZE][SIZE=2]  Details.Show()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

And here is my code from the details form:
VB.NET:
[SIZE=2][COLOR=#0000ff]
Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Details
[/SIZE][SIZE=2][COLOR=#0000ff]   Public[/COLOR][/SIZE][SIZE=2] OrigF [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Form
[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Details_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2]       System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] OrigF.Name = [/SIZE][SIZE=2][COLOR=#800000]"View Machines"[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] MessageBox.Show(OrigF.MachineDataGridView.SelectedCells.Item(0).Value)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]

See, as above I get the error in the OrigF.MachineDataGridView.SelectedCells.Item(0).Value line, becuase at design time OrigF does not have a datagridview, it is a null variable until runtime. Can anyone suggest a way to make this work, or a better way to go about it?

Thanks!
 
Last edited by a moderator:
Back
Top