在VSTO(VB.Net)中获取Ribbon ComboBox控件的选定项目的标签 [英] Get selected item's tag of Ribbon ComboBox control in VSTO (VB.Net)

查看:648
本文介绍了在VSTO(VB.Net)中获取Ribbon ComboBox控件的选定项目的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,可以使用Excel加载项项目为Excel 2010中的SQL列出所有员工的时间输入(时间,超时,注释,员工姓名)信息.在这里,我想向前一步,使用Excel加载项从Excel功能区中的ComboBox控件(分别在标签和标记属性中分别保存员工名称和员工ID)的位置列出选定员工的时间输入信息.

I have a code to list time entry (In Time, Out Time, Comments, Employee Name) information for all the employees from SQL in Excel 2010 using Excel Add-In project. Here, I wanted to move step ahead to list time entry information for selected employee from the ComboBox control (which holds the employee name and employee id in label and tag properties respectively) place in Excel Ribbon using Excel Add-In.

在这里,我无法从添加的ComboBox中获取所选员工的标签(Id).

Here, I was unable to get the selected employee's tag (Id) from ComboBox that I have added.

请任何人帮我解决这个问题.

Please any one help me to resolve this.

谢谢

推荐答案

这有点棘手,但可行.首先,您只能通过Ribbon XML(而不是通过设计器-至少我不知道)来做到这一点

Well this is a bit tricky but doable. First you can do that only via Ribbon XML (not via designer - at least I don't know about it)

我创建了非常简单的XML

I created very simple XML

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns">
        <group id="MyGroup"
               label="My Group">
          <dropDown id ="cbTest"
                    label="Test Item"
                    getItemID="GetItemID"
                    getItemLabel="GetItemLabel" 
                    getItemCount="GetItemCount" 
                    onAction="OnAction">
          </dropDown > 
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

关键部分是 GetItemCount (您可以根据需要命名它) 没有此回调,您将永远不会获得getItemID或getItemLabel的任何回调.

The key part is the GetItemCount (you can name it as you want) Without this callback you will never get any callback for the getItemID or getItemLabel.

然后其余的操作很容易,创建一个对象来存储所需的所有信息,例如此处

The rest is then easy, create an object that stores all the information you need, like here

public class Employee
    {
        public Employee(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
        public int ID { get; set; }
        public string Name { get; set; }
    }

使用如下所示的值初始化对象(为了更容易,我将所有内容都放入Ribbon类中,但这绝对是不好的方法)

initiate the object with values like below (for easier following I put all inside the Ribbon class but this is definitely bad approach)

public class Ribbon1 : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        private List<Employee> _employees = new List<Employee>();

        public Ribbon1()
        {
            _employees.Add(new Employee(1, "John"));
            _employees.Add(new Employee(2, "Mark"));
            _employees.Add(new Employee(3, "Tom"));
        }
// ... rest of the code here
}

然后是回调(仍在Ribbon1类之内) (有关回调列表,请此处)

and then the callbacks (still inside the Ribbon1 class) (For list of callbacks refers here)

    public int GetItemCount(Office.IRibbonControl control)
    {
        return _employees.Count;
    }

    public string GetItemID(Office.IRibbonControl control, int index)
    {
        var employee = _employees[index];
        return employee.ID.ToString();
    }

    public string GetItemLabel(Office.IRibbonControl control, int index)
    {
        var employee = _employees[index];
        return employee.Name;
    }

    public void OnAction(Office.IRibbonControl control, string selectedId, int selectedIndex)
    {
        var selected = string.Format("{0} ({1})", _employees[selectedIndex].Name, _employees[selectedIndex].ID);
        System.Windows.Forms.MessageBox.Show(selected);
    }

然后,在此示例中,您将在Office应用程序的加载项"选项卡下看到带有树值的下拉列表",当您选择一个时,您将获得员工的姓名和ID.

Then you should see the DropDown list in your Office application, under the Add-ins tab with the tree values in this example and when you select one you should get name and ID of the employee.

这篇关于在VSTO(VB.Net)中获取Ribbon ComboBox控件的选定项目的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