How to add an icon or image to a tab in Visual Studio 2010

余生颓废 提交于 2019-12-10 02:21:39

问题


I want to put an icon in the tab header so that this


looks like this.


回答1:


You can do it in the VS Designer this way:

  1. Add an ImageList to your form.
  2. Set the ImageList property of the TabControl to the ImageList which contains the icons.
  3. Set the ImageIndex or ImageKey property of each TabPage in the TabControl to the desired image you want to display.

If you'd like to do it all in code, here's how to go about it.

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);
    }
}



回答2:


If you are using WPF:

<TabItem>
    <TabItem.Header>
        <StackPanel Orientation="Horizontal">
            <Image VerticalAlignment="Center" Source="Icon Imagepath"/>
            <TextBlock>Tab header text</TextBlock>
        </StackPanel>
    </TabItem.Header>
</TabItem>

If you are using WinForms:

  1. Open your form in designer mode
  2. Drop an ImageList on the form and fill it with your icons.
  3. Set the TabControl.ImageList property.
  4. For each tab page, set the ImageIndex property.


来源:https://stackoverflow.com/questions/8429788/how-to-add-an-icon-or-image-to-a-tab-in-visual-studio-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!