Select to view content in your preferred language

Creating dynamic menus, forms

1361
4
08-10-2010 01:37 PM
CarlosPiccirillo
Emerging Contributor
Hi everyone,

My boss would like to automate the creation of menus, commands and forms dynamically in C#.

We have a custom toolbar with lots of menus and submenus on them. On these, there are commands, tools, etc. My boss would like to read a database table and create a new menu for each row in the table, i.e. buildings, roads, canals, etc. Each one of these menus would have either submenus, forms, commands or tools on it.

So far, I have been able to manually create a generic form and populate it (add checkbox controls) with different types of data.

My problem now is creating the menus themselves dynamically every time the toolbar is displayed. I have experimented with IMenuDef but from what I can see, you must know how many options (submenus) you are going to have and I will not know that at runtime. My boss wants to be able to edit the database table and have the new menus, forms, tools, etc. magically appear on the toolbar.

I've read tons of postings on the forums and I get the suspicion that it is possible to do it, but I cannot figure it out.

Any help is greatly appreciated!

Carlos
0 Kudos
4 Replies
AndrewMay
Emerging Contributor
Hi Carlos
I've a nagging feeling I've missed something in your question...but I would have thought you could use IMenuDef to achieve what you want.  You must know how many menus you have at runtime but not at design time.  Then IMenuDef::ItemCount would call the database and count the number of items you want to show.  Similarly IMenuDef::GetItemInfo will return all the items you want to show, again generated from the database.

You might find this is quite slow as the methods in IMenuDef can get called quite regularly so you may find it's better to generate your menus once on startup rather than each time you click a menu.

Another approach is to create an extension which adds items to your toolbar and menus on startup.  I've done this before but can't remember off the top of my head exactly what methods I used.  I'll post details if it comes back to me.

Hope this helps.
0 Kudos
CarlosPiccirillo
Emerging Contributor
Dr. May,

Thanks for the reply. I don't think you missed anything in my question, I think maybe I'm not understanding how to use IMenuDef properly. All of the examples I've seen, always show things hardcoded for the number of menus and I can't seem to figure out how to code this to happen dynamically.

I thought about going the extension way but am at a loss on how to even get started with it beyond creating the extension.

I will try to apply what you suggest. If you have any examples on how to create menus dynamically, I would be greatful if you could share them with me.

Thanks for your time,
Carlos
0 Kudos
LukeBadgerow
Deactivated User
Carlos --

I don't know if you've got your answer or not (I just caught this thread), but I'm doing a somewhat similar task with WPF combo boxes and my domain values : code below.  Good luck. 

public DataTable MakeDomainTable(String codedvaluedomain)
        {
            Utilities utils = new Utilities();

            DataTable domaintable = new DataTable("domaintable");
            DataColumn column;
            DataColumn column2; 
            DataRow row;
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "DomainName";
            column.Caption = "name";
            domaintable.Columns.Add(column);
            column2 = new DataColumn();
            column2.DataType = System.Type.GetType("System.String");
            column2.ColumnName = "DomainValue";
            column2.Caption = "value";
            domaintable.Columns.Add(column2);

            IWorkspace ws = (IWorkspace)ArcMap.Editor.EditWorkspace;
            if (ws == null)
            {
                ws = utils.OpenSDEWorkspace("stuff for sde connection");
            }
            IWorkspaceDomains domains = (IWorkspaceDomains)ws;
            object[] domainobject = null;

            IEnumDomain domainEnum = domains.Domains;
            IDomain domain = domainEnum.Next();
            String name = "";

            while (domain != null)
            {
                name = domain.Name;
                domain = domainEnum.Next();
                if (name.Equals(codedvaluedomain))
                {
                    break;
                }
            }
            ICodedValueDomain codeddomain = new CodedValueDomainClass();
            codeddomain = domains.get_DomainByName(name) as ICodedValueDomain;
            domainobject = new object[codeddomain.CodeCount];

            for (int a = 0; a < codeddomain.CodeCount; a++)
            {
                object domainvalue = codeddomain.get_Value(a);
                row = domaintable.NewRow();
                row["DomainName"] = codeddomain.get_Name(a);
                row["DomainValue"] = codeddomain.get_Value(a);
                
                domaintable.Rows.Add(row);

            }
            return domaintable;


Once I've got the DataTable I use the code behind for the wpf control and set the values this way:

           
DataTable manufacarray = DomainControl.manufactrdomains;
            this.hydrantmanufactcombo.ItemsSource = ((IListSource)manufacarray).GetList();
            this.hydrantmanufactcombo.SelectedValuePath = "DomainValue";
            this.hydrantmanufactcombo.DisplayMemberPath = "DomainName";
            if (featutils.HasSelectedFeatures(focusmap))
            {
                this.hydrantmanufactcombo.SelectedIndex = 
                    Convert.ToInt32(featutils.UpdateDomainValue(focusmap, "MAKE AND MODEL", currentfc, "make"));
            }
            else
            {
                this.hydrantmanufactcombo.SelectedIndex = 0;
            }


I'm actually building my RootMenu items right now.
0 Kudos
CarlosPiccirillo
Emerging Contributor
Luke,

Thanks for the email. No, I was never able to solve the problem. I could never figure out how to create the menus on the fly. I could create forms and commands that way, but not menus. I'm intrigued by your code so I will give it a try when I have some time.

Thanks,
Carlos
0 Kudos