如何解决在ComboBox中显示ChartType的列表-图表
我想在Visual Studio 2019中创建一个comboBox,如图所示,
如何从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
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
这是结果。
要创建带有下拉列表的组合框
对于组合,我查看了 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
看起来像这样:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。