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

Opening a specific form with a string

Hi,
I am currently able to open forms given a string variable using the
following code:
Dim strForm As String = "Form1"
Dim theForm As Form
theForm = AppDomain.CurrentDomain.CreateInstanceAndUnwrap _
(Me.GetType.Assembly.GetName.Name, System.String.Concat
_
(Me.GetType.Assembly.GetName.Name, ".", strForm))

theForm.Show()

My problem is that I want to also be able to open forms that have
constructors that require a parameter eg. Form1(217).
When I try changing the strForm variable to "Form1(217)", i get an
error because it appears to be looking for a form actually called
Form1(217).

Any help is greatly appreciated, cheers.
Paul

Jan 9 '07 #1
8 2248
On 2007-01-09, Paul Craig <pj*****@gmail.comwrote:
Hi,
I am currently able to open forms given a string variable using the
following code:
Dim strForm As String = "Form1"
Dim theForm As Form
theForm = AppDomain.CurrentDomain.CreateInstanceAndUnwrap _
(Me.GetType.Assembly.GetName.Name, System.String.Concat
_
(Me.GetType.Assembly.GetName.Name, ".", strForm))

theForm.Show()

My problem is that I want to also be able to open forms that have
constructors that require a parameter eg. Form1(217).
When I try changing the strForm variable to "Form1(217)", i get an
error because it appears to be looking for a form actually called
Form1(217).

Any help is greatly appreciated, cheers.
Paul
Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Reflection

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.ShowForm("Form2")
End Sub

Private Sub ShowForm(ByVal formType As String)
Dim t As Type = _
Type.GetType(Assembly.GetExecutingAssembly().GetNa me().Name & "." & formType)
Dim f As Form = _
DirectCast(Activator.CreateInstance(t, New Object() {1}), Form)
f.ShowDialog()
End Sub
End Class

--
Tom Shelton
Jan 9 '07 #2
Tom,

I am always curious what people want to achieve with this kind of code.
(Beside to make it unmaintanable). You have to know the name of the form, so
there are plenty of more efficient methods in my idea.

Do you have an idea?

Cor

"Tom Shelton" <to*********@comcastXXXXXXX.netschreef in bericht
news:lp******************************@comcast.com. ..
On 2007-01-09, Paul Craig <pj*****@gmail.comwrote:
>Hi,
I am currently able to open forms given a string variable using the
following code:
Dim strForm As String = "Form1"
Dim theForm As Form
theForm = AppDomain.CurrentDomain.CreateInstanceAndUnwrap _
(Me.GetType.Assembly.GetName.Name, System.String.Concat
_
(Me.GetType.Assembly.GetName.Name, ".", strForm))

theForm.Show()

My problem is that I want to also be able to open forms that have
constructors that require a parameter eg. Form1(217).
When I try changing the strForm variable to "Form1(217)", i get an
error because it appears to be looking for a form actually called
Form1(217).

Any help is greatly appreciated, cheers.
Paul

Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Reflection

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.ShowForm("Form2")
End Sub

Private Sub ShowForm(ByVal formType As String)
Dim t As Type = _
Type.GetType(Assembly.GetExecutingAssembly().GetNa me().Name & "." &
formType)
Dim f As Form = _
DirectCast(Activator.CreateInstance(t, New Object() {1}), Form)
f.ShowDialog()
End Sub
End Class

--
Tom Shelton

Jan 9 '07 #3
Thanks for your replies Cor and Tom. Tom Im not sure if you read my
whole message, but I need to be able to open a form up with a string
variable and also set its constructors parameters with that same string
eg. "Form1("aParamater"). From what I could see from your example, it
just appears to be a more long winded process to what I already have in
my initial post, but let me know if I missed something.

Cor, not sure if you were actually meaning to ask me rather than Tom
the reasoning behind the need for this code. The reason is because I
have a lot of forms, and I am wanting them all to be handled the same
way with various properties and display locations applied to all these
forms. I am using inheritance but it doesnt cover everything I need,
therefore I am wanting to pass the form name and any of its parameters
as 1 string to this method which then opens the form. I can get my
method I previously posted to open the form if I just pass it the form
name but not if I also pass the parameters.

Paul

Cor Ligthert [MVP] wrote:
Tom,

I am always curious what people want to achieve with this kind of code.
(Beside to make it unmaintanable). You have to know the name of the form, so
there are plenty of more efficient methods in my idea.

Do you have an idea?

Cor

"Tom Shelton" <to*********@comcastXXXXXXX.netschreef in bericht
news:lp******************************@comcast.com. ..
On 2007-01-09, Paul Craig <pj*****@gmail.comwrote:
Hi,
I am currently able to open forms given a string variable using the
following code:
Dim strForm As String = "Form1"
Dim theForm As Form
theForm = AppDomain.CurrentDomain.CreateInstanceAndUnwrap _
(Me.GetType.Assembly.GetName.Name, System.String.Concat
_
(Me.GetType.Assembly.GetName.Name, ".", strForm))

theForm.Show()

My problem is that I want to also be able to open forms that have
constructors that require a parameter eg. Form1(217).
When I try changing the strForm variable to "Form1(217)", i get an
error because it appears to be looking for a form actually called
Form1(217).

