简体   繁体   English

从另一个类调用的方法未运行

[英]c# - Methods that are called from another class are not running

I am having trouble doing something that I would have though to have been quite simple, all I am trying to do is to call a method from another class, heres how I am calling the method: 我在做一些本来很简单的事情时遇到了麻烦,我要做的就是从另一个类中调用一个方法,这就是我如何调用该方法:

Gimjes_2D_Game_Framework1.Characters.Character_One.Create();

and here is the contents of the method I am trying to call: 这是我尝试调用的方法的内容:

  public static void Create()
    {
         Form1 f = new Form1();
         System.Windows.Forms.PictureBox s = new System.Windows.Forms.PictureBox();
        //location of image (in thia case it is from resources):
         s.BackgroundImage =    Gimjes_2D_Game_Framework1.Properties.Resources.DefaultSprite;
        //Set to height and width of image:
         s.Height = 64;
         s.Width = 64;
         s.Size = new System.Drawing.Size(60, 60);
         s.Location = new System.Drawing.Point(50, 50); 
         f.Controls.Add(s);

    }

Try adding 尝试添加

f.Show();

or 要么

f.ShowDialog();

to the end of your Create method. 到您的Create方法的末尾。

Otherwise, you're making a form, and never displaying it. 否则,您正在制作表单,并且永远不会显示它。

Show gives you a modeless form, and ShowDialog gives you a modal dialog. Show给您一个无模式的表单, ShowDialog给您一个模式对话框。

See documentation here for more information. 有关更多信息,请参见此处的文档。

If you intend to return a Form1 object, to be shown at a later point, you need to change your method to: 如果打算返回 Form1对象(稍后显示),则需要将方法更改为:

public static Form1 Create()
{
     Form1 f = new Form1();

     ...

     return f;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM