Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok everyone else has taken a shot at this I might as well too. I think this answer has been suggested but maybe not explained clear enough.</p> <p>add a new class to your project in Visual C# you would go Project -> add Class</p> <p>Give your Class a name and click ADD</p> <p>Normally I name this class Tools</p> <p>then inside the Tools.cs file</p> <p>EDITED: I was looking at some of your other questions and I think I found the actual method you are trying to implement. As someone had commented in that thread you really need to use better names, I have made changes to the method to hopefully simulate what you are going for. For anyone trying to understand this please see <a href="https://stackoverflow.com/questions/10460124/c-sharp-graphics-paint-picturebox-centering">C# graphics, paint, picturebox centering</a></p> <pre><code>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) =&gt; { 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; } } } </code></pre> <p>Now in one of your forms </p> <pre><code>Panel1.Controls.Add(Tools.NewBtn("btn1",200,200)); </code></pre> <p>or PictureBox myButton=Tools.NewBtn("btn1",200,200);</p> <p>I did not compile this or test it in anyway, there may well be a typo, please let me know if there is something obvious so I can edit out the errors</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload