在 Visual C# 中获取组件 class 以将图像放入面板中

Get Components class in Visual C# to put an image in the panel

我在 Visual Studio 2010 中使用 C# 在 Forms 应用程序中创建组件 Class 时遇到问题。在我为游戏创建目标的地方,您应该将球投向目标。

这里没有错误,但是应用程序不能运行,只是弹出一个window说"Bounce stoped working","Windows is trying to find the problem.."。如果我删除代码,当然应用 运行 完全没问题。

所以有些事情是错的,但我真的不支持那是什么地方错了。有人有想法吗?

请忽略我的瑞典评论..

在我的组件中 Class Target.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Bounce
{
 class Target : Label
 {
    public double targetPosX, targetPosY;

    public Target(Image image)
    {
        Image = image;
        BackColor = Color.Transparent; // Sätter bakgrundsfärgen till genomskinlig på kontrollen
        Size = new Size(205, 100); // Sätter storleken på kontrollen
        Visible = true; // Ser till att bollarna syns
    }

    public void ShowTarget()
    {
        targetPosX = Location.X;
        targetPosY = Location.Y;
    }

 }
}

代码中的形式bounce.cs(代码相关部分):

       //Target

       Target target;

       target = new Target(Image.FromFile("images/target.png"));
       panel.Controls.Add(target);
       target.Location = new Point(100, 200);
       target.ShowTarget();

您将大小设置为 205、100,但将位置设置为 100、200

所以位置在你的矩形之外...

问题是您的图片路径无效。它应该是图像文件的完整路径。例如"c:\someDirectory\Images\target.png"