将图片框用户控件添加到表单 [英] Add picture box user control to a form

查看:88
本文介绍了将图片框用户控件添加到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





使用C#和VS2010,



我尝试创建一个用户控件继承自PictureBox,我们称之为NewPicBox。我在项目下的一个新文件中声明了它。



我已经向NewPicBox设计师添加了一个ContextMenuStri,

我应该添加到NewPicBox设计师是一个普通的PictureBox,以便拥有PictureBox功能吗?



然后我想把NewPicBox放在一个表格上,



我能这样做就像放置一个普通的PictureBox一样如何?我无法在设计师身上看到它?我应该动态地做,怎么做?





我还想处理用户用鼠标按下NewPicBox的事件。 />


为了处理鼠标事件我应该将MouseUp声明为新的吗?

(new protected void MouseUp(object sender,MouseEventArgs e)



还有其他办法来处理这些事件吗?

谢谢!



我尝试过:



创建NewPicBox右键单击Projects的名称Add-> New Item ...->自定义控件

并将继承从Control更改为PictueBox

Hi,

Using C# and VS2010,

I try to create a user control which inherits from a PictureBox, lets call it NewPicBox. I have declared it in a new file under the project.

I have added to the NewPicBox designer a ContextMenuStri,
Should i add to the NewPicBox designer a regular PictureBox in order to have the PictureBox functionality?

I then want to place NewPicBox on a form,

Can i do that like placing a regular PictureBox if so how? i cant see it on the designer? should i do it dynamically, how?


I also want to handle the event of user pressing the NewPicBox with the mouse.

In order to handle the mouse event Should I declare the MouseUp as new?
(new protected void MouseUp(object sender, MouseEventArgs e)

Is there another way to handle those events?
Thanks!

What I have tried:

Creating the NewPicBox By right click on Projects' name Add->New Item...->Custom Control
and changing the inheritance from Control to PictueBox

推荐答案

PictureBox 是正确的方法。此时,你可以覆盖你心中所需的任何方法或事件处理程序。
Deriving your control from PictureBox is the right way to go. At that point, you can override any method or event handler that your heart desires.


正如John所说,从PictureBox派生你的控件是要走的路,但这并不像改变基地那么简单从UserControl到PictureBox - 会抛出错误,这可能会阻止设计师使用你的控件。

见这里:如何:继承现有的Windows窗体控件Microsoft Docs [ ^ ]解释了您需要采取的步骤,并在此处:自定义PictureBox控件 [ ^ ]示例。
As John has said, deriving your control from PictureBox is the way to go, but it's not as simple as changing the base class from a UserControl to a PictureBox - that will throw up errors which will probably stop the designer working with your control.
See here: How to: Inherit from Existing Windows Forms Controls | Microsoft Docs[^] which explains the steps you will need to take, and here: Customizing the PictureBox Control[^] for an example.


这篇关于将图片框用户控件添加到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