在ComboBox中显示ChartType的列表-图表

问题描述

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

enter image description here

如何从ChartType ComboBox提取图像,并在我的ComboBox显示ChartType列表以及这些图像?

解决方法

以下是我的代码,未使用其他类。这是在CobyC Answer的帮助下完成的。

private List<string> dataSourceNames = new List<string>();
private List<Bitmap> dataSourceImage = new List<Bitmap>();

private void loadCombobox1()
{
    // Get ChartTypes and Images 
    var resourceStream = typeof(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;
            dataSourceNames.Add(ent.Key.ToString());
            dataSourceImage.Add(ent.Value as Bitmap);
         }
     }

     //Load ChartType Into combobox
     comboBox1.DataSource = dataSourceNames;
     comboBox1.MaxDropDownItems = 10;
     comboBox1.IntegralHeight = false;
     comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
     comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
     comboBox1.DrawItem += comboBox1_DrawItem;
 }

private void comboBox1_DrawItem(object sender,DrawItemEventArgs e)
{
    e.DrawBackground();
    if (e.Index >= 0)
    {
        // Get text string
        var txt = comboBox1.GetItemText(comboBox1.Items[e.Index]);
        
        // Specify points for drawing
        var r1 = new Rectangle(e.Bounds.Left + 1,e.Bounds.Top + 1,2 * (e.Bounds.Height - 2),e.Bounds.Height - 2);

        var r2 = Rectangle.FromLTRB(r1.Right + 2,e.Bounds.Top,e.Bounds.Right,e.Bounds.Bottom);

        //Draw Image from list
        e.Graphics.DrawImage(dataSourceImage[e.Index],r1);
        e.Graphics.DrawRectangle(Pens.Black,r1);
        TextRenderer.DrawText(e.Graphics,txt,comboBox1.Font,r2,comboBox1.ForeColor,TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
    }
}
,

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

从包含图表System.Windows.Forms.DataVisualization.Charting.Chart

的程序集中获取清单流 System.Windows.Forms.DataVisualization.Charting.Design.resources
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}"));
            }
        }
    }
}

为简单起见,我只是将其添加到链接到ImageList的{​​{1}}中。

ListView

这是结果。

enter image description here


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

对于组合,我查看了 this questions

代码: 在while块中

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

和其他类:(这将在构建项目后在您的工具箱中创建一个 ... ... while (dictEnumerator.MoveNext()) { var ent = dictEnumerator.Entry; chartSelector1.Items.Add(new ChartDropDownItem($"{ent.Key}",ent.Value as Bitmap)); } ... ... 控件)

ChartSelector

看起来像这样:

enter image description here