简体   繁体   English

C#图片框

[英]Picture Boxes in C#

I am having a problem with adding some picture boxes . 我在添加一些picture boxes时遇到问题。 I have a form (and it's class) and a separate class. 我有一个form (和它的类)和一个单独的类。 What i want to do is to put some picture boxes from my second class in the form , but it seems I just can't figure out how. 我想做的是将第二堂课的一些picture boxes放在form ,但看来我不知道怎么做。 Oh, and I don't want to send the picture box from the second class to the main one, and add it there, but just add it directly. 哦,我不想将图片框从第二类发送到主类,并在其中添加它,而只需直接添加即可。 If I try something like: 如果我尝试以下操作:

Form1.ActiveForm.Controls.Add(x); 

(where x is my picture box) from the second class I get 'Cross-thread operation not valid' exception. (其中x是我的图片框)从第二个类中得到'Cross-thread operation not valid'异常。

Thanks for any advices! 感谢您的任何建议!

Are you trying to run show your picture on a non-UI thread? 您是否要在非UI线程上运行以显示图片? Usually this is how you get a cross-thread operation not valid exception. 通常,这是您获得跨线程操作无效异常的方式。

You may simple need to do something like the following in your class. 您可能需要在课堂上简单地执行以下操作。

if (this.InvokeRequired)
{
   this.Invoke(() => ActiveForm.Controls.Add(x));
}
else
{
   ActiveForm.Controls.Add(x);
}

This will ensure you are using the UI thread to execute your code. 这将确保您正在使用UI线程执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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