:: Home

  login:         
  passwords:  

Winforms Interview Questions

Windows Forms Deployment
Windows Forms Controls
Windows Forms Data Binding
Windows Forms Datagrid
Windows Forms Docking
Windows Forms Keyboard Handling
Windows Forms Layout
Windows Forms Licensing
Windows Forms Menus
Windows Forms Mouse Handling
Windows Forms from MFC
Windows Forms from VB6
Windows Forms Patterns
Windows Forms Printing
Windows Forms Resources
Windows Form Scrolling
Windows Forms Tips
Windows Forms Common Dialogs
Windows Forms Listbox
Windows Forms ComboBox
Windows Forms Rich TextBox
Windows Forms ListView
Windows Forms TreeView
Windows Forms Button
Windows Forms TabControl
Windows Forms TextBox
Windows Forms MDI
Windows Forms Cursors
Windows Forms WebBrowser
Windows Forms PictureBox
Windows Forms Form
Windows Forms MDI
Windows Forms In IE
GDI+Bitmaps&Images
GDI+Font
GDI+Color
GDI+Brushes
GDI Drawing Tips
GDI+ from GDI
GDI Paths Regions
GDI+Pens
Interioerability WIn32
Tools Metadata Viewers
Design Time Serialization
Design Time Custom Designers
Design Time Tips
Design Time Type Editors
Vs.Net Tips
Vs.Net Debugging
Vs.Net Macros
Framework Tips General
Framework Tips Events
Framework Tips General IO
Framework Tips Strings
Framework Tips Threading
Tool Resource Editor
Design Time UI
Framework Tips CGI
Framework Tips XML

WPF Interview Qs

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

GDI+ Font

The General Winforms Interview Questions consists the most frequently asked questions in Winforms. This list of 100+ questions guage your familiarity with the Winforms platform. The q&a have been collected over a period of time from various blogs, forums and other similar Winforms sites

34. GDI+ Font

    34.1 How do I set the font for a control?
    34.2 When I try to set a particular font style, say italics, I get an error message "Property cannot be assigned to -- it is read only". How can I set this read only property?
    34.3 How can I draw font samples from the available fonts?
    34.4 MeasureString seems to have problems with white space and punctuation. Is there a better method to use?
    34.5 How can I draw a single line of text with different fonts using DrawString?
    34.6 How do I display adjacent text?
    34.7 How can I fill a combobox with available fonts?

34.1 How do I set the font for a control?

Use the Font property for the control along with the Font class in the System.Drawing class.

button1.Font = new Font ("Courier", 10, FontStyle.Bold);

34.2 When I try to set a particular font style, say italics, I get an error message "Property cannot be assigned to -- it is read only". How can I set this read only property?

Code such as
tabControl1.Font.Italic = true;
will not work. Instead, you have to create a new Font object, and set the property during its creation. This code will work.
tabControl1.Font = new Font(tabControl1.Font, FontStyle.Italic);

34.3 How can I draw font samples from the available fonts?

James DeBroeck of Microsoft gave this response on the microsoft.public.dotnet.framework.windowsforms.
Here is some C# OnPaint code that will show you how to use FontFamily to get a list of fonts:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
int i, nCount;
SolidBrush b = new SolidBrush( Color.Black );
int x = 0, y = 0;
nCount = FontFamily.Families.Length;

for(i=0;i
{
FontStyle fs = FontStyle.Regular;
Font f;
String StylesDesc = "";

if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Regular))
StylesDesc += "Regular ";
else
fs = FontStyle.Italic;
if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Italic))
StylesDesc += "Italic ";
else
fs = FontStyle.Bold;
if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Bold))
StylesDesc += "Bold ";
if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Underline))
StylesDesc += "Underline ";
if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Strikeout))
StylesDesc += "Strikeout ";
if (StylesDesc.Length > 0)
StylesDesc = StylesDesc.Substring(0,StylesDesc.Length-1);
f = new Font( FontFamily.Families[i].Name, 12, fs);
String s = FontFamily.Families[i].Name + " " + StylesDesc;
e.Graphics.DrawString( s, f, b, x, y );
y += f.Height;

}
}

34.4 MeasureString seems to have problems with white space and punctuation. Is there a better method to use?

Try calling one of the overloads of MeasureString that takes an StringFormat parameter and pass it StringFormat.GenericTypographic. Here is some code that shows the same string and font giving different results depending upon the StringFormat parameter.

StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
SizeF size = e.Graphics.MeasureString("this has some words", Font, 500, sf);
SizeF size1 = e.Graphics.MeasureString("this has some words", Font);
string s = string.Format("GenericTypographic={0:f2} GenericDefault={1:f2}", size.Width, size1.Width);
MessageBox.Show(s);

34.5 How can I draw a single line of text with different fonts using DrawString?

he key is that you have to calculate the ascent height of the font. The font ascent as reported by FontFamily.GetCellAscent is in what is called 'Design Units'. The Cell Spacing design unit value of fonts is proportional to the actual height of the font on the device. We use this relationship to calculate cell ascent in device units.
The rendering code has to just ensure that the x, y position passed to DrawString takes care of the ascent.

private void HandlePaint(object sender, PaintEventArgs args)
{
// clear the background
Graphics g = args.Graphics;
g.Clear(Color.AliceBlue);
// create a pen
Pen pen = new Pen(Color.Red, 1f);
// the string to be drawn
string s = "Side by side";
// the first font
Font f1 = new Font("Arial", 10f);
float strWidth1 = g.MeasureString(s, f1).Width;
float fontHeight1 = f1.GetHeight(g);
float fontAscentHeight1 = (fontHeight1/f1.FontFamily.GetLineSpacing(f1.Style))*f1.FontFamily.GetCellAscent(f1.Style);
// the second font
Font f2 = new Font("Times New Roman", 48);
float fontHeight2 = f2.GetHeight(g);
float fontAscentHeight2 = (fontHeight2/f2.FontFamily.GetLineSpacing(f2.Style))*f2.FontFamily.GetCellAscent(f2.Style);
// draw the base line
Point ptStart = new Point(0, this.ClientSize.Height/2);
Point ptEnd = new Point(this.ClientSize.Width, this.ClientSize.Height/2);
g.DrawLine(Pens.Black, ptStart, ptEnd);

// draw string with first font
g.DrawString(s, f1, Brushes.Red, new PointF(0, ptStart.Y - fontAscentHeight1));
// draw string with second font
g.DrawString(s, f2, Brushes.Red, new PointF(strWidth1, ptStart.Y - fontAscentHeight2));
}

34.6 How do I display adjacent text?

Check out this article by David C. Brown of the Windows Forms team at windowsforms.net. It discusses several reasons why you GDI+ output may lok differently that GDI output. One of the topics discussed is how to draw adjacent text.


34.7 How can I fill a combobox with available fonts?

Try
comboBox1.Items.AddRange(FontFamily.Families);

Copyright 2007, Megasolutions Ltd