简体   繁体   中英

C# PictureBox variable in a class

I was trying to make a struct with 2 variables, Double and PictureBox. People suggested that I should use class instead of struct. When I try to create the class. It won't let me add PictureBox card; it's just underlining it red. Any solution or reason why? or suggestion to how I can do. I just need a variable, that can hold picturebox and a double. So I can use it as an array.

Only put using System.Windows.Forms; then you will be able to use PictureBox card;

You should also use a class not a struct. See the guidelines on when to use a struct vs class.

A class would be easier, and I would also recommend using a List instead of array . Example:

    class Container
    {
        public PictureBox picture { get; set; }
        public double number { get; set; }
    }

    List<Container> PicturesAndNumbers = new List<Container>();

To add things to the list you will need to make a method:

    public void AddToList(Container ContainerToAdd)
    {
        PicturesAndNumbers.Add(ContainerToAdd);
    }

Which you can then call like this:

    Container NewContainer = new Container();
    AddToList(NewContainer);

For this you will need to reference System.Windows.Forms

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