C# 在Visual C中获取Components类以在面板中放置图像

C# 在Visual C中获取Components类以在面板中放置图像,c#,visual-studio-2010,class,components,console-application,C#,Visual Studio 2010,Class,Components,Console Application,我在VisualStudio2010中使用C在窗体应用程序中创建Components类时遇到问题。在这里,我为一个游戏创建了一个目标,在这个游戏中,你应该在目标上放置一个球 没有错误,但应用程序无法运行,只是弹出一个窗口说Bounce Stopped working,Windows正在尝试查找问题。。。如果我删除了代码,当然应用程序运行完全正常 所以有些事情是错的,但我不知道哪一部分是错的。有人有主意吗 不要理会我的瑞典评论 在我的组件类Target.cs中: using System; usi

我在VisualStudio2010中使用C在窗体应用程序中创建Components类时遇到问题。在这里,我为一个游戏创建了一个目标,在这个游戏中,你应该在目标上放置一个球

没有错误,但应用程序无法运行,只是弹出一个窗口说Bounce Stopped working,Windows正在尝试查找问题。。。如果我删除了代码,当然应用程序运行完全正常

所以有些事情是错的,但我不知道哪一部分是错的。有人有主意吗

不要理会我的瑞典评论

在我的组件类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();

您将大小设置为205100,但将位置设置为100200


所以位置在矩形之外…

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

您是否单步完成了程序?这样你就可以知道它到底在哪里崩溃了。不,我没有,但我真的不记得是怎么回事,所以当我还不太熟悉它的时候,我就跳过了它,希望有人能马上看到它出了什么问题;但我下一步要做的是了解设置断点和单步执行代码。这些是你的朋友!我会的,但与此同时,你知道问题出在哪里吗?当我查看目标时,我发现目标在代码中并非完全错误,但我想是图片的某些属性需要更改,这与图像的路径有关。Build Action-Content Copy to outputdirectory-Copy if newerThat dosent真的很匹配,因为位置就是它在面板中的位置。