473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Form question (Re-usuable code)

Is there any way of making the following code into a function so that I
don't have to re-use the same code for all my buttons? I've tried to do this
with the code at the bottom of my post but I get a build error saying 'Type
FormToShow is not defined'.

Cheers,
Paul
Private WithEvents Example1Form As frmDragDropFromExplorer
Private WithEvents Example2Form As frmDragDrop

Public Shared Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New frmMain)
End Sub

Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExample1.Click
Try
Example1Form.Show()
Catch
Example1Form = New frmDragDropFromExplorer
Example1Form.Show()
Finally
Example1Form.Focus()
End Try
End Sub

---- Tried this but got the error 'FormToShow is not defined' ---
Private Sub btnExample1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExample1.Click
ShowForm(Example1Form)
End Sub

Private Function ShowForm(ByVal FormToShow As Form)
Try
FormToShow.Show()
Catch
FormToShow = New FormToShow
FormToShow.Show()
Finally
FormToShow.Focus()
End Try
End Function
Nov 21 '05 #1
4 1209
Paul,

Try chainging this line:

Private Function ShowForm(ByVal FormToShow As Form)

To this:

Private Function ShowForm(ByRef FormToShow As Form)

Passing FormToShow "ByRef" -- by reference instead of by value.

Nov 21 '05 #2
Oh, I should have explained this more: even if you know why this would
work better, somone googling for the answer later may not.

When you pass your form "ByVal", you are not actually passing your
form-- you are making a *copy* of the form, and passing that. The
system doesn't recognize the copy, hence you get the error message that
you reported.

Using "ByRef" passes the *address* of your form to the subroutine,
rather than a copy. So the action is preformed on the origional form,
as you wish, rather than on a "copy" that's just haning around
disconnected from anything.

Hope this helps!

Nov 21 '05 #3

Linda wrote:
Oh, I should have explained this more: even if you know why this would work better, somone googling for the answer later may not.

When you pass your form "ByVal", you are not actually passing your
form-- you are making a *copy* of the form, and passing that. The
system doesn't recognize the copy, hence you get the error message that you reported.

Using "ByRef" passes the *address* of your form to the subroutine,
rather than a copy. So the action is preformed on the origional form,
as you wish, rather than on a "copy" that's just haning around
disconnected from anything.

Hope this helps!


That is actually incorrect. The discrepancy comes in how you think you
are referencing your form.

Object references (which is basically everything except numeric types
and other certain intrinsic types, NOT including strings) are variables
that represent pointers to locations in memory. When you have a
variable defined as, say, a form (or any other object type), that
variable doesn't represent the object itself, but rather that pointer
that points to the location in memory of the object. That is what your
variable is.

Now what does this mean? When you pass an object reference to a
function ByVal, then EVERYTHING PERFORMED on that object reference
EXCEPT FOR ASSIGNMENTS is performed on the actual object. The only
difference between ByVal and ByRef is the behavior of assignments.
ByVal parameters do not affect the variable in the calling method,
ByRef parameters do.

That clear as mud?

Nov 21 '05 #4
As for the original question, this isn't really possible without diving
into reflection (you'd have to pass the type in using the GetType()
keyword, then find the declaring assembly and instantiate it, which is
a costly operation), and IMHO this is a bit of a violation of good
programming practices.

Now, that said, this sort of thing will be a little easier and less
costly in VS2005 using generics. However, you are writing this function
simply because you do not want to instantiate a form?

Also, you are simply catching EVERY exception, then reattempting the
same operation, which is bad. You do not know that the exception you
caught was a NullReferenceException, it could be something else that
got thrown upon creating the control's handle (ie in the Load event of
that form).

If you wish to refer to your forms in VB6 style (ie FormTypeName.show),
then I would recommend creating them as singletons. To do that, just go
into the form's Windows Designer Generated Code and change the
constructor (the New() method) to be private, then up at the top of the
class declaration, just after the Inherits System.Windows.Forms.Form
(well, this can actually go anywhere except inside a method, but this
is where I usually put it for clarity's sake) and type the following:

Public ReadOnly Instance as YourFormTypeNameHere = New
YourFormTypeNameHere

Now, when you want to refer to this single instance of your form, you
do YourFormTypeNameHere.Instance... and do whatever you wish, ie

YourFormTypeNameHere.Instance.Show()

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ojvm | last post by:
ok. thanks again for the time spend reading this. this code adds 2 controls in html form but it places in top of the form. i want this control1 control2 control1 control2 control1 ...
1
by: Jason | last post by:
I have succussfully ran the code below from a module. The code runs SQL update statements from a memo field in a table, called tblSQL. The field ! (Line 11) allows you to filter out the records in...
2
by: enrio | last post by:
I need to process the source code associated with the forms outside access, and import the changes back to Access. I find that I can export the source code of a form, but when I subsequently...
6
by: John Ortt | last post by:
Hi there everyone, I have a part info form which has a faded image of our company logo as a background. I want to replace the faded image with a bright red warning image on items which have run...
11
by: Jozef | last post by:
I have some old code that I use from the Access 95 Developers handbook. The code works very well, with the exception that it doesn't seem to recognize wide screens, and sizes tab controls so that...
16
by: NathyZif | last post by:
I know I know, why would I want to submit the form from server-side code, if the only way to get to the server-side code was BY submitting the form. But that's just it, when the code-behind...
1
by: lakshya | last post by:
how to disable java script form vb code which is written on masterpage
2
by: Jaye | last post by:
Hi. I'm having some trouble with the code I'm using to validate a form. The form is used to query a database and users can enter multiple search criteria separated by commas. Since search criteria...
3
by: PrabodhanP | last post by:
I hv following javascript form validation code works in IE but not in Mozilla-Firefox ...please suggest <script type="text/javascript"> function IsNumeric(strString) // check for valid...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.