473,320 Members | 1,707 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,320 software developers and data experts.

opening form by name

Hi;

is it possible to open one form in .net platform that we have its name in
string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..

Nov 15 '05 #1
11 12763
Well, a variable name (including a form name) is very different from a
string, but you could always do this:

if (sFormName = "frmCustomer")
FormCustomer.Show();
else if (sFormName = "blah")
FormBlah.Show();
else if (sFormName = "blither")
FormBlither.Show();

....etc.
"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4****************@TK2MSFTNGP09.phx.gbl...
Hi;

is it possible to open one form in .net platform that we have its name in
string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..


Nov 15 '05 #2

thnaks.

I have number of forms and all names are written on database to be opened.
so I must find an automatic way

what about fro form items ?
do I have to search every item in Controls list by name or
can I find one item by name like hash

"Michael A. Covington" <Mi*****@CovingtonInnovations.com> wrote in message
news:uj***************@tk2msftngp13.phx.gbl...
Well, a variable name (including a form name) is very different from a
string, but you could always do this:

if (sFormName = "frmCustomer")
FormCustomer.Show();
else if (sFormName = "blah")
FormBlah.Show();
else if (sFormName = "blither")
FormBlither.Show();

...etc.
"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4****************@TK2MSFTNGP09.phx.gbl...
Hi;

is it possible to open one form in .net platform that we have its name in string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..



Nov 15 '05 #3
There are several ways to instantiate a class dynamically. You can check
out the Reflection APIs or the Activator class. Once the object is created,
you can simply use the standard methods such as Show().

"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
Hi;

is it possible to open one form in .net platform that we have its name in
string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..


Nov 15 '05 #4

thanks for your help;
i think activator class will work

but I cant add activator functions to project.
compiler says System.Activator does not exist

Can you please help me with how to use activator class.


"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
There are several ways to instantiate a class dynamically. You can check
out the Reflection APIs or the Activator class. Once the object is created, you can simply use the standard methods such as Show().

"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
Hi;

is it possible to open one form in .net platform that we have its name in string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..



Nov 15 '05 #5

thanks for your help;
i think activator class will work

but I cant add activator functions to project.
compiler says System.Activator does not exist

Can you please help me with how to use activator class.


"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
There are several ways to instantiate a class dynamically. You can check
out the Reflection APIs or the Activator class. Once the object is created, you can simply use the standard methods such as Show().

"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
Hi;

is it possible to open one form in .net platform that we have its name in string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..



Nov 15 '05 #6
search on 'using'
"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...

thanks for your help;
i think activator class will work

but I cant add activator functions to project.
compiler says System.Activator does not exist

Can you please help me with how to use activator class.


"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
There are several ways to instantiate a class dynamically. You can check
out the Reflection APIs or the Activator class. Once the object is

created,
you can simply use the standard methods such as Show().

"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
Hi;

is it possible to open one form in .net platform that we have its name

in string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..




Nov 15 '05 #7
Could you post a small repro of your problem. When I try something like the
following, I can compile.

object act = Activator.CreateInstance(typeof(string), null);

"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...

thanks for your help;
i think activator class will work

but I cant add activator functions to project.
compiler says System.Activator does not exist

Can you please help me with how to use activator class.


"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
There are several ways to instantiate a class dynamically. You can check
out the Reflection APIs or the Activator class. Once the object is

created,
you can simply use the standard methods such as Show().

"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
Hi;

is it possible to open one form in .net platform that we have its name

in string ?

I have
string sFormName = "frmCustomer";
and I must automatically open Customer form.

or is it possible to get one referance of an item on form ?
i mean,
ctrlText = xxxx("textBox1");
ctrlText .Enabled = false;

thanks..




Nov 15 '05 #8
Should not matter. I beleive that Activator is part of the System namespace
in the mscorlib assembly.

"james" <no****@hypercon.net> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
search on 'using'
"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...

thanks for your help;
i think activator class will work

but I cant add activator functions to project.
compiler says System.Activator does not exist

Can you please help me with how to use activator class.


"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
There are several ways to instantiate a class dynamically. You can check out the Reflection APIs or the Activator class. Once the object is

created,
you can simply use the standard methods such as Show().

"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
> Hi;
>
> is it possible to open one form in .net platform that we have its
name in
> string ?
>
> I have
> string sFormName = "frmCustomer";
> and I must automatically open Customer form.
>
> or is it possible to get one referance of an item on form ?
> i mean,
> ctrlText = xxxx("textBox1");
> ctrlText .Enabled = false;
>
> thanks..
>
>
>
>
>



Nov 15 '05 #9
Dear Ahmet,

.NET contains all sorts of stuff to play around with types, forms and
variables and stuff without knowing too much about them. If you know the
name of a Type in .NET you can find out anything about it, create instances
of it and set values, execute methods, etc, etc. This is all given by the
System.Type and System.Reflection namespaces. It's wonderful!! :-)

