Click here to Skip to main content
15,895,606 members
Home / Discussions / C#
   

C#

 
Questioninstance of sql server at client side Pin
laziale24-Sep-08 1:27
laziale24-Sep-08 1:27 
AnswerRe: instance of sql server at client side Pin
Simon P Stevens24-Sep-08 1:49
Simon P Stevens24-Sep-08 1:49 
GeneralRe: instance of sql server at client side Pin
laziale24-Sep-08 1:54
laziale24-Sep-08 1:54 
GeneralRe: instance of sql server at client side Pin
Simon P Stevens24-Sep-08 3:13
Simon P Stevens24-Sep-08 3:13 
AnswerRe: instance of sql server at client side Pin
HemJoshi24-Sep-08 23:06
HemJoshi24-Sep-08 23:06 
AnswerRe: instance of sql server at client side Pin
Guffa24-Sep-08 2:26
Guffa24-Sep-08 2:26 
AnswerRe: instance of sql server at client side Pin
darkelv24-Sep-08 4:00
darkelv24-Sep-08 4:00 
QuestionCustom TextBox [modified] Pin
ajtunbridge24-Sep-08 1:23
ajtunbridge24-Sep-08 1:23 
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

Questioncounting Pin
Krishna Varadharajan24-Sep-08 1:22
Krishna Varadharajan24-Sep-08 1:22 
AnswerRe: counting Pin
Simon P Stevens24-Sep-08 1:42
Simon P Stevens24-Sep-08 1:42 
AnswerRe: counting Pin
Pete O'Hanlon24-Sep-08 2:22
mvePete O'Hanlon24-Sep-08 2:22 
JokeRe: counting Pin
Guffa24-Sep-08 2:31
Guffa24-Sep-08 2:31 
GeneralRe: counting Pin
PIEBALDconsult24-Sep-08 3:50
mvePIEBALDconsult24-Sep-08 3:50 
GeneralRe: counting Pin
Paul Conrad24-Sep-08 8:49
professionalPaul Conrad24-Sep-08 8:49 
Questionfinding width and height of glyphs using its indices Pin
GSSPriya24-Sep-08 0:27
GSSPriya24-Sep-08 0:27 
Questioncreate public folder on exchange 2003 Pin
panim2524-Sep-08 0:27
panim2524-Sep-08 0:27 
QuestionComboobx pulldown problem Pin
Denver Thomas23-Sep-08 23:59
Denver Thomas23-Sep-08 23:59 
AnswerRe: Comboobx pulldown problem Pin
nelsonpaixao24-Sep-08 14:14
nelsonpaixao24-Sep-08 14:14 
GeneralRe: Comboobx pulldown problem Pin
Denver Thomas24-Sep-08 21:13
Denver Thomas24-Sep-08 21:13 
QuestionHelp me with the excel file thingy Pin
newbieprogrammerguy23-Sep-08 23:57
newbieprogrammerguy23-Sep-08 23:57 
AnswerRe: Help me with the excel file thingy Pin
#realJSOP24-Sep-08 1:11
mve#realJSOP24-Sep-08 1:11 
QuestionHow to drag and drop a file into DataGridView? [modified] Pin
mimimimilaw23-Sep-08 23:35
mimimimilaw23-Sep-08 23:35 
AnswerRe: How to drag and drop a file into DataGridView? Pin
Giorgi Dalakishvili24-Sep-08 0:07
mentorGiorgi Dalakishvili24-Sep-08 0:07 
QuestionPrint in C# Pin
Laji5923-Sep-08 23:24
Laji5923-Sep-08 23:24 
QuestionHow to know shutdown using c#? Pin
lovnin23-Sep-08 23:23
lovnin23-Sep-08 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.