Any help is greatly appreciated, cheers.
Paul
Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Reflection

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.ShowForm("Form2")
End Sub

Private Sub ShowForm(ByVal formType As String)
Dim t As Type = _
Type.GetType(Assembly.GetExecutingAssembly().GetNa me().Name & "." &
formType)
Dim f As Form = _
DirectCast(Activator.CreateInstance(t, New Object() {1}), Form)
f.ShowDialog()
End Sub
End Class

--
Tom Shelton
Jan 10 '07 #4
I have used this kind of code for a long time (in Delphi). It is used in the
Mainmenu. I put, eg. Customer Maintenance - CustMas, to the menu item so
that each time I only need to get the string "CustMas", in this case, then
show the form by the generic procedure.

On the other hand, I used Paul's code with success. But the formname seems
to be case sensitive in VB2005?

Benson.

"Cor Ligthert [MVP]" <no************@planet.nl¼¶¼g©ó¶l¥ó·s»D:uS******** ******@TK2MSFTNGP04.phx.gbl...
Tom,

I am always curious what people want to achieve with this kind of code.
(Beside to make it unmaintanable). You have to know the name of the form,
so there are plenty of more efficient methods in my idea.

Do you have an idea?

Cor

"Tom Shelton" <to*********@comcastXXXXXXX.netschreef in bericht
news:lp******************************@comcast.com. ..
>On 2007-01-09, Paul Craig <pj*****@gmail.comwrote:
>>Hi,
I am currently able to open forms given a string variable using the
following code:
Dim strForm As String = "Form1"
Dim theForm As Form
theForm = AppDomain.CurrentDomain.CreateInstanceAndUnwrap _
(Me.GetType.Assembly.GetName.Name, System.String.Concat
_
(Me.GetType.Assembly.GetName.Name, ".", strForm))

theForm.Show()

My problem is that I want to also be able to open forms that have
constructors that require a parameter eg. Form1(217).
When I try changing the strForm variable to "Form1(217)", i get an
error because it appears to be looking for a form actually called
Form1(217).

Any help is greatly appreciated, cheers.
Paul

Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Reflection

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.ShowForm("Form2")
End Sub

Private Sub ShowForm(ByVal formType As String)
Dim t As Type = _
Type.GetType(Assembly.GetExecutingAssembly().GetN ame().Name & "." &
formType)
Dim f As Form = _
DirectCast(Activator.CreateInstance(t, New Object() {1}), Form)
f.ShowDialog()
End Sub
End Class

--
Tom Shelton


Jan 10 '07 #5
Paul,

If you have it in a string, you have in fact the name. That is the only
thing you need to open a form.

Therefore why would you look if they are there, what you propose is a time
spending method named late binding. While you can for the same sake set your
information as a table in a shared class (this is another word for a
difficult written module). If you use a module than in my advice write it as
a class however withouth the keyword shared.

MyFormsModule as Module

This you can describe as a class and than use
dim frm as MyFormsModule.Forms("MyWhatEverForm") 'this is a collection in
that MyFormsModule
frm.ShowDialog

Not tested however cannot be far from that.

Cor

"Paul Craig" <pj*****@gmail.comschreef in bericht
news:11********************@p59g2000hsd.googlegrou ps.com...
Thanks for your replies Cor and Tom. Tom Im not sure if you read my
whole message, but I need to be able to open a form up with a string
variable and also set its constructors parameters with that same string
eg. "Form1("aParamater"). From what I could see from your example, it
just appears to be a more long winded process to what I already have in
my initial post, but let me know if I missed something.

Cor, not sure if you were actually meaning to ask me rather than Tom
the reasoning behind the need for this code. The reason is because I
have a lot of forms, and I am wanting them all to be handled the same
way with various properties and display locations applied to all these
forms. I am using inheritance but it doesnt cover everything I need,
therefore I am wanting to pass the form name and any of its parameters
as 1 string to this method which then opens the form. I can get my
method I previously posted to open the form if I just pass it the form
name but not if I also pass the parameters.

Paul

Cor Ligthert [MVP] wrote:
>Tom,

I am always curious what people want to achieve with this kind of code.
(Beside to make it unmaintanable). You have to know the name of the form,
so
there are plenty of more efficient methods in my idea.

Do you have an idea?

Cor

"Tom Shelton" <to*********@comcastXXXXXXX.netschreef in bericht
news:lp******************************@comcast.com ...
On 2007-01-09, Paul Craig <pj*****@gmail.comwrote:
Hi,
I am currently able to open forms given a string variable using the
following code:
Dim strForm As String = "Form1"
Dim theForm As Form
theForm = AppDomain.CurrentDomain.CreateInstanceAndUnwrap _
(Me.GetType.Assembly.GetName.Name,
System.String.Concat
_
(Me.GetType.Assembly.GetName.Name, ".", strForm))

theForm.Show()

My problem is that I want to also be able to open forms that have
constructors that require a parameter eg. Form1(217).
When I try changing the strForm variable to "Form1(217)", i get an
error because it appears to be looking for a form actually called
Form1(217).

