| Edward Tanguay |
In the book .NET Windows Forms Custom Controls, page 47, it says to add a control (IconButton.cs) with "Add Existing Item" into a self-made sub-directory.
I did that, then tried to compile and it gets 14 errors which say they can't recognize any classes, e.g.:
Type or Namespace 'Windows' does not exist
It is the same code of a control that works in another project.
What do I have to do to make it recognize the System, Window and all other classes?
*** Wouldn't it be great if book publishers would have a web page for each book where readers of the book could post and answer messages about it?
Thanks,
Edward Tanguay http://www.tanguay.info
_________________________________________________________________ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx
|
|
| |
| |
| Erik Brown |
Ed,
Perhaps you need a "using" directive at the top of your file? For example:
using System.Windows.Forms;
If not, post the file (or email me directly) and I might be able to help. FYI, most if not all books from Manning Publications include an Author Online feature to answer questions of this nature. Other books often include an email address for questions somewhere in the introduction. You might look for this in your book as well.
Erik Brown ========== Author of "Windows Forms Programming with C#" www.manning.com/eebrown
[Original message clipped]
|
|
| |
|
| |
| Edward Tanguay |
Hi Erik,
Thanks for the reply.
1. I have the appopriate "using" statements in my file:
using System; using System.Windows.Forms; using System.Drawing;
The exact same file worked where I created it in another directory/project/solution but not after I copied it into a sub-directory in a new project as the book indicated to do.
This is something I don't understand about Visual Studio.NET, what is going on in the background -- it seems that this file does not have the "right environment" so it doesn't function. It is easy to make little samples in VS.NET and all the books tell you how, but how to put together a useful 3-tier Windows application or Web Application, how to name the namespaces, etc., I have seen no book that does this yet. Any suggestions? Oh, wait, I just checked out your book www.manning.com/eebrown and from what I can see of the sample chapters and the table of contents, it does this. Cool, I'm ordering it. Yes, the author online section is what I was talking about, that's great. Not too much in it yet but it looks like the book was published april 2002.
2. So what would be some other reasons why I can't get this file to function just because it's not in the same place, here is the code for all it's worth, it works in one project but in another that I copied it into it can't find any of the libraries (?):
using System; using System.Windows.Forms; using System.Drawing;
namespace SAMS.Toolkit.Controls { [System.ComponentModel.Description("Edward's First Control")] public class IconButton : System.Windows.Forms.Control { #region static member fields protected static int EDGE_PADDING = 4; #endregion
#region Implementation Member Fields protected ButtonState buttonState; protected Icon buttonIcon; protected int iconDrawWidth; protected bool mousePressed; #endregion
#region IconButton Properties [ System.ComponentModel.Description("The Icon to be displayed in the button"), System.ComponentModel.Category("Appearence"), System.ComponentModel.DefaultValue(null) ] public Icon Icon { get { return buttonIcon; } set { buttonIcon = value; Invalidate(); Update(); } } #endregion
#region Construction / Initialization public IconButton() { InitializeComponent(); }
private void InitializeComponent() { buttonIcon = null; buttonState = ButtonState.Normal; mousePressed = false; } #endregion
#region Control Method Overrides protected override void OnGotFocus(EventArgs e) { Invalidate(); base.OnGotFocus(e); }
protected override void OnLostFocus(EventArgs e) { Invalidate(); base.OnLostFocus(e); }
protected override void OnTextChanged(EventArgs e) { Invalidate(); Update(); base.OnTextChanged(e); }
protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); this.Invalidate(); this.Update(); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Draw(e.Graphics); }
protected override void OnMouseDown(MouseEventArgs e) { if(e.Button == MouseButtons.Left) { Focus(); Capture = true; buttonState = ButtonState.Pushed; mousePressed = true; Invalidate(); Update(); } else { base.OnMouseDown(e); } }
protected override void OnMouseUp(MouseEventArgs e) { if(mousePressed && e.Button == MouseButtons.Left) { Capture = false; buttonState = ButtonState.Normal; Invalidate(); Update(); } else { base.OnMouseUp(e); } mousePressed = false; }
#endregion
#region Implemenation protected virtual void Draw(Graphics g) { DrawButton(g); if(buttonIcon != null) DrawIcon(g); DrawText(g); if(base.Focused) DrawFocusClues(g); }
protected virtual void DrawButton(Graphics g) { Rectangle rcButton = new Rectangle(0,0,this.Width,this.Height); if(Focused) rcButton.Inflate(-1,-1); ControlPaint.DrawButton(g,rcButton,buttonState); }
protected virtual void DrawText(Graphics g) { int left = (buttonIcon == null ? IconButton.EDGE_PADDING : iconDrawWidth + IconButton.EDGE_PADDING); int width = Width - left; int top = IconButton.EDGE_PADDING; int height = Height - (2*IconButton.EDGE_PADDING);
RectangleF layoutRect = new RectangleF(left,top,width,height);
if(ButtonState.Pushed == buttonState) layoutRect.Offset(1f,1f); StringFormat fmt = new StringFormat(); fmt.Alignment = StringAlignment.Center; fmt.LineAlignment = StringAlignment.Center; SolidBrush textBrush = new SolidBrush(this.ForeColor); g.DrawString(Text,Font,textBrush,layoutRect,fmt); textBrush.Dispose(); }
protected virtual void DrawIcon(Graphics g) { System.Diagnostics.Debug.Assert(buttonIcon != null,"IconButton Icon is null");
int top = ((Height/2) - (buttonIcon.Height/2)); int height = buttonIcon.Height; int width = buttonIcon.Width;
if((top + height) >= Height) { top = IconButton.EDGE_PADDING; int drawHeight = Height - (2*IconButton.EDGE_PADDING); float scale = ((float)drawHeight / (float)height); width = (int)((float)width*scale); height = drawHeight; } Rectangle iconRect = new Rectangle(IconButton.EDGE_PADDING,top,width,height);
if(buttonState == ButtonState.Pushed) iconRect.Offset(1,1);
g.DrawIcon(buttonIcon,iconRect); this.iconDrawWidth = iconRect.Width; }
protected virtual void DrawFocusClues(Graphics g) { System.Drawing.Pen p = new Pen(System.Drawing.Color.Black, 1f); Rectangle frameRect = new Rectangle(0,0,this.Width,this.Height); g.DrawRectangle(p,frameRect); p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; frameRect = new Rectangle(2,2,this.Width - 6,this.Height - 6); if(buttonState == ButtonState.Pushed) frameRect.Offset(1,1); g.DrawRectangle(p,frameRect); p.Dispose(); } #endregion } }
_________________________________________________________________ Send and receive Hotmail on your mobile device: http://mobile.msn.com
|
|
| |
|
| |
| Erik Brown |
Edward,
The code is fine. You are missing a reference. I suspect you need to reference the System.Drawing.dll and System.Windows.Forms.dll assemblies in your project. (Right click on "References" under your project, select "Add Reference...", double click on System.Drawing.dll and System.Windows.Forms.dll, and click OK). Basically, you are missing some required libraries for your code, what .NET calls assemblies.
Such errors are found in any environment or language when you move code around. In .NET, the errors can take a little practice since there are no header files and the coding model is quite different than earlier Windows programming environments (at least from Microsoft...).
Hope you enjoy the book,
Erik Brown ========== Author of "Windows Forms Programming with C#" www.manning.com/eebrown
[Original message clipped]
|
|
| |
|
| |
| Edward Tanguay |
Hi Erik,
1. Fabulous, that was it, I just had to add the reference:
System.Drawing.dll System.Windows.Forms.dll
Why is this actually, I thought that was what the "using ..." statements were for. The adding reference and using statements seem to me to be redundant.
2. I am now having a similar problem: "ControlDesigner" is not being recognized by Intellisense and causes an error when being compiled:
System.Windows.Forms.Design.ControlDesigner
in the following code:
using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Collections; using System.Drawing;
namespace SAMS.Toolkit.Design { public class IconButtonDesigner : System.Windows.Forms.Design.ControlDesigner { public IconButtonDesigner() { }
protected override void PostFilterProperties(IDictionary Properties) { Properties.Remove("BackgroundImage"); Properties.Remove("BackColor"); } } }
Do I need to add another reference for this or another "using"? I've typed it in as it is in the book.
Thanks,
Edward Tanguay http://www.tanguay.info
[Original message clipped]
_________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.
|
|
| |
|
| |
| Erik Brown |
Edward,
Glad this helped. The "using" statement is nothing more than a shortcut so we programmers do not have to type so much. Sort of like writing "St." rather than "Street" on an envelope to save time and space. You can provide a namespace that you do not wish to type, or provide an abbreviation for a class or namespace.
For example:
using System.Windows.Forms; using Btn = System.Windows.Forms.Button;
The first allows to you to use the "Button" class and have it resolve to "System.Windows.Forms.Button," while the second allows to use the string "Btn" in place of the "System.Windows.Forms.Button" class. Of course, the second form can cause confusion for other programmers trying to understand your code, so most of the time people stick with the first form only.
This is very different from a header file as used in C++, where the "#include" statement pulls in the actually library definition so that it can be used in code (without the concept of namespaces, mind you). In .NET, the assembly contains the required header-file-like information, and must therefore be referenced in order to pick up the definitions for use within a file.
Enjoy,
Erik ========== Author of "Windows Forms Programming with C#" www.manning.com/eebrown
[Original message clipped]
[ lines deleted ]
|
|
| |
|
|