I’m new to C#, so please bear with me. I’ve created a Windows application and inserted code, found in a book, for creating a simple word processor. I’ve placed a ToolStrip, RichTextBox (Dock property set to Fill) and a FontDialogBox on a form. Further, I’ve created six buttons on ToolStrip and set their Text property to Copy, Paste, Bold, Italic, Normal and Font. In the ToolStrip’s Click event handler I’ve written the code:
private void toolStrip1_Click(object sender, EventArgs e)
{ //Get a valid font
Font f = richTextBox1.SelectionFont;
if (f == null){ f = richTextBox1.Font; }
//Take action depending on the text on the Button
switch (e.Button.Text)
{
case "Copy": richTextBox1.Copy();
break;
case "Paste": richTextBox1.Paste();
break;
case "Bold": richTextBox1.SelectionFont =
new Font(f.FontStyle.Bold);
break;
case "Italic": richTextBox1.SelectionFont =
new Font(f.FontStyle.Italic);
break;
case "Normal": richTextBox1.SelectionFont =
new Font(f.FontStyle.Regular);
break;
case "Font": if (fontDialog1.ShowDialog() == DialogResult.OK)
{ richTextBox1.SelectionFont = fontDialog1.Font;
}
break;
}
}
I get the following errors:
(i) ‘System.EventArgs' does not contain a definition for 'Button'
(ii) ‘System.Drawing.Font' does not contain a definition for 'FontStyle'
I’d be grateful if someone could point out what the errors mean and how I can correct them.
private void toolStrip1_Click(object sender, EventArgs e)
{ //Get a valid font
Font f = richTextBox1.SelectionFont;
if (f == null){ f = richTextBox1.Font; }
//Take action depending on the text on the Button
switch (e.Button.Text)
{
case "Copy": richTextBox1.Copy();
break;
case "Paste": richTextBox1.Paste();
break;
case "Bold": richTextBox1.SelectionFont =
new Font(f.FontStyle.Bold);
break;
case "Italic": richTextBox1.SelectionFont =
new Font(f.FontStyle.Italic);
break;
case "Normal": richTextBox1.SelectionFont =
new Font(f.FontStyle.Regular);
break;
case "Font": if (fontDialog1.ShowDialog() == DialogResult.OK)
{ richTextBox1.SelectionFont = fontDialog1.Font;
}
break;
}
}
I get the following errors:
(i) ‘System.EventArgs' does not contain a definition for 'Button'
(ii) ‘System.Drawing.Font' does not contain a definition for 'FontStyle'
I’d be grateful if someone could point out what the errors mean and how I can correct them.