Link to home
Start Free TrialLog in
Avatar of josemariosantos
josemariosantosFlag for Portugal

asked on

Share data between forms vb .net 2005

Hy,
I have 2 forms (form1 and form2) at some point in form1 I press a node on a Treeview and it opens form2 and I need to access some that that is on a grid in the form1 but I can´t

I'm trying this

Dim c As Integer
Dim form As New Form1
c = form.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value()

but it gives me this warning "Object reference not set to an instance of an object."

What shall I do? I'm using VB .NET 2005

Thank you
Avatar of Solar_Flare
Solar_Flare

because you are instantiating a new instance of form1 it won't have the selected row etc - it is not the same version of Form1 that was onscreen before.

you need to pass a reference to Form1 when you create form2 so that form2 can access the instance of Form1 that called it.

eg

public class Form2
   private myform1 as form1

   public sub New(parentForm as Form1)

      InitializeComponent()

      myform1 = parentForm 'store the ref to form1 in the private variable

   end sub

then you can access the activerow with:
myform1.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value()
Avatar of Mike Tomlinson
Solar Flare has given you a great generic answer which should work for all cases (and is good coding style as well!).

With VB.Net 2005 though, if Form1 is your "Startup Object", you can simply use:

   Dim c As Integer = Form1.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value()

"Form1" will refer to the "default instance" of Form1...which is what is loaded at application startup.
Avatar of josemariosantos

ASKER

Solar_Flare

on the Form1

Dim Form2 as new Form2 ---> gives me this error "Argument not specified for parameter 'parentForm' of 'Public Sub New(parentForm As Form1)'"

Form2.Show() ----> I also tried "Form2.Show(Me)"

on the Form2

Public Class Form2
    Private Form1 As Form1
    Public Sub New(ByVal parentForm As Form1)

        InitializeComponent()

        Form1 = parentForm
    End Sub
End Class

No errors here


Idle_Mind

for now Form1 is my "Startup Object" but eventually I'm going to use an MDIParent
For Solar_Flare's submission, you need to use:

    ' <<< from within Form1 >>>
    Dim f2 As New Form2(Me)
    f2.Show()
Thank you Idle_Mind

but I'm getting the same warning that I mentioned in the 1st post


Private Sub Form2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
        Dim c As Integer = Form1.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value() ---> Here
        MsgBox(c)
    End Sub
It's a bad idea to give a variable the same name as its data type....

From within Form1:

    Dim f2 As New Form2(Me)
    f2.Show()

For Form2:

Public Class Form2
    Private f1 As Form1

    Public Sub New(ByVal parentForm As Form1)
        InitializeComponent()
        f1 = parentForm
    End Sub

    Private Sub Form2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
        Dim c As Integer = f1.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value()
        MsgBox(c)
    End Sub

End Class


Does that make a difference?  If not, I think we need to see more complete code posting to spot the mistake...
Same error


Public Class Form2
    Private F1 As Form1
    Public Sub New(ByVal parentForm As Form1)

        InitializeComponent()

        F1 = parentForm
    End Sub

Private Sub Unidade_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
        Dim c As Integer = F1.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value()
        MsgBox(c)
    End Sub

Private Sub UltraGrid1_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles UltraGrid1.InitializeLayout
        With UltraGrid1.DisplayLayout
            .Bands(0).Columns("Cod_Unidade").ValueList = Me.UltraDropDown1
        End With
    End Sub
End Class

Public Class Form1
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim F2 As New Form2Me)
        Select Case Me.TreeView1.SelectedNode.Name
                  Case "Node8"
                F2.Show()
        End Select
End Class


Also when I do "F2.Show()" the Form2 should open over Form1 but it opens under form1
Unidade means Form2 just to be easier for you
Thank you anyway

already found the solution

If anyone needs it ask that I will gladly give it
See:
https://www.experts-exchange.com/help.jsp#hi70

I answered my question myself. What do I do?
Post a request in the Community Support Zone asking for a refund, and asking the Moderators to close the question; be sure to post the URL to your question. You will be required to post your solution in your original question. A Moderator will post a notice of your request which will give the participants four days to object to the refund. Note that if it resembles one of the suggested comments, the likelihood is that your request will not be granted, but rather, the points will be awarded to the Expert who makes the suggestion.

Here is the Community Support Zone:
https://www.experts-exchange.com/Community_Support/General/
ASKER CERTIFIED SOLUTION
Avatar of josemariosantos
josemariosantos
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In your last version, where the FORM was being passed....

This line:

    Dim c As Integer = F1.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value()

Should have been:

    Dim c As Integer = F1.UltraGrid1.Rows(F1.UltraGrid1.ActiveRow.Index).Cells(0).Value()

Note the second "Me" should have been "F1"...
That would probably work, did you test it?

because I already started using my solution and can't try yours.

if you can test so I give you the points
No...I can't test it as I don't have any idea how your app is laid out, and I don't have an UltraGrid, and I don't have your data, etc...  =)

But I'm fairly certain that is what the problem was.  It's no big deal though.  Feel free to close the question and refund your points.  I've got points coming out my ears if hadn't noticed...  ;)
I'm just glad you got a working solution.  =)
Closed, 500 points refunded.
modus_operandi
Community Support Moderator