josemariosantos
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.Ul traGrid1.A ctiveRow.I ndex).Cell s(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
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.Ul
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
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.U ltraGrid1. ActiveRow. Index).Cel ls(0).Valu e()
"Form1" will refer to the "default instance" of Form1...which is what is loaded at application startup.
With VB.Net 2005 though, if Form1 is your "Startup Object", you can simply use:
Dim c As Integer = Form1.UltraGrid1.Rows(Me.U
"Form1" will refer to the "default instance" of Form1...which is what is loaded at application startup.
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
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()
' <<< from within Form1 >>>
Dim f2 As New Form2(Me)
f2.Show()
ASKER
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.U ltraGrid1. ActiveRow. Index).Cel ls(0).Valu e() ---> Here
MsgBox(c)
End Sub
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.U
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.Ultr aGrid1.Act iveRow.Ind ex).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...
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.Ultr
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...
ASKER
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.Ultr aGrid1.Act iveRow.Ind ex).Cells( 0).Value()
MsgBox(c)
End Sub
Private Sub UltraGrid1_InitializeLayou t(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinG rid.Initia lizeLayout EventArgs) Handles UltraGrid1.InitializeLayou t
With UltraGrid1.DisplayLayout
.Bands(0).Columns("Cod_Uni dade").Val ueList = Me.UltraDropDown1
End With
End Sub
End Class
Public Class Form1
Private Sub TreeView1_AfterSelect(ByVa l sender As Object, ByVal e As System.Windows.Forms.TreeV iewEventAr gs) 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
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.Ultr
MsgBox(c)
End Sub
Private Sub UltraGrid1_InitializeLayou
With UltraGrid1.DisplayLayout
.Bands(0).Columns("Cod_Uni
End With
End Sub
End Class
Public Class Form1
Private Sub TreeView1_AfterSelect(ByVa
Dim F2 As New Form2Me)
Select Case Me.TreeView1.SelectedNode.
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
ASKER
Unidade means Form2 just to be easier for you
ASKER
Thank you anyway
already found the solution
If anyone needs it ask that I will gladly give it
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/
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In your last version, where the FORM was being passed....
This line:
Dim c As Integer = F1.UltraGrid1.Rows(Me.Ultr aGrid1.Act iveRow.Ind ex).Cells( 0).Value()
Should have been:
Dim c As Integer = F1.UltraGrid1.Rows(F1.Ultr aGrid1.Act iveRow.Ind ex).Cells( 0).Value()
Note the second "Me" should have been "F1"...
This line:
Dim c As Integer = F1.UltraGrid1.Rows(Me.Ultr
Should have been:
Dim c As Integer = F1.UltraGrid1.Rows(F1.Ultr
Note the second "Me" should have been "F1"...
ASKER
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
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... ;)
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
modus_operandi
Community Support Moderator
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