在ComboBox中显示ChartType的列表-图表 [英] Show list of ChartType in ComboBox - Chart

查看:40
本文介绍了在ComboBox中显示ChartType的列表-图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Visual Studio 2019中创建一个comboBox,如图所示,

如何从ChartType ComboBox中提取图像,并在我的ComboBox中将ChartType列表与图像一起显示?

解决方案

要获取图像,您需要从已编译的.Net程序集中的嵌入式资源中提取图像.您将需要在语句中添加 using System.Resources; .

从包含图表 System.Windows.Forms.DataVisualization.Charting.Chart 的程序集中获取清单流 System.Windows.Forms.DataVisualization.Charting.Design.resources >

 公共局部类Form1:表单{公共Form1(){InitializeComponent();}private void button2_Click(对象发送者,EventArgs e){var resourceStream = typeof(System.Windows.Forms.DataVisualization.Charting.Chart).Assembly.GetManifestResourceStream("System.Windows.Forms.DataVisualization.Charting.Design.resources");使用(System.Resources.ResourceReader resReader = new ResourceReader(resourceStream)){var dictEnumerator = resReader.GetEnumerator();同时(dictEnumerator.MoveNext()){var ent = dictEnumerator.Entry;imageList1.Images.Add($"{ent.Key}",ent.Value as Bitmap);listView1.Items.Add(new ListViewItem($"{ent.Key}",$"{ent.Key}")));}}}} 

为简单起见,我只是将其添加到链接到 ListView ImageList .

  listView1.View = View.LargeIcon;listView1.LargeImageList = imageList1;listView1.SmallImageList = imageList1 

这是结果.


要创建一个带有下拉列表的组合框

对于组合,我查看了

I'd like to create a comboBox in visual studio 2019 as presented as shown,

How can I extract the images from the ChartType ComboBox and show list of ChartType in my ComboBox with the images?

解决方案

To get the images you need to extract them from the embedded resources within the compiled .Net assembly. You will need to add using System.Resources; to your using statements.

Get the manifest stream System.Windows.Forms.DataVisualization.Charting.Design.resources from the assembly containing the Chart System.Windows.Forms.DataVisualization.Charting.Chart

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        var resourceStream = typeof(System.Windows.Forms.DataVisualization.Charting.Chart)
            .Assembly.GetManifestResourceStream("System.Windows.Forms.DataVisualization.Charting.Design.resources");
        using (System.Resources.ResourceReader resReader = new ResourceReader(resourceStream))
        {
            var dictEnumerator = resReader.GetEnumerator();
            while (dictEnumerator.MoveNext())
            {
                var ent = dictEnumerator.Entry;                   
                imageList1.Images.Add($"{ent.Key}", ent.Value as Bitmap);
                listView1.Items.Add(new ListViewItem($"{ent.Key}", $"{ent.Key}"));
            }
        }
    }
}

For simplicity I just added it to a ImageList linked to a ListView.

listView1.View = View.LargeIcon;
listView1.LargeImageList = imageList1;
listView1.SmallImageList = imageList1

This is the outcome.


To create a combo box with a drop-down

For the combo I looked at this questions

The code: In the while block

   ...
   ...
    while (dictEnumerator.MoveNext())
    {
       var ent = dictEnumerator.Entry;
       chartSelector1.Items.Add(new ChartDropDownItem($"{ent.Key}",ent.Value as Bitmap));
    }
   ...
   ...

and the additional classes: (This will create a ChartSelector control in your toolbox after building the project)

public class ChartDropDownItem
{
    public string Value { get; set; }

    public Image Image { get; set; }

    public ChartDropDownItem(string val, Bitmap img)
    {
        Value = val;
        Image = img;
    }
}

public class ChartSelector : ComboBox
{
    public ChartSelector()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        DropDownStyle = ComboBoxStyle.DropDownList;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();

        if (e.Index >= 0 && e.Index < Items.Count)
        {
            ChartDropDownItem item = (ChartDropDownItem)Items[e.Index];

            e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
            e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
        }

        base.OnDrawItem(e);
    }
}

and that looks like this:

这篇关于在ComboBox中显示ChartType的列表-图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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