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

Dynamic menus

Hello all,
I did a lot of research on the subject and came to the
conclusion that what I want to do might not be "good practice". I
created a set of table to manage user rights to forms in my solution
and hope I might be able to auto-generate the menu at solution startup.

I successfully created the menu structure but am stuck trying to add
code to the click event of my menuitems.

The database sends me the string name of the form to open. Now, how can
I instantiate a form by it's string name? Did I mention that I'm new to
OOP... I realise that I might be completely wrong trying to do such
thing. Is there a better approach to creating dynamic menus?

Hope this is clear enough...

Herre is how I generate the click event for each menuitem:
tsCont.Click += new System.EventHandler(this.toolStrip_Click);

Here is my test code for the click event:
private void toolStrip_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Test");

}

I need to open the form refered by the "project" string property of the
sender object (class is tsmiWithProject)?

Thanks!
Justin

Dec 18 '06 #1
8 1539
Justin,

See inline:
The database sends me the string name of the form to open. Now, how can
I instantiate a form by it's string name? Did I mention that I'm new to
OOP... I realise that I might be completely wrong trying to do such
thing. Is there a better approach to creating dynamic menus?
In order to create the form, or any other object dynamically, you will
have to call the static CreateInstance method on the Activator class. It
will take a Type instance which it will create an instance of. You will
probably also need to call the static GetType method on the Type class to
get the Type instance that you are referring to.

Make sure that you are storing the assembly-qualified full name of the
type, unless you are loading the form from one single library (and will only
ever load it from that library).
Hope this is clear enough...

Herre is how I generate the click event for each menuitem:
tsCont.Click += new System.EventHandler(this.toolStrip_Click);

Here is my test code for the click event:
private void toolStrip_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Test");

}
Well, when you create the forms, I would suggest putting them in a
Dictionary that is keyed by this string, and then in your Click event
handler, perform a lookup for the form.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>
I need to open the form refered by the "project" string property of the
sender object (class is tsmiWithProject)?

Thanks!
Justin

Dec 18 '06 #2
Thanks for the very fast reply. I think all I need to do is to read on
Dictionaries. MSDN, here I come! ;)

Thanks again,
Justin

Dec 18 '06 #3
Nicholas,
I spent the day working with dictionaries and learn a lot on how it
will help me. Thanks a lot. I just have a some questions though just to
be sure I understand everything right. I hope you have a few minutes to
answer since I still have difficulties finding answer to my questions
on MSDN.

Is it possible to "save" my dictionary (let say in a dll or something)
or is it just a runtime object?
In that case, must I fill my dictionary each time the application
launches?
Is it ok to create my dictionary as <string, form>? I tried working
with types but I'm a little confuse with them.

Thanks a lot!
Justin

tonFrere a écrit :
Thanks for the very fast reply. I think all I need to do is to read on
Dictionaries. MSDN, here I come! ;)

Thanks again,
Justin
Dec 19 '06 #4
tonFrere wrote:
Nicholas,
I spent the day working with dictionaries and learn a lot on how it
will help me. Thanks a lot. I just have a some questions though just to
be sure I understand everything right. I hope you have a few minutes to
answer since I still have difficulties finding answer to my questions
on MSDN.

Is it possible to "save" my dictionary (let say in a dll or something)
or is it just a runtime object?
In that case, must I fill my dictionary each time the application
launches?
Is it ok to create my dictionary as <string, form>? I tried working
with types but I'm a little confuse with them.

Thanks a lot!
Justin

tonFrere a écrit :
>Thanks for the very fast reply. I think all I need to do is to read on
Dictionaries. MSDN, here I come! ;)

Thanks again,
Justin
I am not an expert as Nicholas, but here is my answer to your question.
1You can serialize the dictionary into xml file, and load it from xml
file too. You can serialize it into other format like binary stream too.

2You should be able to create the dictionary as <string, form>.
Dec 19 '06 #5
Sounds perfect.

Thank you!
Justin

Jianwei Sun a écrit :
tonFrere wrote:
Nicholas,
I spent the day working with dictionaries and learn a lot on how it
will help me. Thanks a lot. I just have a some questions though just to
be sure I understand everything right. I hope you have a few minutes to
answer since I still have difficulties finding answer to my questions
on MSDN.

Is it possible to "save" my dictionary (let say in a dll or something)
or is it just a runtime object?
In that case, must I fill my dictionary each time the application
launches?
Is it ok to create my dictionary as <string, form>? I tried working
with types but I'm a little confuse with them.

Thanks a lot!
Justin

tonFrere a écrit :
Thanks for the very fast reply. I think all I need to do is to read on
Dictionaries. MSDN, here I come! ;)

Thanks again,
Justin

I am not an expert as Nicholas, but here is my answer to your question.
1You can serialize the dictionary into xml file, and load it from xml
file too. You can serialize it into other format like binary stream too.

2You should be able to create the dictionary as <string, form>.
begin:vcard
fn:John Sun
n:Sun;John
note;quoted-printable:The best post is here...=0D=0A=
http://www.yoda.arachsys.com/csharp/complete.html
version:2.1
end:vcard
Dec 19 '06 #6

tonFrere wrote:
Nicholas,
I spent the day working with dictionaries and learn a lot on how it
will help me. Thanks a lot. I just have a some questions though just to
be sure I understand everything right. I hope you have a few minutes to
answer since I still have difficulties finding answer to my questions
on MSDN.

