C# Windows Forms – 新建一个从 PictureBox 继承的类。

huangapple go评论74阅读模式
英文:

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 { }

这会产生以下结果:

C# Windows Forms – 新建一个从 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:

C# Windows Forms – 新建一个从 PictureBox 继承的类。

Hopefully this gives you enough information to hunt down how the designer is doing it.

huangapple
  • 本文由 发表于 2023年7月23日 23:22:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定