When you look at the help info on these it's daunting to say the least.
It is however very simple once explained.

Your problem as I understand it is that you have various form names in a
database and you want to get these forms up and running with only the name
as your starting point. No problem using Reflection :-)

A form's name is it's class name. And a class name is a Type name. Or
near enough. Let's say you have a form called frmFoo. And this is defined in
your application's namespace which will be something like Akgun.WonderApp.
The full type name of your form will therefore be Akgun.WonderApp.frmFoo.
Now if you plug that name, as a string, into Type.GetType(), you'll get a
Type object - which is something that you can use to create an instance of
the form.

So:
sFormName = <Obtain form name from database>;
Type oFormType = Type.GetType ("Akgun.WonderApp." + sFormName);

Now the next part depends on whether your forms' constructors have
parameters or not.

If there are no parameters, ie. you have simply
public frmFoo() {
InitializeComponent();
: : :
}

then you can get your form up in one (complex) line.
((Form) oFormType.GetConstructor (System.Type.EmptyTypes).Invoke
(null)).Show(); // Display form..

Let's break this down. This is the long version:
ConstructorInfo oConstructorInfo = oFormType.GetConstructor
(System.Type.EmptyTypes);
Form oForm = (Form) oConstructorInfo.Invoke (null);
oForm.Show();

Every Type can provide, amongst many things, information about the
constructor or constructors that are available with the type.
GetConstructor() takes an array of the Types of the arguments to the
constructor - part of its "signature" - with which it matches against all
the constructors for that type. System.Type.EmptyTypes is simply an empty
Types array signifying that we want the constructor with no parameters.

Having got the constructor info object, we can ask it to create an
instance of the type that it knows about - in this case the required form.
That's the job of Invoke() and the null is there because the constructor
takes no arguments. The value returned is an instance of your form which is
cast and assigned to a Form variable.

Form oForm = (Form) oConstructorInfo.Invoke (null);
is the equivalent of
Form oForm = new frmForm()
except, of course, that frmFoo is not mentioned anywhere.

Ok. That's how it works if you have no arguments for your form's
contructor. In case there are args, and for anyone interested, here's a more
complex version.

Let's say that this time we're dealing with frmBar which has the
following constructor
public frmBar (integer i, double d) {...}

This time we can't use a one-liner to do the job:

Type[] aoArgTypes = new Type[2] {Type.GetType ("System.Int32"),
Type.GetType ("System.Double")}; //Fussy about names.
ConstructorInfo oConstructorInfo = oType.GetConstructor
(aoArgTypes);
Object[] aoArgs = new Object[2] {(System.Int32) 1, (double) 2};
//Not fussy.
Form oForm = (Form) oConstructorInfo.Invoke (aoArgs);
oForm.Show();

You'll see in the second line that we are again getting a constructor
info object. But this time we actually have some arguments which must be
specified. That's done in the first line and you'll notice that while frmBar
takes an integer, GetType() requires a string containing the full .NET name,
"System.Int32". Likewise for double.
The third line creates an array corresponding to the array of types, but
this time with the actual argument values. This array is then given to
Invoke() so that the constructor has some values to play with.

Easy, huh?

I would recommend having form constructors with no arguments because
that makes it dead simple using the first example. Having arguments will
involve storing type names and values in the database and packaging them up
for GetConstructor() and Invoke(). Nah, don't want to bother with that,
thanks.

A final note. Hard-coding "Akgun.WonderApp." is not best practice. As
your empire expands, you might change the namespace to
Akgun.Apps.Database.WonderApp and forget to change the corresponding string.
It's possible to get the name from the assembly. I'll leave that as an
exercise.

Best wishes,
Fergus.
Nov 15 '05 #10
Hi again,

I forgot to mention that you'll need
using System.Reflection;
up at the top.

Fergus.
Nov 15 '05 #11
chahat
1
Dear Fergus

i try ur method of creating forms but it gives error that "object not set to reference of an object"
pls gives solution to this problem


Regards
chahat


Dear Ahmet,

.NET contains all sorts of stuff to play around with types, forms and
variables and stuff without knowing too much about them. If you know the
name of a Type in .NET you can find out anything about it, create instances
of it and set values, execute methods, etc, etc. This is all given by the
System.Type and System.Reflection namespaces. It's wonderful!! :-)

When you look at the help info on these it's daunting to say the least.
It is however very simple once explained.

Your problem as I understand it is that you have various form names in a
database and you want to get these forms up and running with only the name
as your starting point. No problem using Reflection :-)

A form's name is it's class name. And a class name is a Type name. Or
near enough. Let's say you have a form called frmFoo. And this is defined in
your application's namespace which will be something like Akgun.WonderApp.
The full type name of your form will therefore be Akgun.WonderApp.frmFoo.
Now if you plug that name, as a string, into Type.GetType(), you'll get a
Type object - which is something that you can use to create an instance of
the form.

