如何将图标或图像添加到Visual Studio 2010中的选项卡

Xel*_*Xel 11 c# tabcontrol winforms

我想在选项卡标题中放置一个图标,以便这样

winforms选项卡
看起来像这样.

花哨的标签

Ale*_*fie 16

您可以通过以下方式在VS Designer中执行此操作:

  1. 将ImageList添加到表单中.
  2. 设置包含图标的ImageList 的ImageList属性TabControl.
  3. 将TabControl 中每个的ImageIndexor ImageKey属性设置为TabPage要显示的所需图像.

如果您想在代码中完成所有操作,请按照以下步骤操作.

using System.Drawing;
using System.Windows.Forms;

public class Form1
{

    public void Form1()
    {
        InitializeComponent();

        // initialize the imagelist
        ImageList imageList1 = new ImageList();
        imageList1.Images.Add("key1", Image.FromFile(@"C:\path\to\file.jpg"));
        imageList1.Images.Add("key2", Image.FromFile(@"C:\path\to\file.ico"));

        //initialize the tab control
        TabControl tabControl1 = new TabControl();
        tabControl1.Dock = DockStyle.Fill;
        tabControl1.ImageList = imageList1;
        tabControl1.TabPages.Add("tabKey1", "TabText1", "key1"); // icon using ImageKey
        tabControl1.TabPages.Add("tabKey2", "TabText2", 1);      // icon using ImageIndex
        this.Controls.Add(tabControl1);
    }
}
Run Code Online (Sandbox Code Playgroud)


Mah*_*eep 8

如果您使用的是WPF

<TabItem>
    <TabItem.Header>
        <StackPanel Orientation="Horizontal">
            <Image VerticalAlignment="Center" Source="Icon Imagepath"/>
            <TextBlock>Tab header text</TextBlock>
        </StackPanel>
    </TabItem.Header>
</TabItem>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是WinForms

  1. 在设计器模式下打开表单
  2. 将 ImageList 放到表单上并用您的图标填充它。
  3. 设置 TabControl.ImageList 属性。
  4. 对于每个选项卡页,设置 ImageIndex 属性。