さて、他のみんなもこれを撃ちました。私もそうかもしれません。この答えは提案されていると思いますが、十分に明確に説明されていない可能性があります。
Visual C#でプロジェクトに新しいクラスを追加します。[プロジェクト]->[クラスの追加]に移動します。
クラスに名前を付けて、[追加]をクリックします
通常、私はこのクラスにToolsという名前を付けます
次に、Tools.csファイル内
編集:私はあなたの他の質問のいくつかを見ていました、そして私はあなたが実装しようとしている実際の方法を見つけたと思います。誰かがそのスレッドであなたが本当により良い名前を使う必要があるとコメントしたので、私はあなたが何をしようとしているのかをうまくシミュレートするためにメソッドに変更を加えました。これを理解しようとしている人は、C#グラフィック、ペイント、ピクチャーボックスのセンタリングを参照してください。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyNameSpace //Make sure this matches your projects namespace
{
class Tools
{
//Because this is outside the form that is calling it, you cannot access
//the actual form or panel where you wish to place the button(PictureBox)
//so instead of returning void (which is nothing) we return the newly
//created Button(PictureBox)
public static PictureBox NewBtn(string nName, int locX, int locY)
{
Font btnFont = new Font("Tahoma", 16);
PictureBox S = new PictureBox();
//You need to specify a size for your pictureBox
S.Width=100;
S.Height=100;
S.Location = new System.Drawing.Point(locX, locY);
S.Paint += new PaintEventHandler((sender, e) =>
{
var size = g.MeasureString(Name, btnFont);
e.Graphics.DrawString(Name, btnFont, Brushes.Black,
(S.Width - size.Width) / 2,
(S.Height - size.Height) / 2));
}
return S;
}
}
}
今あなたのフォームの1つで
Panel1.Controls.Add(Tools.NewBtn("btn1",200,200));
またはPictureBoxmyButton= Tools.NewBtn( "btn1"、200,200);
私はこれをコンパイルしたりテストしたりしませんでした。タイプミスがあるかもしれません。何か明らかなことがあれば教えてください。エラーを編集できます。