ToolBarButton.ImageIndex 属性

定义

获取或设置分配给按钮的图像的索引值。

C#
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ImageIndexConverter))]
public int ImageIndex { get; set; }

属性值

分配给工具栏按钮的 Image 的索引值。 默认值为 -1。

属性

例外

value 小于 -1。

示例

下面的代码示例创建和三ToolBarButtonToolBar控件,将按钮分配给工具栏,并设置一些按钮的通用属性。 此代码要求MenuItem已创建 、 ToolTipImageList和 ,Form并且ImageList至少为其分配了一个 Image

C#
public void InitializeMyToolBar()
 {
    // Create the ToolBar, ToolBarButton controls, and menus.
    ToolBarButton toolBarButton1 = new ToolBarButton("Open");
    ToolBarButton toolBarButton2 = new ToolBarButton();
    ToolBarButton toolBarButton3 = new ToolBarButton();
    ToolBar toolBar1 = new ToolBar();
    MenuItem menuItem1 = new MenuItem("Print");
    ContextMenu contextMenu1 = new ContextMenu(new MenuItem[]{menuItem1});

    // Add the ToolBarButton controls to the ToolBar.
    toolBar1.Buttons.Add(toolBarButton1);
    toolBar1.Buttons.Add(toolBarButton2);
    toolBar1.Buttons.Add(toolBarButton3);
 
    // Assign an ImageList to the ToolBar and show ToolTips.
    toolBar1.ImageList = imageList1;
    toolBar1.ShowToolTips = true;
 
    /* Assign ImageIndex, ContextMenu, Text, ToolTip, and 
       Style properties of the ToolBarButton controls. */
    toolBarButton2.Style = ToolBarButtonStyle.Separator;
    toolBarButton3.Text = "Print";
    toolBarButton3.Style = ToolBarButtonStyle.DropDownButton;
    toolBarButton3.ToolTipText = "Print";
    toolBarButton3.ImageIndex = 0;
    toolBarButton3.DropDownMenu = contextMenu1;
 
    // Add the ToolBar to a form.
    Controls.Add(toolBar1);
 }

注解

ImageIndex引用分配给父ToolBar控件的 中ImageList图像的索引值。

ImageKeyImageIndex 是互斥的,这意味着如果设置了一个,另一个设置为无效值并忽略。 如果设置 ImageKey 属性,该 ImageIndex 属性将自动设置为 -1。 或者,如果设置 ImageIndex 属性,则 ImageKey 会自动设置为空字符串 (“”) 。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

另请参阅