txtQuestion.Text = "Question text"; int answerBreak = panel1.Height / NUMBEROFANSWERS; int runningHeightTotal = 0; panel1.Controls.Clear(); for (int i = 0; i < NUMBEROFANSWERS; i++) { Font stringFont = new System.Drawing.Font( SystemFonts.DefaultFont.ToString(), 12, System.Drawing.FontStyle.Regular); //Gotcha 1: MeasureText does not produce accurate results Size textSize = TextRenderer.MeasureText( testQuestions[currentlyTestedQuestionID].Answers[i].AnswerText, stringFont); RadioButton rb = new RadioButton(); rb.Tag = testQuestions[currentlyTestedQuestionID].Answers[i].AnswerID; //shift down if the previous answer took too much room rb.Location = new System.Drawing.Point(5, (i * answerBreak >= runningHeightTotal ? i * answerBreak : runningHeightTotal + 5)); rb.Text = testQuestions[currentlyTestedQuestionID].Answers[i].AnswerText; rb.Size = new System.Drawing.Size(textSize.Width, textSize.Height); rb.Font = stringFont; using (Graphics g = rb.CreateGraphics()) { bool height = false; int linesNumber = 1; //so the text size is measured using the Graphics object Size s = g.MeasureString(rb.Text, rb.Font).ToSize(); if (rb.Size.Height <= s.Height) height = true; if (s.Width > PANEL1WIDTH) { linesNumber = s.Width / 750; if (s.Width % PANEL1WIDTH > 0) linesNumber += 1; } rb.Size = new Size(PANEL1WIDTH, height ? (s.Height + 5) * linesNumber : rb.Size.Height * linesNumber); } runningHeightTotal += rb.Height; panel1.Controls.Add(rb); }If checkbox's size needs to be adjusted, declare rb as a CheckBox, and everything will work just as well.
Random programming things I'd want to remember
Wednesday, December 28, 2011
Adjust the label text size of radiobutton or checkbox
Subscribe to:
Post Comments (Atom)
Labels
508
(2)
accessibility
(2)
angularjs
(2)
aspnet
(2)
BackgroundWorker
(2)
benchmarking
(1)
binaryreader
(1)
bootable
(1)
c#
(40)
canvas
(1)
checkbox
(1)
code snippet
(1)
config
(1)
controls
(1)
data manipulation
(2)
debugging
(1)
embeddedresource
(3)
entity-framework
(2)
Excel
(1)
f#
(2)
firefox
(1)
foreign language
(1)
git
(2)
hardware
(1)
html
(1)
iso
(1)
ItemLoader
(1)
ItemTester
(1)
japanese
(1)
javascript
(3)
knockout
(1)
ms-access
(2)
multithreading
(3)
mvc
(1)
mvvm
(7)
objective-c
(10)
office
(1)
powershell
(1)
productivity
(1)
programming
(53)
project_euler
(2)
radiobutton
(1)
reflection
(3)
resolution
(1)
shortcut
(1)
silverlight
(3)
source control
(1)
sql
(2)
system administration
(2)
system rescue cd
(1)
tab
(1)
tdd
(4)
tip
(2)
UI
(1)
usb
(1)
visualstudio
(6)
visualstudio2008
(1)
webapi
(1)
webdev
(2)
windows
(1)
windows-phone
(4)
windows-phone-7
(8)
winforms
(4)
wpf
(4)
xaml
(8)
No comments:
Post a Comment