Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Calling a Sub from another Form


Reply
 
Thread Tools Display Modes
  #1  
Old 08-05-2004, 09:00 AM
theberzerker69 theberzerker69 is offline
Centurion
 
Join Date: Dec 2003
Posts: 148
Default Calling a Sub from another Form


I have a project with multiple forms. Most of them are displayed from the main form, Form1.

Form2
l
Form5--Form1--Form3
l
Form4

Form1 displays data from an access table. The other forms are used to add/edit data in the datatable. When the other forms are unloaded or closed, I need the datatable on form1 to be refreshed. This is done on Form1 with a sub "QuerySub()." Right now the QuerySub is put in a random control behind a picture box with the code:

Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click QuerySub() End Sub

So right now the Button is clicked from the other forms like so:

Code:
Form1.Button1.PerformClick()

This works, but it isn't clean. Is there a way to execute a Sub on Form1 from Forms 2-5?

Thanks in Advance

Tom

Last edited by theberzerker69; 08-05-2004 at 09:09 AM.
Reply With Quote
  #2  
Old 08-05-2004, 10:36 AM
PWNettle PWNettle is offline
Verbose Coder

Retired Moderator
* Guru *
 
Join Date: Dec 1999
Location: Phoenix, Arizona
Posts: 3,011
Default

The more "oop correct" way to do it would be to make your QuerySub() public, pass a reference to form1 to your other forms whenever you instantiate them, and then call QuerySub() from from the other forms via the reference back to form1.

Code:
' In some other (sub) form: frmFormOneReference.QuerySub()

Another approach would be to have a seperate object that models/represents your data and includes the update functionality and pass around references to the data object instead of having the data functionality tied into your form (which is a display/control element, not a data element, per se).

Paul
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->