Reputation: 3
How to add icons with a button (for devices in a house) when selected in a ComboBox
into a FlowLayoutPanel
. The ComboBox
has a list of different devices and when you select one, the power rating appears in a TextBox
.
Is it possible to store this information and an icon in a FlowLayoutPanel
?
However, I am not sure if a FlowLayoutPanel
is the correct option to do this.
Upvotes: 0
Views: 3520
Reputation: 5314
Is it possible to store this information and an icon in a flow layoutpanel?
Not really, since the panel is meant to store UI controls, not arbitrary data. You can of course work around this.
One way to do this is with a backing list of custom objects that contain an Image. The code below defines such a class (with a title and image for each item) and uses this info plus a couple of event hooks to fill the panel with images based on the combobox, and then set the text in a textbox based on clicks on the images.
public partial class Form2 : Form
{
List<Item> itemList = new List<Item>();
public Context c = new Context();
public Form2()
{
InitializeComponent();
itemList.Add(new Item("First"));
itemList.Add(new Item("Second"));
itemList.Add(new Item("Third" ));
comboBox1.DataSource = itemList;
comboBox1.DisplayMember = "Title";
foreach (var itemView in itemList)
{
itemView.onClick += this.ItemClicked;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var s = (ComboBox) sender;
flowLayoutPanel1.Controls.Add(itemList[s.SelectedIndex].ThisImage);
}
private void ItemClicked(object sender, EventArgs e)
{
Item v = (Item)sender;
AssignTextBox(v.Title);
}
public void AssignTextBox(string text)
{
textBox1.Text = text;
}
}
public class Item
{
public string Title { get; set; }
public PictureBox ThisImage { get; set; }
public delegate void clicked(object sender, EventArgs e);
public event clicked onClick;
public Item(string title)
{
Title = title;
ThisImage = new PictureBox();
ThisImage.Paint += Paint_This;
ThisImage.Click += onClicked;
}
public void onClicked(object sender, EventArgs e)
{
if (onClick != null)
onClick(this, e);
}
private void Paint_This(object sender, PaintEventArgs e)
{
using (Font f = new Font("Verdana", 14))
{
e.Graphics.DrawString(Title, f, Brushes.Black, new Point(1,1));
}
}
}
Upvotes: 1