473,795 Members | 2,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventHan dler(this.toolS trip_Click);

Here is my test code for the click event:
private void toolStrip_Click (object sender, System.EventArg s 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 1559
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.EventHan dler(this.toolS trip_Click);

Here is my test code for the click event:
private void toolStrip_Click (object sender, System.EventArg s 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.co m
>
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.TryG etValue("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.TryG etValue("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.Reflecti on:

using System.Reflecti on;

Type typeOfFormToCre ate = (Type)formTypes[codeFromDatabas e];
if (typeOfFormToCr eate == null)
{
... error: bad code in database ...
}
else
{
ConstructorInfo ci =
typeOfFormToCre ate.GetConstruc tor(Type.EmptyT ypes);
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
2554
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 problem is, how can I make the selection in the first menu affect the value list in the second, without using any buttons? I mean, you just have to click on the first menu and select the desired item, then the proper select list is "loaded" into...
1
1616
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 http://javascript.about.com/library/weekly/aa072903a.htm , and I tried to model my problem after that. My attempt can be viewed at http://www.riverhill.org/english/robb/summer_school/dynamic_menus.html (I've removed all of the other html). It's supposed to display a list of middle...
3
4778
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 creation takes some time. The approach I took was to override the OnSelect method of the MenuItem class, which had code to populate the
0
978
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 any real example. does anyone know where i could find it? thanks a lot.
3
1850
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 selected it will execute a routine whose name will also appear in the text file. Thanks. Stan Stan Smith
2
3452
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 event to I "capture" in order to build the tree node menu in real time? right click on a tree node? or is it too late? just FYI: the menu is different for each node and is based on "real time"
2
2322
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 go to query the sql db. Is it possible to use a javascript to open the web page, and the select the options I want from a dynamic dropdown menus? Also selecting the option I want from the available checkboxes, then "clicking" go.
3
2624
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 text. The problem does not occur in Firefox, Konqueror, or Opera. I'm a bit lost for what the cause could be. Demonstration URL: http://delta.teamcenturion.com Thanks in advance for the help.
0
1293
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 client installs are all overseas and need to be changed occasionally. I have figured basic dyamic menus inside VB but this doesn't help me solve the problem of being able to control the menu items from the backend. Please help! Thanks.
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7544
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.