none
how to set the default value to the dropdown in word ribbon tab RRS feed

  • Question

  • Hi,

    I am trying to create the Custom Tab in word addin project, in that  i have written a code like below

    <ribbon>
            <tabs>
                <tab label="Custom Tab">
      <group id="group1" label="New Group">

       <dropDown id="dropdownId" label="Swap Environment">
         <item id="UAT" label="UAT"/>
         <item id="SIT" label="SIT"/>
      </groups>
         </tab>   
            </tabs>
     </ribbon>

    now dropdown is showing with blank as default and SIT and UAT as values when we expand the dropdown. I need to set the dropdown default value as SIT or UAT .

    Can you please help me on this.

    Thanks

    Nagaravikumar


    ravi

    Monday, August 27, 2012 11:11 AM

Answers

  • Hi RAvi

    You need to use the getSelectedItemID (or getSelectedItemIndex) attribute of the dropDown element:

    <dropDown id="TestDropdown" getSelectedItemID="ddGetSelectedItemID" visible="true" onAction="ddOnaction"> <item id="one" label="one" /> <item id="two" label="two"/>

    </dropDown>

    The callback signature and some sample code are below. Notice the use of the Startup variable at the class level. This will be false when the Ribbon loads. When the Ribbon loads it will execute the "Get" callback which will return the ID value I want ("two" in my example) and display it in the dropdown. It then sets Startup to true so that in the future the first half of the IF statement is executed, which returns the value the user selected.
            bool StartUp = false;
            string ddSelection = "";
            public void ddOnaction(Office.IRibbonControl control, string selectedID, int selectedIndex)
            {
                ddSelection = selectedID;
                MessageBox.Show(ddSelection);
            }
            public string ddGetSelectedItemID(Office.IRibbonControl control)
            {
                if (StartUp)
                {
                    return control.Id;
                }
                else
                {
                    StartUp = true;
                    return "two";
                }
            }


    Cindy Meister, VSTO/Word MVP


    Addendum to this item: The top part of the "IF" in the code above is not correct, as it should be returning a valid ID for a dropdown item. It does not, but in the context of this sample that doesn't matter as GetSelectedItemID is not triggered again. This would have to be fixed if the Ribbon would need to preset the selection in the dropdown at any later point.
    Monday, August 27, 2012 12:58 PM
    Moderator
  • Hi ravi

    getSelectedItemId is indeed called when the Ribbon is loaded. I tested it yesterday and it worked just fine. Are you certain the value you pass back is a valid id for an item in the dropdown (including capitalization)? Note this is an ID, not a value (label) that appears in the list!


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by pnrkumar Friday, August 31, 2012 8:58 AM
    Wednesday, August 29, 2012 4:25 PM
    Moderator

All replies

  • Hi RAvi

    You need to use the getSelectedItemID (or getSelectedItemIndex) attribute of the dropDown element:

    <dropDown id="TestDropdown" getSelectedItemID="ddGetSelectedItemID" visible="true" onAction="ddOnaction"> <item id="one" label="one" /> <item id="two" label="two"/>

    </dropDown>

    The callback signature and some sample code are below. Notice the use of the Startup variable at the class level. This will be false when the Ribbon loads. When the Ribbon loads it will execute the "Get" callback which will return the ID value I want ("two" in my example) and display it in the dropdown. It then sets Startup to true so that in the future the first half of the IF statement is executed, which returns the value the user selected.
            bool StartUp = false;
            string ddSelection = "";
            public void ddOnaction(Office.IRibbonControl control, string selectedID, int selectedIndex)
            {
                ddSelection = selectedID;
                MessageBox.Show(ddSelection);
            }
            public string ddGetSelectedItemID(Office.IRibbonControl control)
            {
                if (StartUp)
                {
                    return control.Id;
                }
                else
                {
                    StartUp = true;
                    return "two";
                }
            }


    Cindy Meister, VSTO/Word MVP


    Addendum to this item: The top part of the "IF" in the code above is not correct, as it should be returning a valid ID for a dropdown item. It does not, but in the context of this sample that doesn't matter as GetSelectedItemID is not triggered again. This would have to be fixed if the Ribbon would need to preset the selection in the dropdown at any later point.
    Monday, August 27, 2012 12:58 PM
    Moderator
  • Hi Cindy,

    I have tried the above solution, but not able to fire the callback method GetSelectedItemID. is it call when ribbon load it self? are we need to do anything else. Because for the same dropdown we are using GetVisible callback also, it is firing automatically when the ribbon loads. Please let me know how to fire the GetSelectedItemID callback on load...

    Thanks

    Nagaravikumar


    ravi

    Wednesday, August 29, 2012 1:46 PM
  • Hi ravi

    getSelectedItemId is indeed called when the Ribbon is loaded. I tested it yesterday and it worked just fine. Are you certain the value you pass back is a valid id for an item in the dropdown (including capitalization)? Note this is an ID, not a value (label) that appears in the list!


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by pnrkumar Friday, August 31, 2012 8:58 AM
    Wednesday, August 29, 2012 4:25 PM
    Moderator
  • Hi Cindy,

    Thank you.. it is worked for me....

    Thanks

    Nagaravikumar


    ravi

    Friday, August 31, 2012 9:00 AM