473,327 Members | 2,074 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,327 software developers and data experts.

Calling a public sub in another form having the name of the form as a string

Hi,
I wonder if anyone can help me out.

I'm building a vb.net application that has a form with a panel that
contains several other sub forms (as a collection of controls). What
I'm wanting to do is call a generically named public sub in the
top-most sub form in the panel.

I have the name of the top-most form as a string (eg. g_TopForm =
"frmCustomers") and I can reference the form with:

Me.pnlMainWindow.Controls.Item(FormRef)

where the FormRef integer is derived from the function call:

FormRef = FormItemNo(g_TopForm)

and the FormItemNo function is defined as:

Private Function FormItemNo(ByVal formname As String) As Integer
Dim itemref As Integer

Try
'Return impossible number if the form hasn't already been
opened
FormItemNo = 99
For itemref = 0 To Me.pnlMainWindow.Controls.Count - 1
If Me.pnlMainWindow.Controls.Item(itemref).Name =
formname Then FormItemNo = itemref
Next

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Function

So, what I'd like to be able to do now is somehow derive a variable
(say subfrm) of type 'frmCustomers' and call:

subfrm.NewRec()

where NewRec() is a public sub in the frmCustomers form class.

Am I barking up completely the wrong tree with this approach or does
anyone have a simple solution to this? I'm not very adept in .net
programming yet (as you may have gathered?!)

Any help is very much appreciated.
Paul

Jun 28 '06 #1
4 2201
On 28 Jun 2006 06:26:10 -0700, "Bugs" <pa***************@hotmail.com>
wrote:
Hi,
I wonder if anyone can help me out.

I'm building a vb.net application that has a form with a panel that
contains several other sub forms (as a collection of controls). What
I'm wanting to do is call a generically named public sub in the
top-most sub form in the panel.

I have the name of the top-most form as a string (eg. g_TopForm =
"frmCustomers") and I can reference the form with:

Me.pnlMainWindow.Controls.Item(FormRef)

where the FormRef integer is derived from the function call:

FormRef = FormItemNo(g_TopForm)

and the FormItemNo function is defined as:

Private Function FormItemNo(ByVal formname As String) As Integer
Dim itemref As Integer

Try
'Return impossible number if the form hasn't already been
opened
FormItemNo = 99
For itemref = 0 To Me.pnlMainWindow.Controls.Count - 1
If Me.pnlMainWindow.Controls.Item(itemref).Name =
formname Then FormItemNo = itemref
Next

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Function

So, what I'd like to be able to do now is somehow derive a variable
(say subfrm) of type 'frmCustomers' and call:

subfrm.NewRec()

where NewRec() is a public sub in the frmCustomers form class.

Am I barking up completely the wrong tree with this approach or does
anyone have a simple solution to this? I'm not very adept in .net
programming yet (as you may have gathered?!)

Any help is very much appreciated.
Paul
When you're creating your sub forms can you not store that top most one
in a member variable?

E.g.

=====================

Public Class MainWindow
Private customers_form As CustomersForm

Public Sub New()
customer_form = new CustomersForm()
End Sub

Public ReadOnly Property TopForm As CustomersForm
Get
return customers_form
End Get
End Property
End Class

=====================

Then you can just call, pnlMainWindow.TopForm.NewRec()
FormItemNo = 99 This would probaly be better as -1. Most zero based arrays use this to
mean 'not found' as it's an index that definitely will not ever be used
as the array begins at zero.
On a side note: Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
End Try

You really shouldn't catch all exceptions, as it could be something
critical like, OutOfMemory. If you don't know what the error is then
don't handle it.

If you want to keep the message box there though you could change it to:

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)

'Throw with no arguments rethrows the current exception
Throw
End Try
Jun 28 '06 #2

Chris Chilvers wrote:
On 28 Jun 2006 06:26:10 -0700, "Bugs" <pa***************@hotmail.com>
wrote:
Hi,
I wonder if anyone can help me out.

I'm building a vb.net application that has a form with a panel that
contains several other sub forms (as a collection of controls). What
I'm wanting to do is call a generically named public sub in the
top-most sub form in the panel.

I have the name of the top-most form as a string (eg. g_TopForm =
"frmCustomers") and I can reference the form with:

Me.pnlMainWindow.Controls.Item(FormRef)

where the FormRef integer is derived from the function call:

