将图片框用户控件添加到表单
使用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 $ c派生您的控件$ c>是正确的方法。此时,你可以覆盖你心中所需的任何方法或事件处理程序。
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.