英文:
C# Windows Forms - new class that inherits from PictureBox
问题
问题如下:我尝试编写我的第一个基于Windows Forms的应用程序,其中将进行一些计算。
在窗体中,我定义了一个名为“canva”的图片框,它应该用于绘制要分析的钢型材的截面。
我不想在“MainForm.cs”中拥有所有绘制该图片框上的内容的方法,因此我创建了一个名为“Sketch”的新类,该类继承自“PictureBox”。
因此,新类的定义目前相当简单:
public class Sketch : PictureBox
{
}
因此,当我初始化窗体时,我重新定义“canva”为新类型“Sketch”,以便稍后访问将绘制某些内容、箭头等的新方法。
Sketch canva = new Sketch();
canva.Show();
canva.BackColor = Color.Red;
MessageBox.Show("SketchboxName: " + canva.Name.ToString());
但是然后“canva”不显示,并且我甚至无法通过“MessageBox”检查此控件的名称。
难道不应该这样的新类定义继承自“PictureBox”的所有方法吗?
我做错了什么?
英文:
The problem is as follows: I try to write my first app based on Windows Forms, where some calculations will be done.
In form I have defined a picture box (name = canva
) that is supposed to sketch cross sections of the steel profiles that are to be analyzed.
I do not want to have all the methods that will draw something on this picture box in the MainForm.cs
, thus I created new class called Sketch
that inherits from PictureBox
.
The new class definition is thus quite simple for a while:
public class Sketch:PictureBox
{
}
So when I initilalize the form, I re-define the canva
to be of the new type of Sketch
, to access later new methods that will draw certain things, arrows, etc.
Sketch canva = new Sketch();
canva.Show.
canva.BackColor = Color.Red;
MessageBox.Show("SketchboxName: " + canva.Name.ToString());
But then the canva
does not show, and I do not even can check the name of this control through MessageBox
.
Shouldn't such a new class definition inherit all the methods from pictureBox
?
What did I do wrong?
答案1
得分: 0
在将新类Sketch
添加后,编译您的项目。由于Sketch
是一个控件,它现在会出现在窗体设计器的工具箱窗口中。您可以像添加其他控件一样,将其拖放到设计表面以添加到您的窗体中。
或者,如果您的窗体上已经有一个PictureBox
,您可以打开名为MyForm.designer.cs
的文件(其中MyForm
是您的窗体名称)。您会看到一个变量PictureBox canva;
。将其类型更改为Sketch canva;
。还有一行this.canva = new System.Windows.Forms.PictureBox()
。将其更改为this.canva = new Sketch()
。这将把现有的PictureBox
转换为Sketch
。
请注意,您的Sketch canva = new Sketch();
不起作用,因为它只定义了一个局部变量canva
,而没有将Sketch
分配给现有字段this.canva
。此外,您必须从窗体的Controls
集合中删除现有的PictureBox,然后添加您的新控件,以使其可见且可用。但最好使用我上面描述的两种方法之一,第一种方法是最简单和安全的方法。
英文:
Compile your project after having added the new class Sketch
. Since Sketch
is a control, it now appears in the Toolbox window of the forms designer. You can simply drag and drop it to the design surface as any other control to add it to your form.
Alternatively, if you already have a PictureBox
on your form, you can open the file MyForm.designer.cs
(where MyForm
is the name of your form). You will see a variable PictureBox canva;
. Change its type to Sketch canva;
. There is also a this.canva = new System.Windows.Forms.PictureBox()
somewhere. Change it to this.canva = new Skecth()
. This will convert the existing PictureBox
to a Sketch
.
Note that your Sketch canva = new Sketch();
does not work, because it defines only a local variable canva
instead of assigning a Sketch
to the existing field this.canva
. Also, you would have to remove the existing PictureBox from and add your new control to the Form's Controls
collection to make it visible and functional. But prefer one of the two approaches I described above, the first one being the most simple and safe one.
答案2
得分: 0
以下是将继承的 PictureBox
添加到 Form
的最小工作示例。
void Main()
{
Form form = new Form();
Sketch canvas = new Sketch();
canvas.BackColor = Color.Red;
form.Controls.Add(canvas);
form.Show();
}
public class Sketch : PictureBox { }
这会产生以下结果:
希望这为您提供足够的信息来查找设计器是如何实现的。
英文:
Here is a minimal working example of adding an inherited PictureBox
to a Form
.
void Main()
{
Form form = new Form();
Sketch canva = new Sketch();
canva.BackColor = Color.Red;
form.Controls.Add(canva);
form.Show();
}
public class Sketch : PictureBox { }
That gives me:
Hopefully this gives you enough information to hunt down how the designer is doing it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论