FormRef = FormItemNo(g_TopForm)

and the FormItemNo function is defined as:

Private Function FormItemNo(ByVal formname As String) As Integer
Dim itemref As Integer

Try
'Return impossible number if the form hasn't already been
opened
FormItemNo = 99
For itemref = 0 To Me.pnlMainWindow.Controls.Count - 1
If Me.pnlMainWindow.Controls.Item(itemref).Name =
formname Then FormItemNo = itemref
Next

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Function

So, what I'd like to be able to do now is somehow derive a variable
(say subfrm) of type 'frmCustomers' and call:

subfrm.NewRec()

where NewRec() is a public sub in the frmCustomers form class.

Am I barking up completely the wrong tree with this approach or does
anyone have a simple solution to this? I'm not very adept in .net
programming yet (as you may have gathered?!)

Any help is very much appreciated.
Paul


When you're creating your sub forms can you not store that top most one
in a member variable?

E.g.

=====================

Public Class MainWindow
Private customers_form As CustomersForm

Public Sub New()
customer_form = new CustomersForm()
End Sub

Public ReadOnly Property TopForm As CustomersForm
Get
return customers_form
End Get
End Property
End Class

=====================

Then you can just call, pnlMainWindow.TopForm.NewRec()
FormItemNo = 99

This would probaly be better as -1. Most zero based arrays use this to
mean 'not found' as it's an index that definitely will not ever be used
as the array begins at zero.
On a side note:
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
End Try

You really shouldn't catch all exceptions, as it could be something
critical like, OutOfMemory. If you don't know what the error is then
don't handle it.

If you want to keep the message box there though you could change it to:

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)

'Throw with no arguments rethrows the current exception
Throw
End Try


Hi Chris,
Huge thanks for your reply! I really appreciate the good advice. I've
taken into account everything you've said and I'm finding the more you
look into this stuff, the more you realise there is to learn!

I can see how your approach will help but how would this work if I
don't know which type of form class my topmost subform is
(unfortunately each of the subforms are of a different form class)?

Since your post, I've found a nifty function - CallByName and have
found this to do the trick in the meantime (albeit not perhaps such a
neat way as you suggested). The call to the subform procedure now looks
like this in the main window code:

CallByName(Me.pnlMainWindow.Controls.Item(FormRef) , "NewRec",
CallType.Method)

where FormRef is still derived as originally described in my first post
(by using a string variable to hold the topmost form name).

Whilst this is doing the job for now (and just avoiding knowing the
object's methods at design time), I'd like to ultimately know how to do
this 'properly' if there is a way!

Many thanks for your help!
Paul

Jun 28 '06 #3
On 28 Jun 2006 15:29:49 -0700, "Bugs" <pa***************@hotmail.com>
wrote:

Chris Chilvers wrote:
On 28 Jun 2006 06:26:10 -0700, "Bugs" <pa***************@hotmail.com>
wrote:
>Hi,
>I wonder if anyone can help me out.
>
>I'm building a vb.net application that has a form with a panel that
>contains several other sub forms (as a collection of controls). What
>I'm wanting to do is call a generically named public sub in the
>top-most sub form in the panel.
>
>I have the name of the top-most form as a string (eg. g_TopForm =
>"frmCustomers") and I can reference the form with:
>
> Me.pnlMainWindow.Controls.Item(FormRef)
>
>where the FormRef integer is derived from the function call:
>
> FormRef = FormItemNo(g_TopForm)
>
>and the FormItemNo function is defined as:
>
> Private Function FormItemNo(ByVal formname As String) As Integer
> Dim itemref As Integer
>
> Try
> 'Return impossible number if the form hasn't already been
>opened
> FormItemNo = 99
> For itemref = 0 To Me.pnlMainWindow.Controls.Count - 1
> If Me.pnlMainWindow.Controls.Item(itemref).Name =
>formname Then FormItemNo = itemref
> Next
>
> Catch exp As Exception
> MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
> End Try
> End Function
>
>So, what I'd like to be able to do now is somehow derive a variable
>(say subfrm) of type 'frmCustomers' and call:
>
> subfrm.NewRec()
>
>where NewRec() is a public sub in the frmCustomers form class.
>
>Am I barking up completely the wrong tree with this approach or does
>anyone have a simple solution to this? I'm not very adept in .net
>programming yet (as you may have gathered?!)
>
>Any help is very much appreciated.
>Paul


When you're creating your sub forms can you not store that top most one
in a member variable?

E.g.

=====================

Public Class MainWindow
Private customers_form As CustomersForm

Public Sub New()
customer_form = new CustomersForm()
End Sub

Public ReadOnly Property TopForm As CustomersForm
Get
return customers_form
End Get
End Property
End Class

=====================

Then you can just call, pnlMainWindow.TopForm.NewRec()
> FormItemNo = 99

This would probaly be better as -1. Most zero based arrays use this to
mean 'not found' as it's an index that definitely will not ever be used
as the array begins at zero.
On a side note:
> Catch exp As Exception
> MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
> End Try

You really shouldn't catch all exceptions, as it could be something
critical like, OutOfMemory. If you don't know what the error is then
don't handle it.

If you want to keep the message box there though you could change it to:

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)