Any help is greatly appreciated, cheers.
Paul
Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Reflection

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.ShowForm("Form2")
End Sub

Private Sub ShowForm(ByVal formType As String)
Dim t As Type = _
Type.GetType(Assembly.GetExecutingAssembly().GetNa me().Name & "." &
formType)
Dim f As Form = _
DirectCast(Activator.CreateInstance(t, New Object() {1}), Form)
f.ShowDialog()
End Sub
End Class

--
Tom Shelton

Jan 10 '07 #6

Paul Craig wrote:
Thanks for your replies Cor and Tom. Tom Im not sure if you read my
whole message, but I need to be able to open a form up with a string
variable and also set its constructors parameters with that same string
eg. "Form1("aParamater"). From what I could see from your example, it
just appears to be a more long winded process to what I already have in
my initial post, but let me know if I missed something.
There is no method to directly do what you are asking. All the
creation methods are going to think that your parameters are part of
the name. So, you have a couple of choices:

1) Parse out the parameters from the passed name. I think this is a
bad option.
2) Pass in the parameters as an array of objects.

CreateForm ("Form2", New Object() {1, 2, 3})
CreateForm ("Form3", Nothing)
CreateForm ("Form4", New Object() {"Hi", 2.4}

etc.

You might need to rethink your desing some.

--
Tom Shelton

Jan 10 '07 #7

Cor Ligthert [MVP] wrote:
Paul,

If you have it in a string, you have in fact the name. That is the only
thing you need to open a form.

Therefore why would you look if they are there, what you propose is a time
spending method named late binding.
What he is doing is not latebinding. It is dynamic creation. Yes,
there is a penalty for the creation of the object itself, because there
is a lookup of the type in the meta-data. But, once created there is no
more penalty....

' this is late binding
Dim f As object = dyanmiccreate("formname")
f.Show() ' must do runtime lookup of all methods

' this is not
dim f as form = dyanmaiccreate("formname")
form.show() ' no runtime lookup. this is a early bound call.

I use this technique a lot. For things like setup screens, that look
something like the vs options dialog. I have various user controls all
descended from a base type, and i store the type of control associated
with the tree node in the nodes tag propertie. So, then to display the
screen it's a simple matter of grabing the tag property and calling
activator.createinstance. I assign it to a common base type, so there
is no performance penalty for usign the control. Remember in OOP, you
should be programming to the interface and not to the implementation :)

--
Tom Shelton

Jan 10 '07 #8

Cor Ligthert [MVP] wrote:
Tom,

I am always curious what people want to achieve with this kind of code.
(Beside to make it unmaintanable).
I must respectfully dissagree with this statement. In fact, I believe
in many situations this kind of code increases the maintainability of
your code - by making it more flexable and resistant to change....

Here is a simple realife example. I have a server application that
takes commands from a client (with parameters) and responds with
results. The number of those commands was begining to grow at a very
rapid pace... add to the fact that i started working with another
developer, and the main server class was starting to come into source
control contention because both were adding commands - it was becomming
somewhat of a pain.

Sure, when I only had a couple of functions something like this was
fine (what follows is vast simplification of the code, and is not the
real implementation - it's also in c# :):

class Server
{
private void ExecuteCommand(string command)
{
switch (command)
{
case "SOMECOMMAND":
this.SOMEMETHOD();
break;
. .....
}
}
}
to solve the above problem, I refactored my code to use a comand
pattern where the server dyanmically created the command object based
on the command name:

so now, i had:
class Server
{
private void ExecuteCommand(string command)
{
ServerCommand command = GetCommandObject (command);
command.Execute();
}
}
abstract class ServerCommand
{
public abstract void Execute();
}

class SomeCommand : ServerCommand
{
public override void Execute()
{
// do cool stuff
}
}

This simplified things in many ways (no particular order):

1) we were no longer touching the server class everytime we added a new
command, so there was no more source control fight.

2) the server class got much simpler - focusing on what it did, handle
client connections and let the command objects handle there single
responsability.

3) extending the server was as simple as inheriting form the
ServerCommand base class and filling in the execute method.

--
Tom Shelton

Jan 10 '07 #9

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

Similar topics

14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
11
by: Susan Bricker | last post by:
Hi. I am trying to open a form using 'openargs' parameter of the docmd.openform action. The problem is that I would like to pass the form some character (string) and integer data. My plan was to...
2
by: Not Me | last post by:
Hi, I'm opening one form from another, passing the criteria through using OpenArgs (this works correctly - I have tested with a msgbox). The problem is with the findfirst method, I get the syntax...
2
by: UJ | last post by:
How can I open a window, preferably through a hyperlink, that will open a new window with no menu bar/address bar and is a specific size? TIA - Jeffrey.
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
3
by: MartinR | last post by:
Hi, I'm still new to writing code in vba as I've only been introduced to access three weeks ago. I have written this code below and it executes but does not do what I want it to do. What I want is...
7
by: mahgnilla | last post by:
Hi, I was wondering if anyone could help?! I have a button on one form (Participant form) that opens another form at a specific record (Learning Session form with specific session for that...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.