So:
sFormName = <Obtain form name from database>;
Type oFormType = Type.GetType ("Akgun.WonderApp." + sFormName);

Now the next part depends on whether your forms' constructors have
parameters or not.

If there are no parameters, ie. you have simply
public frmFoo() {
InitializeComponent();
: : :
}

then you can get your form up in one (complex) line.
((Form) oFormType.GetConstructor (System.Type.EmptyTypes).Invoke
(null)).Show(); // Display form..

Let's break this down. This is the long version:
ConstructorInfo oConstructorInfo = oFormType.GetConstructor
(System.Type.EmptyTypes);
Form oForm = (Form) oConstructorInfo.Invoke (null);
oForm.Show();

Every Type can provide, amongst many things, information about the
constructor or constructors that are available with the type.
GetConstructor() takes an array of the Types of the arguments to the
constructor - part of its "signature" - with which it matches against all
the constructors for that type. System.Type.EmptyTypes is simply an empty
Types array signifying that we want the constructor with no parameters.

Having got the constructor info object, we can ask it to create an
instance of the type that it knows about - in this case the required form.
That's the job of Invoke() and the null is there because the constructor
takes no arguments. The value returned is an instance of your form which is
cast and assigned to a Form variable.

Form oForm = (Form) oConstructorInfo.Invoke (null);
is the equivalent of
Form oForm = new frmForm()
except, of course, that frmFoo is not mentioned anywhere.

Ok. That's how it works if you have no arguments for your form's
contructor. In case there are args, and for anyone interested, here's a more
complex version.

Let's say that this time we're dealing with frmBar which has the
following constructor
public frmBar (integer i, double d) {...}

This time we can't use a one-liner to do the job:

Type[] aoArgTypes = new Type[2] {Type.GetType ("System.Int32"),
Type.GetType ("System.Double")}; //Fussy about names.
ConstructorInfo oConstructorInfo = oType.GetConstructor
(aoArgTypes);
Object[] aoArgs = new Object[2] {(System.Int32) 1, (double) 2};
//Not fussy.
Form oForm = (Form) oConstructorInfo.Invoke (aoArgs);
oForm.Show();

You'll see in the second line that we are again getting a constructor
info object. But this time we actually have some arguments which must be
specified. That's done in the first line and you'll notice that while frmBar
takes an integer, GetType() requires a string containing the full .NET name,
"System.Int32". Likewise for double.
The third line creates an array corresponding to the array of types, but
this time with the actual argument values. This array is then given to
Invoke() so that the constructor has some values to play with.

Easy, huh?

I would recommend having form constructors with no arguments because
that makes it dead simple using the first example. Having arguments will
involve storing type names and values in the database and packaging them up
for GetConstructor() and Invoke(). Nah, don't want to bother with that,
thanks.

A final note. Hard-coding "Akgun.WonderApp." is not best practice. As
your empire expands, you might change the namespace to
Akgun.Apps.Database.WonderApp and forget to change the corresponding string.
It's possible to get the name from the assembly. I'll leave that as an
exercise.

Best wishes,
Fergus.
Jun 17 '06 #12

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

Similar topics

3
by: Display Name | last post by:
When this form's being submitted, two windows are opening instead of only one here is the function I'm using: function subForm(){ //insert form validation here //if form is valid, open a...
9
by: BLUE WATER | last post by:
Help, When I am finished entering in data into my form A, I press the save button that saves this new data to a new record. However I would like my other form to open at a specific record, the...
3
by: Rolan | last post by:
I need assistance regarding code needed to prevent someone from opening a form if a table field name(s) has been changed. For example, there is existing code to check for certain data based on...
1
by: kevcar40 | last post by:
hi is it possible to stop a form opening if the result of the query is null? eg select a company name from a combobox if the name is not in the table (ie the query returns null) can i stop...
1
by: paintedjazz | last post by:
I am using the following javascript to make a popup menu allow the user to navigate to different pages on a website. Yet it is opening windows as if I have <base target="some-name"> defined in the...
0
by: KavithaSing | last post by:
Hi everyone, In our website we have an option ‘official login’ for users to login and send information through the site. We face the following problem while loggin in. We use the following...
1
by: deararuns | last post by:
i hav a window wit a button and text box.. Then am opening a new window... suppose if i type somethin in a first window's text box.. and click the button.. the value typed in the text box shoult...
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
2
by: John | last post by:
Hi I have a number of forms which open like this; Dim frm1 As frmForm1 = New frmForm1 Dim frm2 As frmForm2 = New frmForm2 Dim frm3 As frmForm3 = New frmForm3 Dim frm4 As frmForm4 = New...
1
by: geetamadhavi | last post by:
Hi All, I have developed a php applciaiton where a new window is opening on checking the whether valid user orntot how to make that in same window after checking i have die(' not valid user ' ); i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.