'Throw with no arguments rethrows the current exception
Throw
End Try


Hi Chris,
Huge thanks for your reply! I really appreciate the good advice. I've
taken into account everything you've said and I'm finding the more you
look into this stuff, the more you realise there is to learn!

I can see how your approach will help but how would this work if I
don't know which type of form class my topmost subform is
(unfortunately each of the subforms are of a different form class)?

Since your post, I've found a nifty function - CallByName and have
found this to do the trick in the meantime (albeit not perhaps such a
neat way as you suggested). The call to the subform procedure now looks
like this in the main window code:

CallByName(Me.pnlMainWindow.Controls.Item(FormRef ), "NewRec",
CallType.Method)

where FormRef is still derived as originally described in my first post
(by using a string variable to hold the topmost form name).

Whilst this is doing the job for now (and just avoiding knowing the
object's methods at design time), I'd like to ultimately know how to do
this 'properly' if there is a way!

Many thanks for your help!
Paul


Ah, another good solution is to use an interface. Then each of your sub
forms must implement this interface. In doing so they garuntee that they
will have the methods / properties you specify.

Thus:
=====
Public Interface ISubForm
Public Sub NewRec()
End Interface
=====

Notice how the interface only says this method will exist and not how it
will function.
Now in the class:
=====
Public Class CustomersForm
Implements ISubForm

Public Sub NewRec() Implements ISubForm.NewRec
'Run you code here for creating a new customer record
End Sub
End Class

Public Class SomeOtherForm
Implements ISubForm

Public Sub NewRec() Implements ISubForm.NewRec
'Run you code here for creating a new other record
End Sub
End Class
=====

Both of these class can be considered to be of type ISubForm, thus:
=====
Dim some_form As ISubForm

some_form = New CustomersForm()
some_form.NewRec()

some_form = New SomeOtherForm()
some_form.NewRec()
=====

So now you can change the property to be of type ISubForm instead of
CustomersForm.

Have a look in the help about, when to use interfaces (interfaces, vs.
classes), they can be very helpful as a class can implement more than
one interface as well.
Jun 28 '06 #4

Chris Chilvers wrote:
On 28 Jun 2006 15:29:49 -0700, "Bugs" <pa***************@hotmail.com>
wrote:

Chris Chilvers wrote:
On 28 Jun 2006 06:26:10 -0700, "Bugs" <pa***************@hotmail.com>
wrote:

>Hi,
>I wonder if anyone can help me out.
>
>I'm building a vb.net application that has a form with a panel that
>contains several other sub forms (as a collection of controls). What
>I'm wanting to do is call a generically named public sub in the
>top-most sub form in the panel.
>
>I have the name of the top-most form as a string (eg. g_TopForm =
>"frmCustomers") and I can reference the form with:
>
> Me.pnlMainWindow.Controls.Item(FormRef)
>
>where the FormRef integer is derived from the function call:
>
> FormRef = FormItemNo(g_TopForm)
>
>and the FormItemNo function is defined as:
>
> Private Function FormItemNo(ByVal formname As String) As Integer
> Dim itemref As Integer
>
> Try
> 'Return impossible number if the form hasn't already been
>opened
> FormItemNo = 99
> For itemref = 0 To Me.pnlMainWindow.Controls.Count - 1
> If Me.pnlMainWindow.Controls.Item(itemref).Name =
>formname Then FormItemNo = itemref
> Next
>
> Catch exp As Exception
> MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
> End Try
> End Function
>
>So, what I'd like to be able to do now is somehow derive a variable
>(say subfrm) of type 'frmCustomers' and call:
>
> subfrm.NewRec()
>
>where NewRec() is a public sub in the frmCustomers form class.
>
>Am I barking up completely the wrong tree with this approach or does
>anyone have a simple solution to this? I'm not very adept in .net
>programming yet (as you may have gathered?!)
>
>Any help is very much appreciated.
>Paul

When you're creating your sub forms can you not store that top most one
in a member variable?

E.g.

=====================

Public Class MainWindow
Private customers_form As CustomersForm

Public Sub New()
customer_form = new CustomersForm()
End Sub

Public ReadOnly Property TopForm As CustomersForm
Get
return customers_form
End Get
End Property
End Class

=====================

Then you can just call, pnlMainWindow.TopForm.NewRec()

> FormItemNo = 99
This would probaly be better as -1. Most zero based arrays use this to
mean 'not found' as it's an index that definitely will not ever be used
as the array begins at zero.
On a side note:
> Catch exp As Exception
> MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
> End Try
You really shouldn't catch all exceptions, as it could be something
critical like, OutOfMemory. If you don't know what the error is then
don't handle it.

If you want to keep the message box there though you could change it to:

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)

'Throw with no arguments rethrows the current exception
Throw
End Try


Hi Chris,
Huge thanks for your reply! I really appreciate the good advice. I've
taken into account everything you've said and I'm finding the more you
look into this stuff, the more you realise there is to learn!

I can see how your approach will help but how would this work if I
don't know which type of form class my topmost subform is
(unfortunately each of the subforms are of a different form class)?

Since your post, I've found a nifty function - CallByName and have
found this to do the trick in the meantime (albeit not perhaps such a
neat way as you suggested). The call to the subform procedure now looks
like this in the main window code:

CallByName(Me.pnlMainWindow.Controls.Item(FormRef ), "NewRec",
CallType.Method)

where FormRef is still derived as originally described in my first post
(by using a string variable to hold the topmost form name).

Whilst this is doing the job for now (and just avoiding knowing the
object's methods at design time), I'd like to ultimately know how to do
this 'properly' if there is a way!

Many thanks for your help!
Paul


Ah, another good solution is to use an interface. Then each of your sub
forms must implement this interface. In doing so they garuntee that they
will have the methods / properties you specify.

Thus:
=====
Public Interface ISubForm
Public Sub NewRec()
End Interface
=====

Notice how the interface only says this method will exist and not how it
will function.
Now in the class:
=====
Public Class CustomersForm
Implements ISubForm

Public Sub NewRec() Implements ISubForm.NewRec
'Run you code here for creating a new customer record
End Sub
End Class

Public Class SomeOtherForm
Implements ISubForm

Public Sub NewRec() Implements ISubForm.NewRec
'Run you code here for creating a new other record
End Sub
End Class
=====

Both of these class can be considered to be of type ISubForm, thus:
=====
Dim some_form As ISubForm

some_form = New CustomersForm()
some_form.NewRec()

some_form = New SomeOtherForm()
some_form.NewRec()
=====

So now you can change the property to be of type ISubForm instead of
CustomersForm.

Have a look in the help about, when to use interfaces (interfaces, vs.
classes), they can be very helpful as a class can implement more than
one interface as well.


Chris,
Thanks very much for that. That works a treat! (and another new one for
me!) I appreciate your time in helping me out.

Just as a matter of interest, I'm now storing the topmost subform in
the Tag property of the panel so calling the NewRec function of the
interface is done with:

Dim subfrm As ISubForm
subfrm = CType(Me.pnlMainWindow.Tag, ISubForm)
subfrm.NewRec()

I'm getting there I hope! Thanks again.
Paul

Jun 29 '06 #5

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

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
10
by: headware | last post by:
I know that you can call the method of one from from inside another form by doing something like this Forms("MyForm").MyFunction(12, 34) However, you have to know that MyForm has a function...
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
0
by: Mike Collins | last post by:
I apologize for using this newsgroup for what seems like a VB6 question, but I did not see a newsgroup for VB6. I also think I may not have the C# code setup correctly for calling from VB6. If...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
7
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type...
7
by: WhiskRomeo | last post by:
I have a WIN .NET application that calls a web service to retrieve data. I deployed the application to a server and configured the webservice. The webservice and SQL Server 2000 database are on...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.