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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:14:53+00:00 2026-05-25T21:14:53+00:00

I have overridden the OnPaint method of my Label control in VS2008: void Label_OnPaint(object

  • 0

I have overridden the OnPaint method of my Label control in VS2008:

void Label_OnPaint(object sender, PaintEventArgs e) {
  base.OnPaint(e);
  dim lbl = sender as Label;
  if (lbl != null) {
    string Text = lbl.Text;
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    if (myShowShadow) { // draw the shadow first!
      e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(myShadowColor), myShadowOffset, StringFormat.GenericDefault);
    }
    e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(lbl.ForeColor), 0, 0, StringFormat.GenericDefault);
  }
}

This works, but I really want to find out how to center the text both vertically and horizontally. I’ve heard of the MeasureString() method, but my “Text” complicates matters because it could include page breaks.

Could someone guide me with how to do this?

  • 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-05-25T21:14:54+00:00Added an answer on May 25, 2026 at 9:14 pm

    Here is the code i’m using at the moment,

    SizeF size;
    string text = "Text goes here";
    size = e.Graphics.MeasureString(text, font);
    x = (lineWidth / 2) - (size.Width / 2);
    y = top;
    e.Graphics.DrawString(text, font, Brushes.Black, x, y);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have overridden this method: protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { base.OnBackKeyPress(e); IAsyncResult guide
I have the following code: int a = 0; protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
I have overridden the onDraw() method as follows: public void onDraw(Canvas canvas1){ Canvas canvas2
I have overridden the ApplySortCore method for a custom BindingList like so: public void
I have this code, that draws an image. private void timer1_Tick(object sender, EventArgs e)
Suppose we have two overridden versions of a method. One accepts int as argument
I have got a JComponent. The paintComponent method is overridden. I mentioned that tooltips
I have an object of a subclass extending its superclass. There is an overridden
I have a panel with multiple panels inside of it. I have overridden OnPaint
When deleting cells it calls my setEditing:animated: method which i have overridden because I

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.