Select to view content in your preferred language

Add dynamically created command to dynmically created menu

1477
11
08-18-2010 04:48 AM
CarlosPiccirillo
Emerging Contributor
Hi everyone,

From an existing custom menu on a custom toolbar called Data, I am trying to create a structure like this and do it all on the fly at run time:

Menu1
    |
    |----Submenu1
    |           |----Command1
    |           |----Command2
    |----Submenu2
    |           |----Command1
    |           |----Command2
    |----Submenu3


Thanks to LOTS of help from the forums, I have been able to create the menus and sub-menus on the fly with no problems but now I am stuct trying to do the same with the commands.

In the example above, if I add an existing ArcMap or custom command previously created to Command1 in Submenu1, the code works fine. If I try to add a new instance of an existing command, the code crashes trying to give it a new UID with an error of, "Value does not fall within the expected range."

        internal static void PopulateDataSubMenu(ICommandBar pMenu, string entitySetName)
        {
            try
            { 
                OleDbConnection dataConnection = new OleDbConnection();
                dataConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                                  "Data Source=\\\\ad.sfwmd.gov\\dfsroot\\data\\err_gis\\applications\\dev\\mdb\\rimGISLibDatabase\\mdb\\GISLIBFeaterClassName1.mdb";
                dataConnection.Open();

                OleDbCommand dataCommand = new OleDbCommand();
                dataCommand.Connection = dataConnection;
                dataCommand.CommandText = "SELECT distinct(Order1ClassName) " +
                                          "FROM qryorder1 " +
                                          "WHERE Order1SetName = '" + entitySetName + "'";

                OleDbDataReader dataReader = dataCommand.ExecuteReader();

                object idx = 0;
                int counter = 0;
                while (dataReader.Read())
                {
                    string className = dataReader.GetString(0);
                    idx = counter;
                    CmdTest2 cmdTest3 = new CmdTest2(entitySetName + " - " + className, entitySetName, className);
                    UID uid = new UIDClass();
                    uid.Value = cmdTest3;    <---CRASH HERE       
                    pMenu.Add(uid, ref idx);
                    counter++;
                }

                dataReader.Close();
                dataConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


In the code above, CmdTest2 is a command created using the ESRI BaseCommand template and I am passing it a couple of parameters so that I can use the same command to load different datasets.


Does anyone know how what is going on and how I can fix this?

Thanks,
Carlos
0 Kudos
11 Replies
CarlosPiccirillo
Emerging Contributor
Vincent,

The reason I wanted to have new instances of the same command is because my command takes in three parameters read in from a table. These parameters correspond to different datasets. My thinking was that I could have one command, create new instances of it, pass each instance the parameters it needs and then add each one of these to a menu/submenu. Is there a better way of doing this? I'm pretty new to C# programming so I am learning new stuff all the time. If you have another way, I'm all ears.

You can see the code of this command in the message that Neil replied to.

Thanks,
Carlos


Question for you:

Do you really need to create a new instance of your command?  Why just not pass same UID?  It will be 2 input in menu bar, but it will call the same command. 

If your command do 2 differents operation, it's better to split this command in 2 distinct command.  Else, juste add same UID in your commandItem.

Vincent
0 Kudos
AndrewMay
Emerging Contributor
Hi
I was just thinking you could create a dockable window that the user can hide/show when they want to, similar I suppose to a toolbar but it gives you a bit more flexibility with regards to what you can put on it.  It might not be exaclty what you want but could be a fall-back position if you can't get the dynamic commands to work for you.
Andy
0 Kudos