Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Custom TextBox [modified]

Custom TextBox [modified]

Scheduled Pinned Locked Moved C#
graphicshelpworkspace
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ajtunbridge
    wrote on last edited by
    #1

    Hello. Got an annoying problem here. I'm trying to create a custom TextBox which renders the text central vertically. Additionally, I want the font to reduce in size if it is wider than the TextBox so it is all visible. The problem is the text renders fine until it's too wide, then it won't draw at all. Here's the KeyPress event handler: private void TextArea_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 8) { if (baseString.Length > 0) baseString.Remove(baseString.Length - 1, 1); } else baseString.Append(e.KeyChar); if (baseString.Length == 0) { using (Graphics gfx = this.CreateGraphics()) { gfx.Clear(this.BackColor); return; } } // Setup the graphics object Graphics g = this.CreateGraphics(); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; SizeF stringSize = g.MeasureString(baseString.ToString(), this.Font); if (stringSize.Width > this.Width) { while (stringSize.Width > this.Width) { this.Font = new Font(this.Font.FontFamily, this.Font.Size - 1); stringSize = g.MeasureString(baseString.ToString(), this.Font); } } else { while (stringSize.Height < (this.Height - 6)) { this.Font = new Font(this.Font.FontFamily, this.Font.Size + 1); stringSize = g.MeasureString(baseString.ToString(), this.Font); } } g.Clear(this.BackColor); float posX, posY; posX = (this.Width / 2) - (stringSize.Width / 2); posY = (this.Height / 2) - (stringSize.Height / 2); g.DrawString(baseString.ToString(), this.Font, new SolidBrush(this.ForeColor), posX, posY); g.Dispose(); } You can see the reduced size string flicker in and out when typing fairly fast but then it disappears. Any help would be appreciated. ----------------------------------------------------------------- EDIT: Realised I should have put the painting in the OnPaint method which I did, and found an unintentional loop which I've removed. It's working correctly now apart from the fact that as the text gets longer, and therefore the font smaller, my alignment veers off to the left a bit.

    modified on Wednesday, September 24, 2008 9:29 AM

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups