I have the following idea for your problem, but it will depend on you using some conventions: the images should all be in the same format and the images related to Coloured should be in the format name_color.extension. For example, if you are using PNG as image format, the eraser item would look borracha_azul.png. Follow the code I set for example:
Create a class ComboBoxItem
. It will serve to popular combos with key/value items:
public class ComboboxItem
{
public string Texto { get; set; }
public object Valor { get; set; }
public override string ToString()
{
return Texto;
}
}
Populate your combos using created class objects:
ComboboxItem itemBorracha = new ComboboxItem();
itemBorracha.Texto = "Borracha";
// Utilize como valor o nome da imagem do item
itemBorracha.Valor = "borracha.png";
ComboboxItem itemBorrachaAzul = new ComboboxItem();
itemBorrachaAzul.Texto = "Azul";
// utilize como valor o nome da cor usada no nome da imagem
itemBorrachaAzul.Valor = "azul";
comboCor.Items.Add(itemBorrachaAzul);
comboItem.Items.Add(itemBorracha);
Use the event SelectedIndexChanged
of the combos to change the image (if using the designer just double-click on the combo that the event is automatically created by Visual Studio):
private void comboItem_SelectedIndexChanged(object sender, EventArgs e)
{
// recupera o item selecionado do combo de itens
ComboboxItem itemSelecionado = (ComboboxItem)comboItem.SelectedItem;
// a propriedade Valor é a imagem do item
pictureBox1.Image = Image.FromFile(@"d:\img\" + itemSelecionado.Valor);
}
private void comboCor_SelectedIndexChanged(object sender, EventArgs e)
{
// Recupera os valores dos combos
ComboboxItem itemCor = (ComboboxItem)comboCor.SelectedItem;
ComboboxItem item = (ComboboxItem)comboItem.SelectedItem;
// substitui a extensão da imagem do item pelo nome da cor + extensão
string imagem = item.Valor.ToString().Replace(".png", String.Format("_{0}.png", itemCor.Valor));
pictureBox1.Image = Image.FromFile(@"d:\img\" + imagem);
}
In the code above, I’m assuming you’re using control PictureBox
to display the image. Replace "d:\img\"
in the method parameter Image.FromFile
by the correct way your images are.
WPF or Windows Forms?
– ramaral
Or ASP.NET? Combo box can be any list selection control.
– Marcus Vinicius
is windows form.
– CShrp Student
You know how to use the Combobox?
– ramaral
Dude, the combo box I know, the items are already there. I wanted to know if there is a command that changes the image according to what is selected in the combo.
– CShrp Student