• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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
0
josemariosantos
Asked:
josemariosantos
1 Solution
 
Solar_FlareCommented:
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()
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
josemariosantosAuthor Commented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For Solar_Flare's submission, you need to use:

    ' <<< from within Form1 >>>
    Dim f2 As New Form2(Me)
    f2.Show()
0
 
josemariosantosAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
 
josemariosantosAuthor Commented:
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
0
 
josemariosantosAuthor Commented:
Unidade means Form2 just to be easier for you
0
 
josemariosantosAuthor Commented:
Thank you anyway

already found the solution

If anyone needs it ask that I will gladly give it
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
See:
http://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:
http://www.experts-exchange.com/Community_Support/General/
0
 
josemariosantosAuthor Commented:
In the form1

Private Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim parentvalue As Integer = Me.UltraGrid1.Rows(Me.UltraGrid1.ActiveRow.Index).Cells(0).Value

Dim Form2 As Form2
Form2 = New Form2(parentvalue)
Form2.ShowDialog()
End Sub
End Class

in the Form2

Public Class Form2
Dim value as integer

Public Sub New(ByVal parentvalue As Integer)
        MyBase.New()


        InitializeComponent()

        v = parentvalue
    End Sub

    Public WriteOnly Property v() As Integer
        Set(ByVal parentvalue As Integer)
            value = parentvalue
        End Set
    End Property

Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        MsgBox(value)
    End Sub

End Class
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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"...
0
 
josemariosantosAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...  ;)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm just glad you got a working solution.  =)
0
 
modus_operandiCommented:
Closed, 500 points refunded.
modus_operandi
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now