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 7898699
In Process

The Archive Base Latest Questions

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

Ok, so this is the problem: in C# forms I’ve created a new private

  • 0

Ok, so this is the problem: in C# forms I’ve created a new private void:

private void NewBtn(string Name, int x, int y)

Which has a purpose of creating a picturebox that imitates behavior of a button (don’t ask why, I simply enjoy complicating things) and can be called as many times as I want.

Font btnFont = new Font("Tahoma", 16);
PictureBox S = new PictureBox();
S.Location = new System.Drawing.Point(x, y);
S.Paint += new PaintEventHandler((sender, e) =>
{
    e.Graphics.TextRenderingHint = 
        System.Drawing.Text.TextRenderingHint.AntiAlias;
    e.Graphics.DrawString(Name, btnFont, Brushes.Black, 0, 0);
});
Controls.Add(S);

Now, I am worried about part with Paint/Graphics (ignore rest of the code, I only gave some of it). I want to center the text that I write as “Name” in void when I call it “NewBtn(Name, x, y)”. So, what should I put as

e.Graphics.DrawString(Name, btnFont, Brushes.Black, ThisX???, 0);

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:30:19+00:00Added an answer on June 3, 2026 at 8:30 am
    var size = g.MeasureString(Name, btnFont);
    
    e.Graphics.DrawString(Name, btnFont, Brushes.Black,
                          (S.Width - size.Width) / 2,
                          (S.Height - size.Height) / 2));
    

    You can improve this by measuring the string only once, considering the font and text won’t change for a specific Button/PictureBox.

    And I would also suggest checking if S.Size is wider / taller than size and handling it, so graphics won’t try to draw a string starting at negative coordinates.

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

Sidebar

Related Questions

I'm new to Forms Authentication and am having difficulty with this problem: I have
I'm trying to solve this problem for a long time. I have 2 forms,
This problem has bugged me so many times and i have now decided to
This problem is happening for me on IE8 and Chrome which makes me think
I hit this problem in Windows Forms, after using PInvoke of SetWindowLongPtr to remove
I am new to MongoDB, but am looking at it to solve this problem:
I'm new to angularjs and python and I have this problem. I've been trying
i am getting started with zend framework.I have encountered this problem with forms. when
I have a problem with my application: my application has many forms and need
I am experiencing a strange problem with asp.net forms authentication. This problem only occurs

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.