[英]C# PictureBox variable in a class
I was trying to make a struct with 2 variables, Double and PictureBox.我试图用 2 个变量创建一个结构,Double 和 PictureBox。 People suggested that I should use class instead of struct.人们建议我应该使用 class 而不是 struct。 When I try to create the class.当我尝试创建类时。 It won't let me add PictureBox card;
它不会让我添加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;只放使用 System.Windows.Forms; then you will be able to use PictureBox card;然后你就可以使用PictureBox卡了;
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
.一个类会更容易,我还建议使用List
而不是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
为此,您需要参考System.Windows.Forms
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.