Beginning Visual C# Exercises - Chapter 15
1. The following adds menu pad under "Format", adds the "FontDialog" object, and handler code.
...Above the constructor
// Adding the FontDialog object from the toolbox writes this code
private System.Windows.Forms.FontDialog dlgFont;
...In the InitializeComponent() method
dlgFont = new System.Windows.Forms.FontDialog();
// Change default font to match text box
dlgFont.Font = textBoxEdit.Font;
// Activate the apply button
dlgFont.ShowApply = true;
// Subscribe to the apply event
dlgFont.Apply += new System.EventHandler(OnApplyFontDialog);
// miFormatFont menu pad
miFormatFont.Index = 0;
miFormatFont.Text = "&Font";
miFormatFont.Click += new System.EventHandler(miFormatFont_Click);
...At the bottom of the code as handler to Color menu pad
// Launch dialog for font change of text in textbox
private void miFormatFont_Click(object sender, System.EventArgs e)
{
dlgFont.ShowDialog();
}
// When apply button is pressed within font dialog
private void OnApplyFontDialog(object sender, System.EventArgs e)
{
textBoxEdit.Font = dlgFont.Font;
}
...Revise two lines of code in this existing method
private void OnPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
...Existing code ignored
<s>//e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);</s>
e.Graphics.DrawString(lines[linesPrinted++], textBoxEdit.Font, Brushes.Black, x, y);
<s>//y += 15;</s>
y += textBoxEdit.Font.Height;
...Existing code ignored
}
2. The following adds menu pad under "Format", adds the "ColorDialog" object, and handler code.
...Above the constructor
// Adding the ColorDialog object from the toolbox writes this code
private System.Windows.Forms.ColorDialog dlgColor;
...In the InitializeComponent() method
dlgColor = new System.Windows.Forms.ColorDialog();
// miFormatColor menu pad
miFormatColor.Index = 1;
miFormatColor.Text = "&Color";
miFormatColor.Click += new System.EventHandler(miFormatColor_Click);
...At the bottom of the code as handler to Color menu pad
// Launch dialog for color change of text in textbox
private void miFormatColor_Click(object sender, System.EventArgs e)
{
if (dlgColor.ShowDialog() == DialogResult.OK)
textBoxEdit.ForeColor = dlgColor.Color;
}
...Revise some code in this existing method
private void OnPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
...Existing code ignored
// Add following lines above the while loop
Brush pageBrush = Brushes.Black;
if (e.PageSettings.Color == true)
pageBrush = new SolidBrush(textBoxEdit.ForeColor);
...Existing code ignored
<s>//e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);</s>
<s>//e.Graphics.DrawString(lines[linesPrinted++], textBoxEdit.Font, Brushes.Black, x, y);</s>
e.Graphics.DrawString(lines[linesPrinted++], textBoxEdit.Font, pageBrush, x, y);
...Existing code ignored
}
|