Is it possible to "save" my dictionary (let say in a dll or something)
or is it just a runtime object?
You can serialize a dictionary, but I believe that the point here is to
avoid storing class names in your database. In other words, store a
code in the database to indicate which form you want, and then provide
a mapping in your program that says, "This code means this form."

If you then serialize that dictionary (mapping) into the database, it
defeats the whole purpose of having it, which is to decouple your
database information from your program design.
In that case, must I fill my dictionary each time the application
launches?
Yes. Just make a little method that fills in your dictionary and call
it on start-up. Or, if you like, put the mapping into your App.config
file. It's probably simplest to just put it directly in code rather
than a config file, since you would have to change the code to add a
new form, etc. anyway.
Is it ok to create my dictionary as <string, form>? I tried working
with types but I'm a little confuse with them.
Yes, that's fine. It implies that you'll be using string codes in your
database to represent your different forms, which is fine.

Dec 19 '06 #7
Hello again,
I think I'm doing well so far, thanks to you guys.

Could you just validate this code. Maybe there's a better way to do
things, I'm just not 100% sure about that code. The results are there
but I would rather do things right.

This is from my forms dictionary class constructor. It's how I add each
form to the dictionary:
Form2 f2 = new Form2();
add(f2.Text, f2);
REM: The "code" I'm using is the form header. I just have to make sure
it's the same name in the database.

Then, on my click event, I open the correct form using that code in a
try/catch block:
dictProjet.TryGetValue("Form2", out nouvForm);
nouvForm.Show();

Works well, just wonder if it's "swell" ;)

Justin

Dec 21 '06 #8

tonFrere wrote:
Hello again,
I think I'm doing well so far, thanks to you guys.

Could you just validate this code. Maybe there's a better way to do
things, I'm just not 100% sure about that code. The results are there
but I would rather do things right.

This is from my forms dictionary class constructor. It's how I add each
form to the dictionary:
Form2 f2 = new Form2();
add(f2.Text, f2);
REM: The "code" I'm using is the form header. I just have to make sure
it's the same name in the database.

Then, on my click event, I open the correct form using that code in a
try/catch block:
dictProjet.TryGetValue("Form2", out nouvForm);
nouvForm.Show();

Works well, just wonder if it's "swell" ;)
This code does something different from what I originally understood.

I thought that you would fetch from the database an indication of
_which type_ of form to create, and then instantiate that form.

The code you just posted instantiates all of the forms and then shows
them based on a value read from the database.

I was thinking of something more along these lines (please note that
this is written for .NET 1.1; someone else can no doubt provide a
superior implementation using generics):

Hashtable formTypes = new Hashtable();
formTypes["DBCODEFORFORM1"] = typeof(Form1);
formTypes["DBCODEFORFORM2"] = typeof(Form2);
....

The important thing to notice here is that the codes used to look up
the form types don't correspond to any property of the form. That way,
you can change the form title, or even the form name, and you don't
have to fix your database tables.

Once you have the table set up, when you retrieve a code from the
database you can create the form using System.Reflection:

using System.Reflection;

Type typeOfFormToCreate = (Type)formTypes[codeFromDatabase];
if (typeOfFormToCreate == null)
{
... error: bad code in database ...
}
else
{
ConstructorInfo ci =
typeOfFormToCreate.GetConstructor(Type.EmptyTypes) ;
if (ci == null)
{
... error: Form has no public no-argument constructor ...
}
else
{
Form newForm = (Form)ci.Invoke(null);
... do something with the newly created form ...
}
}

One important difference between this code and yours is that you can
create multiple instances of the same form this way, if you want to.

Dec 21 '06 #9

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

Similar topics

1
by: Andrea Bampi | last post by:
I'm just trying to do my first experiments with js, but I need some good examples to start with.. I need to design a dynamic form with two dropdown menus picking their select values from a db: my...
1
by: Colvin | last post by:
I'm trying to create a pair of dynamic drop-down menus. I'm new to Javascript, so I tried to find a simple example on the web. I found a good example at...
3
by: Eddie de Bear | last post by:
Hi, A project I am working on has a requirement for dynamic menus. For the most part this works really well. The menus I'm creating a based on files and directories, so naturally the menu...
0
by: JMMB | last post by:
Just couldn't find any article in MSDN about dynamic menus. I know i can create menus dynamically in run time using the information from a database combined with System.Refletion, but didn't find...
3
by: Stan Smith | last post by:
Is there a way to create dynamic menus and create dynamic events associated with those menus? I would like to create a menu system based on the contents of a text file. When the menu item is...
2
by: Ron M. Newman | last post by:
Hi, Just need a little advice. Id like to build *dynamic* context menus for tree nodes. I'm pretty versed in building context menus and attaching them to tree nodes. My question is, what...
2
by: rpeterson84 | last post by:
Hello: I was hoping to gain some insight, a point in the right direction if you will... We use an .asp web page to select from a couple of dynamic drop down menus then enter a number, and press...
3
by: EnigmaticSource | last post by:
Currently, I am designing a site using CSS driven vertical menus, it works well in everything but MSIE. The menus seem to work well enough, except that they float behind the images, but above the...
0
by: cwhiteau | last post by:
I am trying to create dynamic menus using menu items loaded in a SQL server table. I need to be able to add/delete menu items on the back end (on the server)rather than be hard coded inside VB. The...
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: 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...
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: 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: 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.