Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have two forms, the first form has data entry and the second form has only monthview conrol. On the first form I have two text boxes and two command buttons associated with these text boxes. The first text box is Date Dispute Received and the second one is Date of Transaction. Besides these text boxes I have placed command buttons and when the user clicks on the command button, the first form will be invisible and the calnedar form pops up. When the user selects a date, the date would be sent back to the first form in the respective text box. I am stuced here. My code goes like this.

In First form
CmdTxnDt_click:
me.visible=False
Dim X as new frmCal
x.show

In Second Form
monthview_click
Form1.txtTxn.Text=montview1.date

While trying to type the above line, I am not able to get the objects on the first form. If I instantiate the form1 in the second form a new copy of form1 is created. But I want data to be placed on the existing Form1.

Could someone help me out on this?

Thanks in Advance.

Regards,
Krishna
Posted
Updated 7-Jul-11 21:25pm
v3

This is a popular question about form collaboration. The most robust method is implementation of appropriate interface in form class.

Please see my past answer here: How to copy all the items between listboxes in two forms[^].
See also other suggestions and discussion.

—SA
 
Share this answer
 
Comments
[no name] 8-Jul-11 3:26am    
Good Answer. My 5 .
Sergey Alexandrovich Kryukov 8-Jul-11 3:38am    
Thank you, Ramalinga.
--SA
[no name] 8-Jul-11 8:41am    
It's my Pleasure.
Tarun.K.S 8-Jul-11 4:59am    
Good link with excellent answers. 5+
Sergey Alexandrovich Kryukov 8-Jul-11 5:02am    
Thank you, Tarun.
--SA
The best way to do this is to create delegates between the forms, so that when an event happens in form1, it can call a method in form2, passing the data needed to update itself. Solution 1 shows a way to do this, although the exact solution offered is horrible ( you should not expose controls on your form, only methods that allow you to make the changes you want to allow ), but it only works from the parent to the child. Delegates can be set up to work both ways. I'm not sure how having an interface implimented by the form class helps, as you still need to get the instance, but SA generally gives good advice, so I'd suggest reading his link, also.

A Beginner's Guide to Delegates[^]
 
Share this answer
 
Comments
[no name] 8-Jul-11 3:27am    
Good Call. My 5.
Why don't you handle monthview's click event like this :
VB
'in form1
Private Sub CmdTxnDt_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdTxnDt.Click
Me.Visible = False
Dim X as new frmCal
AddHandler frmCal.monthview.Click, AddressOf frm2_Click
x.show
End Sub

Private Sub frm2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Form1.txtTxn.Text=sender.date
End Sub
 
Share this answer
 
create object for form and use variables in other form using objects

eg: dim obj as new form1
obj.variable1=data
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900