[英]c# Error: Cannot implicitly convert type 'string' to 'System.Windows.Forms.Control' (CS0029)
i am currently writing a program that allows users to view multiple transparent images ontop of one another, they are able to toggle 6 different images on or off. 我目前正在编写一个程序,允许用户彼此查看多个透明图像,他们能够打开或关闭6个不同的图像。
To explain how it works: 要解释它是如何工作的:
Image 1 is always the background (unless its toggled off in which case the next 'on' image is the background) 图像1始终是背景(除非将其关闭,在这种情况下,下一个“打开”图像是背景)
for example: 例如:
if image 1 is ON and image 2 is ON then image 2 uses image 1 as its parent. 如果图像1为开,图像2为开,则图像2将图像1作为其父对象。
if image 1 is ON, image 2 is OFF and image 3 is ON then image 3 will use Image 1 as its parent 如果图像1打开,图像2关闭,图像3打开,则图像3将使用图像1作为其父级
etc 等等
To achieve this i created an array - imageName[6]. 为此,我创建了一个数组-imageName [6]。 When an image is turned on the corresponding element (eg image 1 = imageName[0]) says [image1].
打开图像时,相应的元素(例如,图像1 = imageName [0])表示[image1]。
Using this method i was hoping to create a control which would change the 'parent' of an image based on the example shown above. 我希望使用这种方法来创建一个控件,该控件可以根据上述示例更改图像的“父级”。
I thought using the following code would work: 我认为使用以下代码将起作用:
imageB.parent = imageName[0]
//This is just an example, the required answer would be imageB.parent = imageA //I need to achieve this answer using the data in the array. //这只是一个示例,所需答案将是imageB.parent = imageA //我需要使用数组中的数据来实现此答案。
but i just get the error message listed in the title: 但是我只收到标题中列出的错误消息:
(Cannot implicitly convert type 'string' to 'System.Windows.Forms.Control' (CS0029) ) (无法将类型'string'隐式转换为'System.Windows.Forms.Control'(CS0029))
Could anyone help me with this problem? 有人可以帮我解决这个问题吗? Im afraid im stumped.
我怕我陷入困境。
Please note, my code is alot more complicated then this, the above just shows a small snippet of it rather then displaying all 776 lines of my code xD 请注意,我的代码要复杂得多,上面仅显示了其中的一小段代码,而不是显示我的代码xD的全部776行
UPDATE: To summarise, i need to assign an images parent based on the name stored in part of my array (in this case [0] ie [image1] 更新:总而言之,我需要根据存储在我的数组一部分中的名称分配一个图像父对象(在本例中为[0]即[image1]
The error im having makes this impossible, 我的错误使这不可能
Is there a way to solve this or am i just writing very bad code? 有没有办法解决这个问题,或者我只是编写非常糟糕的代码?
You have a string which is factionLayersID[0]
and you are trying to assing it a Control
variable.That's why you are getting that exception. 您有一个字符串,即
factionLayersID[0]
,并试图将其factionLayersID[0]
为Control
变量。这就是为什么要获取该异常的原因。
Your array elements type should be Control
. 您的数组元素类型应为
Control
。 Like this: 像这样:
Control[] factionLayers = new Control[6];
Then you can assign your element to parent control: 然后,您可以将元素分配给父控件:
imageB.parent = factionLayers[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.