Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# program will not compile

Status
Not open for further replies.

raydona

Programmer
May 12, 2005
27
GB
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.
 
You'll need to get the text property from sender, not e.

I think you will need to cast sender to a control, in order to retrieve its text property.

Something like:

Code:
((Control)sender).Text


Also, you don't need to create a new font at all.

You'd just want to do something like this:

Code:
richTextBox1.SelectionFont.Bold = true;

Hope this helps,

Alex


[small]----signature below----[/small]
Numbers is hardly real and they never have feelings but you push too hard, even numbers got limits
Why did one straw break the camel's back? Here's the secret: the million other straws underneath it

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top