简体   繁体   中英

How can I make an image appear in Windows Forms C# when a specific value in ComboBox is selected?

I am trying to make a drop down list with several options ( educational majors ) and when the user selects a specific value ( educational major ) a study plan of that Major appears.

I already did the drop down list as follows:

InitializeComponent();
string i = "Information Technology";
string m = "Medical Lap";
comboBox1.Items.Add(i);
comboBox1.Items.Add(m);
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

And now remains the process when a value is selected an image related to that value appears.

Based on your description, you want to show the correspond image when you select an item from combobox.

I suggest that you can use Dictionary to do it.

Here is a code example you can refer to.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (var item in dic)
            {
                if(item.Key==comboBox1.SelectedItem.ToString())
                {
                    pictureBox1.Image = item.Value;
                }
            }
        }
        Dictionary<string, Image> dic = new Dictionary<string, Image>();
        private void Form1_Load(object sender, EventArgs e)
        {
            dic.Add("Information", Image.FromFile("D:\\1.jpg"));
            dic.Add("Medical", Image.FromFile("D:\\2.jpg"));
            dic.Add("Political", Image.FromFile("D:\\3.jpg"));
            dic.Add("biological", Image.FromFile("D:\\4.jpg"));
            foreach (var item in dic)
            {
                comboBox1.Items.Add(item.Key);
            }
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }

The result:

在此处输入图片说明

You will need to create an event handler that subscribes to the SelectedIndexChanged event of the combobox and inside of that event handler write the code that will display the proper image.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindexchanged?view=netcore-3.1

if you created the combobox using VS once you double click on it creates the event handler for you , and attach it to the control, however if you need to add that manually you will need to do that in the initialize function see below

public Form1()
    {
        InitializeComponent();
    }




private void InitializeComponent()
    {
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        this.SuspendLayout();
        // 
        // comboBox1
        // 
        this.comboBox1.FormattingEnabled = true;
        this.comboBox1.Location = new System.Drawing.Point(174, 68);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(121, 21);
        this.comboBox1.TabIndex = 0;
        this.comboBox1.SelectedIndexChanged += new 
     ***System.EventHandler(this.comboBox1_SelectedIndexChanged);***

 }

Properties of the Combobox to assign event handler

after that you need to insert the code that will define the source of the picture box

example

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string picturelocation = "";
        picturelocation =@"c:\images\" + comboBox1.Text + ".jpg" ;
        pictureBox1.ImageLocation = picturelocation;
    }

where the combobox1.text will be your file names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM