We help IT Professionals succeed at work.

Share data between forms vb .net 2005

josemariosantos
on
226 Views
Last Modified: 2008-01-09
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
Comment
Watch Question

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()
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.

Author

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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
For Solar_Flare's submission, you need to use:

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

Author

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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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...

Author

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

Author

Commented:
Unidade means Form2 just to be easier for you

Author

Commented:
Thank you anyway

already found the solution

If anyone needs it ask that I will gladly give it
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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/
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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"...

Author

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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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...  ;)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
I'm just glad you got a working solution.  =)
CERTIFIED EXPERT

Commented:
Closed, 500 points refunded.
modus_operandi
Community Support Moderator
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.