C # Добавление изображений в список

#c# #image #list

#c# #изображение #Список

Вопрос:

Я пытаюсь добавить изображения, уже используемые в ImageList с именем «Images1». Я не очень хорош в программировании программ, но я знаю, что остальная часть моей программы работает, за исключением того факта, что «Images1» не существует для двух последних пустот. Я искал, чтобы решить эту проблему, но мне трудно найти ответ для этого конкретного случая.

Как я могу сделать свой список изображений «Images1» доступным для всех моих личных пустот?

Это мой код:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Globalization;
using System.Resources;
using System.Reflection;

namespace Booster_pack_2
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            List<Image> Images1 = new List<Image>();
            ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
           string index1 = textBox1.Text;
            Bitmap image1 = (Bitmap)rm.GetObject(index1);
            pictureBox1.Image = image1;
            Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index1));
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
            string index2 = textBox2.Text;
            Bitmap image2 = (Bitmap)rm.GetObject(index2);
            pictureBox2.Image = image2;
            Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index2));
        }
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
            string index3 = textBox3.Text;
            Bitmap image3 = (Bitmap)rm.GetObject(index3);
            pictureBox3.Image = image3;
            Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index3));
        }
   }
}
  

Комментарии:

1. Объявите List<Image> Images1 как член вашего класса.

Ответ №1:

Поместите его в соответствующую область. Если вам нужно получить к нему доступ во всех ваших обработчиках событий, сделайте его членом класса:

 namespace Booster_pack_2
{
public partial class Form3 : Form
{
    List<Image> Images1;
    public Form3()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Images1 = new List<Image>();
        ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
       string index1 = textBox1.Text;
        Bitmap image1 = (Bitmap)rm.GetObject(index1);
        pictureBox1.Image = image1;
        Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index1));
    }
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
        string index2 = textBox2.Text;
        Bitmap image2 = (Bitmap)rm.GetObject(index2);
        pictureBox2.Image = image2;
        Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index2));
    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
        string index3 = textBox3.Text;
        Bitmap image3 = (Bitmap)rm.GetObject(index3);
        pictureBox3.Image = image3;
        Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index3));
    }
  

}
}

Я бы посоветовал вместо этого переместить эту строку: Images1 = new List(); в конструктор, на случай, если TextBox1 не будет отредактирован первым.