Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:38:44+00:00 2026-06-03T08:38:44+00:00

I have numerous methods returning voids that I written for my program in C#

  • 0

I have numerous methods returning voids that I written for my program in C# forms which I use all the time. Since I need to use them on multiple forms, I figure there must be better way to save them in some Class, Data rather then copying them for every form individually… I am quite new at this so I am looking for advice. What to do?

Sample: I’ve created methods similar to these:

private void NewBtn(string nName, int locX, int locY)
private void NewPic(string nName, int locX, int locY, int SizX, int SizY, Image Img)

Now, instead of copying them in every form I want to write them in some other file and still be able to call them in any new form I add. Suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:38:45+00:00Added an answer on June 3, 2026 at 8:38 am

    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.

    add a new class to your project
    in Visual C# you would go
    Project -> add Class

    Give your Class a name and click ADD

    Normally I name this class Tools

    then inside the Tools.cs file

    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 C# graphics, paint, picturebox centering

    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;
             }
        }
    }
    

    Now in one of your forms

    Panel1.Controls.Add(Tools.NewBtn("btn1",200,200));
    

    or
    PictureBox myButton=Tools.NewBtn(“btn1”,200,200);

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ dll that contains methods that I need to use in
I have numerous functions (unknown at design time) that each take a specific number
In my program I have numerous Entity classes with a Texture2D attribute that are
I have noticed that numerous Date methods in Java have been deprecated, but rather
If you have a Bitmap object that needs to be passed to numerous methods
There are numerous Agile software development methods. Which ones have you used in practice
I have seen numerous ways to highlight the current tab, but they all have
I have seen numerous questions on MVP that mostly revolve around the View and
I have some internal-facing ASP.NET web services that have had numerous API additions over
I have two classes called Person and Animal that have a lot of methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.