Calling A Method In Other Forms

ayozzhero

Well-known member
Joined
Apr 6, 2005
Messages
186
Location
Malaysia
Programming Experience
1-3
How do I call methods in other forms, i.e I am in FormA and I want to refresh FormB from FormA.
 

ritesh_jain1982

Well-known member
Joined
Jan 10, 2005
Messages
53
Programming Experience
1-3
Hi,
U can create new instance of FormB and u can call it's Public methods like.........

'From A Code

.......................
dim objfrmB as new FormB
objfrmB.Refresh()
.......................


I hope this will help u..............

Regards,
Ritesh
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
in FormB make a sub (delcare is a Friend) that accepts the parameters that need to be refreshed

then on FormA call FormB's refresh method and pass the needed info

such as:
VB.NET:
'FormB
Friend Sub RefreshMe(byval Text as String)
  Label1.Text = Text
End Sub

'FormA
Dim frmB as New FormB
frmB.Show
Call frmB.RefreshMe("I feel Refreshed")
I hope that helps
 

ayozzhero

Well-known member
Joined
Apr 6, 2005
Messages
186
Location
Malaysia
Programming Experience
1-3
I tried a few variations of your replies... e.g:

Friend Sub RefreshMe()
Me.Refresh()
End Sub

but it keep generating the error: Object reference not set to an instance of an object.

Well, it happened because I didn't put 'New' while dimming the variable. But I just can't use the new keyword because it will create a new form and will not update the existing one. The scenario is like this:

1. frmDisplay is opened. User click a button to add new data
2. frmAddData is opened. User adds a few data and save it to database
3. Upon Close event of frmAddData, I want frmDisplay (or certain controls on it) to refresh, so new data will displayed.

Sorry for not making it clear before.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
ok, i see what's going on now and here's one way of doing that (there is probably a better way to do this)

add a code module to the project then in the module:
Friend DisplayForm as frmDisplay

in the frmDisplay load event add:
DisplayForm = Me

Friend Sub RefreshMe(parameters needed)
'Refresh the data here
End Sub

in the frmAddData closing event:
DisplayForm.RefreshMe(pass the needed info)

and that should do what you're looking for
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,322
Location
Sydney, Australia
Programming Experience
10+
I'm going to assume that frmAddData is a modal dialog of frmDisplay and say that I would use this:
VB.NET:
If frmAddData.DialogResult = DialogResult.OK Then
	<Read public properties of frmAddData and use as needed>
End If
You can then make whatever data you need available in public properties of frmAddData.
 
Top Bottom