- •Table of Contents
- •C# and the .NET Platform, Second Edition
- •Introduction
- •Part One: Introducing C# and the .NET Platform
- •Part Two: The C# Programming Language
- •Part Three: Programming with .NET Assemblies
- •Part Four: Leveraging the .NET Libraries
- •Part Five: Web Applications and XML Web Services
- •Obtaining This Book's Source Code
- •The .NET Solution
- •What C# Brings to the Table
- •The Role of the Assembly Manifest
- •Summary
- •Chapter 2: Building C# Applications
- •Summary
- •Chapter 3: C# Language Fundamentals
- •Defining Program Constants
- •Defining Custom Class Methods
- •C# Enumerations
- •Summary
- •The Second Pillar: C#'s Inheritance Support
- •Summary
- •Catching Exceptions
- •Finalizing a Type
- •Garbage Collection Optimizations
- •Summary
- •Chapter 6: Interfaces and Collections
- •Building Comparable Objects (IComparable)
- •Summary
- •Summary
- •Internal Representation of Type Indexers
- •Summary
- •An Overview of .NET Assemblies
- •Understanding Delayed Signing
- •Using a Shared Assembly
- •GAC Internals
- •Summary
- •Spawning Secondary Threads
- •A More Elaborate Threading Example
- •Summary
- •Summary
- •Object Persistence in the .NET Framework
- •The .NET Remoting Namespaces
- •Understanding the .NET Remoting Framework
- •All Together Now!
- •Terms of the .NET Remoting Trade
- •Testing the Remoting Application
- •Revisiting the Activation Mode of WKO Types
- •Deploying the Server to a Remote Machine
- •Summary
- •Control Events
- •The Form Class
- •Summary
- •Regarding the Disposal of System.Drawing Types
- •Understanding the Graphics Class
- •Summary
- •The TextBox Control
- •Working with Panel Controls
- •Configuring a Control's Anchoring Behavior
- •Summary
- •Chapter 16: The System.IO Namespace
- •The Static Members of the Directory Class
- •The Abstract Stream Class
- •Summary
- •The Role of ADO.NET Data Providers
- •The Types of System.Data
- •Selecting a Data Provider
- •The Types of the System.Data.OleDb Namespace
- •Working with the OleDbDataReader
- •Summary
- •Submitting the Form Data (GET and POST)
- •Some Benefits of ASP.NET
- •Creating an ASP.NET Web Application by Hand
- •The Composition of an ASP.NET Page
- •The Derivation of an ASP.NET Page
Control EventsC# and the .NET Platform, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
The Control class also defines a number of events that can logically be grouped into two major categories:
Apress © 2003 (1200 pages)
Mouse events and keyboard events (Table 13-7).
This comprehensive text starts with a brief overview of the
C# language and then quickly moves to key technical and
architectural issues for .NET developers.
Table 13-7: Events of the Control Type
Control Event
Meaning in Life
Table of Contents
Click, DoubleClick, MouseEnter, MouseLeave,
C# and the .NET Platform, Second Edition
MouseDown, MouseUp, MouseMove, MouseHover,
Introduction
MouseWheel
Part One - Introducing C# and the .NET Platform
ChapterKeyPress,1 - TheKeyUp,PhilosophyKeyDownof .NET
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
The Control class defines numerous events triggered in response to mouse input.
The Control class also defines numerous events triggered in response to keyboard input.
Chapter 3 - C# Language Fundamentals
Chapter 4 - Object-Oriented Programming with C#
ChapterFun 5with- Exceptionsthe Controland ObjectClassLifetime
Chapter 6 - Interfaces and Collections
To be sure, the Control class does define additional properties, methods, and events beyond the subset you
Chapter 7 - Callback Interfaces, Delegates, and Events
have just examined. However, to illustrate some of these core members, let's build a new Form type (also
Chapter 8 - Advanced C# Type Construction Techniques
called MainForm) that provides the following functionality:
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
Set the initial size of the Form to some arbitrary dimensions.
Chapter 10 - Processes, AppDomains, Contexts, and Threads
ChapterOverride11 - Typethe Reflection,Dispose() methodLate Binding,. and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Respond to the MouseMove and MouseUp events (using two approaches).
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Capture and process keyboard input.
Chapter 14 - A Better Painting Framework (GDI+)
ChapterTo begin,15 assume- Programmingyou havewitha Windowsnew C# classFormsderivedControlsfrom Form. First, update the default constructor to set
Chapthe top,er 16left,- Thebottom,Systemand.IOrightNamespacecoordinates of the Form using various properties of the Control class. To
Chapterconfirm17these- Datachanges,Acc ss withmakeADOuse.NETof the Bounds property, and display the string version of the current
PartdimensionsFive - Web. DoApplicationsbe aware thatandBoundsXML WebreturnsServicesa Rectangle type that is defined in the System.Drawing Chnamespacept r 18 - .ASPTherefore.NET WebbePagessure andto setWebanControlsassembly reference (to System.Drawing.dll) if you are building this
Form by hand (Visual Studio .NET Windows Forms projects do so automatically):
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index// Need this for Rectangle definition.
Listusingof FiguresSystem.Drawing;
...
List of Tables
public class MainForm : Form
{
public static int Main(string[] args)
{
Application.Run(new MainForm()); return 0;
}
public MainForm()
{
Top = 100;
Left = 75;
Height = 100;
Width = 500;
MessageBox.Show(Bounds.ToString(), "Current rect");
}
C# and the .NET Platform, Second Edition
}
by Andrew Troelsen |
ISBN:1590590554 |
Apress © 2003 (1200 pages)
When you run thisThisapplication,comprehensiveyou aret xtablestartsto confirmwith a briefthe overviewcoordinatesof theof your Form via the Bounds property. C# language and then quickly moves to key technical and
architectural issues for .NET developers.
Now, let's retrofit your class to override the inherited Dispose() method (this is done automatically when using VS .NET). As you recall from earlier in this chapter, the Application object defines an event named
TableApplicationExitof C ntents. If you configure your Form to intercept this event, you are effectively informed of the C#destructionand the .NETof thePlatform,applicationSecond. AsEditiona (much) simpler alternative, you can achieve the same effect by simply
Introductionoverriding the Dispose() method. Do note that you should call your base class' Dispose() method before
Partexiting:One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
protected override void Dispose( bool disposing )
Part Two - The C# Programming Language
{
Chapter 3 - C# Language Fundamentals
MessageBox.Show("Disposing this Form");
Chapterif(4 -disposingObject-Oriented) Programming with C#
Chapter{5 - Exceptions and Object Lifetime
Chapter 6 -ifInterfaces(componentsa d Collections!= null)
Chapter 7 -{Callback Interfaces, Delegates, and Events
components.Dispose();
Chapter 8 - Advanced C# Type Construction Techniques
}
Part Three - Programming with .NET Assemblies
Chapter |
9 |
-}Understanding .NET Assemblies |
Chapter |
10 |
-base.Dispose(Processes, AppDomains,disposingContexts,);and Threads |
Chapter} |
11 |
- Type Reflection, Late Binding, and Attribute-Based Programming |
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
ChapterResponding13 - Bu ldingtoa BetterMouseWindowEvents:(IntroducingTakeWindowsOneForms)
Chapter 14 - A Better Painting Framework (GDI+)
Next, you need to intercept the MouseUp event. The goal is to display the (x,y) position at which the
Chapter 15 - Programming with Windows Forms Controls
MouseUp event occurred. When you wish to respond to events from within a Windows Forms application,
Chapter 16 - The System.IO Namespace
you have two general approaches. The first approach should be familiar to you at this point in the game: Use delegates. The second approach is to override the appropriate base class method. Let's examine each
technique, beginning with standard delegation. Here is the updated MainForm:
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
public class MainForm : Form
Chapter 20 - XML Web Services
{
Index
public static int Main(string[] args)
List of Figures
{
List of Tables
Application.Run(new MainForm()); return 0;
}
public MainForm()
{
...
// Listen for the MouseUp event...
this.MouseUp += new MouseEventHandler(OnMouseUp);
}
// Method called in response to the MouseUp event.
public void OnMouseUp(object sender, MouseEventArgs e)
{
this.Text = string.Format("Current Pos: ({0}, {1})", e.X, e.Y);
}
...
}
C# and the .NET Platform, Second Edition
Now, recall that GUI-based delegates take an EventArgs (or derivativeISBN:1590590554thereof) as the second parameter. by Andrew Troelsen
When you process mouse events, the second parameter is of type MouseEventArgs. This type (defined in
Apress © 2003 (1200 pages)
the System.Windows.Forms namespace) defines a number of interesting properties that may be used to
This comprehensive text starts with a brief overview of the gather various statisticsC# languageregardingand ttheenstatequicklyof themovesmouse,to keyastechnicalseen inandTable 13-8.
architectural issues for .NET developers.
Table 13-8: Properties of the MouseEventArgs Type
|
|
|
|
|
|
|
|
TableMouseEventArgsof Contents |
|
|
Meaning in Life |
|
|
||
C#Propertyand the .NET Platform, Second |
|
Edition |
|
|
|||
|
|
|
|
|
|
|
|
|
Introduction |
|
|
Gets which mouse button was pressed, as defined by the |
|
|
|
|
Button |
|
|
|
|
|
|
Part One - Introducing C# and the |
|
.NET Platform |
|
|
|||
|
|
|
|
|
MouseButtons enumeration |
|
|
|
Chapter 1 - The Philosophy of .NET |
|
|
|
|
||
|
ChapterClicks2 - Building C# Applications |
Gets the number of times the mouse button was pressed and |
|
|
|||
Part Two - The C# Programming |
Languagereleased |
|
|
||||
|
|
|
|
|
|||
|
Chapter 3 - C# Language Fundamentals |
|
|
||||
|
Delta |
|
|
|
Gets a signed count of the number of detents the mouse wheel |
|
|
|
Chapter 4 |
- Object-Oriented Programming with C# |
|
|
|||
|
|
|
|
|
has rotated |
|
|
|
Chapter 5 - Exceptions and Object |
|
Lifetime |
|
|
||
|
|
||||||
|
X |
|
|
|
Gets the x-coordinate of a mouse click |
|
|
|
Chapter 6 - Interfaces and Collections |
|
|
||||
|
|||||||
|
Chapter 7 - Callback Interfaces, |
|
Delegates, and Events |
|
|
||
|
Y |
|
|
|
Gets the y-coordinate of a mouse click |
|
|
Chapter 8 |
- Advanced C# Type Construction Techniques |
Part Three - Programming with .NET Assemblies |
|
The implementation of the OnMouseUp() method simply extracts the (x, y) position of the cursor and |
|
Chapter 9 |
- U derst nding .NET Assemblies |
displays this information in the Form's caption via the inherited Text property. |
|
Chapter 10 |
- Processes, AppDomains, Contexts, and Threads |
To make things even more interesting, we could also capture a MouseMove event, and display the same (x,
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
y) position data in the caption of the Form. In this way, the current location of the cursor is tracked whenever
Part Four - Leveraging the .NET Libraries
the mouse cursor is moved within the client area:
Chapter 12 - Object Serialization and the .NET Remoting Layer Chapter 13 - Building a Better Window (Introducing Windows Forms)
public class MainForm : Form
Chapter 14 - A Better Painting Framework (GDI+)
{
Chapter 15 - Programming with Windows Forms Controls
...
Chapter 16 - The System.IO Namespace
public MainForm()
Chapter 17 - Data Access with ADO.NET
Part Five{- Web Applications and XML Web Services
...
Chapter 18 - ASP.NET Web Pages and Web Controls
// Track mouse movement and MouseUp event.
Chapter 19 - ASP.NET Web Applications
this.MouseUp += new MouseEventHandler(OnMouseUp);
Chapter 20 - XML Web Services
this.MouseMove += new MouseEventHandler(OnMouseMove);
Index
}
List of Figures
public void OnMouseUp(object sender, MouseEventArgs e)
List of Tables
{
// Now we will just show a message when clicked. MessageBox.Show("Stop clicking me!");
}
public void OnMouseMove(object sender, MouseEventArgs e)
{
this.Text = string.Format("Current Pos: ({0}, {1})", e.X, e.Y);
}
}
Determining Which Mouse Button Was Clicked
One thing to be aware of is that the MouseUp (or MouseDown) event is sent whenever any mouse button is clicked. If you wish to determine exactly which button was clicked (left, right, or middle) you need to examine the Button property of the MouseEventArgs class. The value of Button is constrained by the
MouseButtons enumeration. For example:
C# and the .NET Platform, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
public void OnMouseUp(object sender, MouseEventArgs e)
Apress © 2003 (1200 pages)
{
This comprehensive text starts with a brief overview of the
// Which mouse button was clicked?
C# language and then quickly moves to key technical and
if(e.Button == MouseButtons.Left) architectural issues for .NET developers.
MessageBox.Show("Left click!");
if(e.Button == MouseButtons.Right)
Table of ContentsMessageBox.Show("Right click!");
if (e.Button == MouseButtons.Middle)
C# and the .NET Pla form, Second Edition
Introduction MessageBox.Show("Middle click!");
}
Part One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
PartRespondingTwo - The C# Programmingto MouseLanguageEvents: Take Two
Chapter 3 - C# Language Fundamentals
The previous mouse event logic followed the standard .NET delegate pattern. The other approach to
Chapter 4 - Object-Oriented Programming with C#
capture events in a Control-derived type is to override the correct base class method, which in this case
Chapter 5 - Exceptions and Object Lifetime
would be OnMouseUp() and OnMouseMove(). The Control type defines a number of protected virtual
Chapter 6 - Interfaces and Collections
methods that will be called automatically when the corresponding event is triggered. If you were to update
Chapter 7 - Callback Interfaces, Delegates, and Events
your Form using this technique, you have no need to manually specify a custom event handler (or write the
Chapter 8 - Advanced C# Type Construction Techniques
necessary event handling logic):
Part Three - Programming with .NET Assemblies
Chapter |
9 - Understanding .NET Assemblies |
||
Chapterpublic10class- Processes,MainFormAppDomains,: FormContexts, and Threads |
|||
Chapter{ |
11 |
- Type Reflection, Late Binding, and Attribute-Based Programming |
|
Part Four...- Leveraging the .NET Libraries |
|||
Chapter public12 - ObjectMainForm()Serialization and the .NET Remoting Layer |
|||
Chapter {13 |
- Building a Better Window (Introducing Windows Forms) |
||
Chapter |
14 |
|
... |
- A Better Painting Framework (GDI+) |
|||
Chapter |
15 |
|
// No need to do this when overriding! |
- Programming with Windows Forms Controls |
|||
Chapter |
16 |
|
// this.MouseUp += new MouseEventHandler(OnMouseUp); |
- The System.IO Namespace |
|||
|
|
|
// this.MouseMove+= new MouseEventHandler(OnMouseMove); |
Chapter |
17 |
- Data Access with ADO.NET |
|
|
} |
|
|
Part Five - Web Applications and XML Web Services |
|||
|
protected override void OnMouseUp(/*object sender,*/ MouseEventArgs e) |
||
Chapter |
18 |
- ASP.NET Web Pages and Web Controls |
|
|
{ |
|
|
Chapter |
19 |
- ASP.NET Web Applications |
|
|
|
|
// Which mouse button was clicked? |
Chapter |
20 |
- XML Web Services |
|
|
|
|
if(e.Button == MouseButtons.Left) |
Index |
|
|
MessageBox.Show("Left click!"); |
List of Figures |
if(e.Button == MouseButtons.Right) |
||
List of Tables |
MessageBox.Show("Right click!"); |
||
|
|
|
if(e.Button == MouseButtons.Middle) |
|
|
|
MessageBox.Show("Middle click!"); |
|
} |
|
base.OnMouseUp(e); |
|
|
|
protected override void OnMouseMove(/*object sender,*/ MouseEventArgs e) { this.Text = string.Format("Current Pos: ({0}, {1})", e.X, e.Y); }
}
Notice how the signature of each method takes a single parameter of type MouseEventArg, rather than two parameters that conform to the MouseEventHandler delegate. If you run the program again, you see no change whatsoever (which is good).
Recall that these virtual methods defined by your base types are simply the default event handlers that will be called if you do not explicitly handle a given event. Typically you only need to override an "OnXXXX()" method if you have additional work to perform before calling your parent's implementation. The preferred
approach (and the one used by Visual Studio .NET) is to handle the event directly as you did in the first
C# and the .NET Platform, Second Edition
mouse example, and make use of the .NET event syntax. Given this, I will refrain from overriding base class
by Andrew Troelsen ISBN:1590590554
GUI event handlers during the remainder of this text.
Apress © 2003 (1200 pages)
This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and architectural issues for .NET developers.
Table of Contents
C# and the .NET Platform, Second Edition
Introduction
Part One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
Chapter 3 - C# Language Fundamentals
Chapter 4 - Object-Oriented Programming with C#
Chapter 5 - Exceptions and Object Lifetime
Chapter 6 - Interfaces and Collections
Chapter 7 - Callback Interfaces, Delegates, and Events
Chapter 8 - Advanced C# Type Construction Techniques
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
RespondingC#toandKeyboardthe .NET Platform,EventsSecond Edition
by Andrew Troelsen |
ISBN:1590590554 |
Processing keyboard input is almost identical to responding to mouse activity. The following code captures
Apress © 2003 (1200 pages)
the KeyUp event and displays the textual name of the character that was pressed in a message box. Here,
This comprehensive text starts with a brief overview of the
you capture this eventC# languausingetheanddelegationth n quicklytechniquemoves to(therek y technicalis a methodand named OnKeyUp() that can be overridden as anarchitecalternative):ural issues for .NET developers.
public class MainForm : Form
Table of Contents
{
C# and the .NET Platform, Second Edition
...
Introduction
public MainForm()
Part One{- Introducing C# and the .NET Platform
Chapter 1 |
- The Philosophy of .NET |
|
|
|
... |
Chapter 2 |
- Building C# Applications |
|
|
|
// Listen to KeyUp Event. |
Part Two - The C# Programming Language |
||
|
|
this.KeyUp += new KeyEventHandler(OnKeyUp); |
Chapter 3 |
- C# Language Fundamentals |
|
|
} |
|
Chapter 4 |
- Object-Oriented Programming with C# |
|
|
public void OnKeyUp(object sender, KeyEventArgs e) |
|
Chapter 5 |
- Exceptions and Object Lifetime |
|
|
{ |
- InterfacesMessageBoxand Collections.Show(e.KeyCode.ToString(), "Key Pressed!"); |
Chapter 6 |
||
Chapter }7 |
- Callback Interfaces, Delegates, and Events |
|
Chapter} |
8 |
- Advanced C# Type Construction Techniques |
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
As you can see, the KeyEventArgs type maintains an enumeration named KeyCode that holds the ID of
Chapter 10 - Processes, AppDomains, Contexts, and Threads
the key press. In addition, the KeyEventArgs type, defines the useful properties listed in Table 13-9.
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Table 13-9: Properties of the KeyEventArgs Type
|
Chapter 12 - Object Serialization and the .NET Remoting Layer |
|
|
||
|
ChapterKeyEventArgs13 - Building a BetterMeaningWi dow (Introducingin Life Windows Forms) |
|
|
||
|
ChapterProperty14 - A Better Painting Framework (GDI+) |
|
|
||
|
|
|
|
|
|
|
Chapter 15 - Programming |
with Windows Forms Controls |
|
||
|
Alt |
|
Gets a value indicating whether the Alt key was pressed |
|
|
|
Chapter 16 - The System.IO |
|
Namespace |
|
|
|
|
|
|
||
|
ChapterControl17 - Data Access withGetsADOa.NETvalue indicating whether the Ctrl key was pressed |
|
|
||
|
|
|
|
|
|
Part Five - Web Applications |
|
and XML Web Services |
|
|
|
|
Handled |
|
Gets or sets a value indicating whether the event was handled |
|
|
|
Chapter 18 - ASP.NET Web |
|
Pages and Web Controls |
|
|
ChapterKeyCode19 - ASP.NET Web ApplicationsGets the keyboard code for a System.Windows.Forms.Control.KeyDown Chapter 20 - XML Web Servicesor System.Windows.Forms.Control.KeyUp event
|
|
|
|
Index |
|
Gets the key data for a System.Windows.Forms.Control.KeyDown or |
|
|
KeyData |
|
|
List of Figures |
|
System.Windows.Forms.Control.KeyUp event |
|
|
|
|
|
List of Tables |
|
|
|
|
|
||
|
Modifiers |
|
Indicates which modifier keys (Ctrl, Shift, and/or Alt) were pressed |
|
|
|
|
|
Shift |
|
Gets a value indicating whether the Shift key was pressed |
Figure 13-11 shows a possible key press.
Figure 13-11: Intercepting key presses
SOURCE
CODE
C# andThe theControlBehaviors.NET Pl tf rm,projectSecondis includedEdition under the Chapter 13 subdirectory.
by Andrew Troelsen |
ISBN:1590590554 |
Apress © 2003 (1200 pages)
This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and architectural issues for .NET developers.
Table of Contents
C# and the .NET Platform, Second Edition
Introduction
Part One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
Chapter 3 - C# Language Fundamentals
Chapter 4 - Object-Oriented Programming with C#
Chapter 5 - Exceptions and Object Lifetime
Chapter 6 - Interfaces and Collections
Chapter 7 - Callback Interfaces, Delegates, and Events
Chapter 8 - Advanced C# Type Construction Techniques
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
The ControlC#Classand theRevisited.NET Pla form, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
The Control class defines further behaviors to configure background and foreground colors, background
Apress © 2003 (1200 pages)
images, font characteristics, drag-and-drop functionality and support for pop-up context menus. This class
This comprehensive text starts with a brief overview of the
provides dockingC#andlanguageanchoringandbehaviorsthen quicklyformovesthe derivedto keytypestechnical(whichandyou examine in Chapter 15). Perhaps the mostarchitecturalimportant dutyissuesof forthe.ControlNET developersclass is. to establish a mechanism to render images, text, and various geometric patterns onto the client area via a registered Paint event handler. To begin, observe these additional properties of the Control class, as seen in Table 13-10.
Table of Contents
C# and the .NET Platform, Second Edition |
|||||||
|
Table 13-10: Additional Control Properties |
||||||
|
|
Introduction |
|
|
|
|
|
PartControlOne - IntroducingPropertyC# and theMeaning.NET Platformin Life |
|
|
|||||
|
|
|
|
|
|
|
|
|
|
Chapter 1 |
- The Philosophy of |
.NET |
|
|
|
|
|
AllowDrop |
|
If AllowDrop is set to true then this control allows drag-and-drop |
|
|
|
|
Chapter 2 |
- Building C# Applications |
|
|
|||
|
|
|
|
|
operations and events to be used. |
|
|
Part Two - The C# Programming |
|
Language |
|
|
|||
|
|||||||
ChapterAnchor3 |
- C# Language FundamentalsThe anchor property determines which edges of the control are |
|
|
||||
|
|
Chapter 4 |
- Object-Oriented |
|
anchored to the container's edges. |
|
|
|
|
Programming with C# |
|
|
|||
|
|
|
|
|
|
|
|
|
|
Chapter 5 |
- Exceptions and Object Lifetime |
|
|
||
|
|
BackColor, |
|
These properties configure how the client area should be |
|
|
|
|
Chapter 6 |
- Interfaces and Collections |
|
|
|||
|
|
BackgroundImage, Font, |
|
displayed. |
|
|
|
|
Chapter 7 |
- Callback Interfaces, Delegates, and Events |
|
|
|||
|
|
ForeColor, Cursor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Chapter 8 |
- Advanced C# Type |
Construction Techniques |
|
||
|
|
ContextMenu |
|
Specifies which context menu (e.g., pop-up menu) will be shown |
|
|
|
Part Three - Programming with |
|
.NET Assemblies |
|
|
|||
|
|
|
|
|
when the user right-clicks the control. |
|
|
|
Chapter 9 |
- Understanding .NET |
|
Assemblies |
|
|
|
|
Chapter 10 |
- Processes, AppDomains, C ntexts, and Threads |
|
|
|||
|
|
Dock |
|
|
The dock property controls which edge of the container this control |
|
|
|
Chapter 11 |
- Type Reflection, LatedocksBinding,to.andForAttributeexample,-BasedwhenPrdockedgrammingto the top of the container, the |
|
|
|||
Part Four - Leveraging the .NET Librariescontrol is displayed flush at the top of the container, extending the |
|
|
|||||
Chapter 12 |
- Object Serialization andlengththe of.NETtheRemotingcontainerLayer. |
|
|
||||
|
|
|
|
|
|
|
|
|
|
Chapter 13 |
- Building a Better |
Window (Introducing Windows Forms) |
|
|
|
|
|
Opacity |
- A Better Painting |
|
Determines the opacity of the control, in percentages (0.0 is |
|
|
|
Chapter 14 |
Framework (GDI+) |
|
|
|||
|
|
|
|
|
completely transparent, 1.0 is completely opaque). |
|
|
|
|
Chapter 15 |
- Programming with |
|
Windows Forms Controls |
|
|
|
|
|
|||||
|
ChapterRegion16 |
- The System.IO NamespaceThis property configures a Region object that specifies the |
|
|
|||
|
|
Chapter 17 |
- Data Access with |
|
outline/silhouette/boundary of the control. |
|
|
|
|
ADO.NET |
|
|
|||
|
|
|
|
|
|
||
Part Five - Web Applications and |
|
XML Web Services |
|
|
|||
|
|
RightToLeft |
|
This is used for international applications where the language is |
|
|
|
Chapter 18 |
- ASP.NET Web Pages |
|
and Web Controls |
|
|
||
|
|
|
|
|
written from right to left. |
|
|
|
Chapter 19 |
- ASP.NET Web Applications |
Chapter 20 - XML Web Services
The Control class also defines a number of additional methods and events used to configure how the
Index
Control should respond to drag-and-drop operations and respond to painting operations (Table 13-11).
List of Figures
List of Tables
Table 13-11: Additional Control Methods
|
Control |
C# and the |
|
.MeaningNET Platform,in LifeSecond Edition |
|
|
Method/Eventby Andrew |
|
Troelsen |
ISBN:1590590554 |
|
|
|
|
|
|
|
|
DoDragDrop() |
Apress © 2003 |
(1200 pages) |
|
|
|
|
|
These methods are used to monitor drag-and-drop operations for a |
||
|
|
This comprehensive text starts with a brief overview of the |
|||
|
OnDragDrop() |
|
|
given Control descendent. |
|
|
C# language and then quickly moves to key technical and |
||||
|
OnDragEnter() architectural issues for .NET developers. |
|
|||
|
OnDragLeave() |
|
|||
Table of Contents |
|
|
|
|
|
|
OnDragOver() |
|
|
|
|
|
|
|
|
|
|
C# and the .NET Platform, Second Edition |
|
||||
|
ResetFont() |
|
|
These methods reset various UI attributes of a child control to the |
|
Introduction |
|
|
corresponding value of the parent. |
||
|
|
|
|
||
PartResetCursor()One - Introducing C# and the .NET Platform |
|
||||
Chapter 1 - The Philosophy of .NET |
|
||||
|
ResetForeColor() |
|
|||
Chapter 2 - Building C# Applications |
|
||||
|
ResetBackColor() |
|
|||
Part Two - The C# Programming |
|
Language |
|
ChapterOnPaint()3 - C# Language FundamInheritingntals classes should override this method to handle the Paint
event.
Chapter 4 - Object-Oriented Programming with C#
Chapter 5 - Exceptions and Object Lifetime
DragEnter These events are sent in response to drag-and-drop operations.
Chapter 6 - Interfaces and Collections
DragLeave
Chapter 7 - Callback Interfaces, Delegates, and Events
DragDrop
Chapter 8 - Advanced C# Type Construction Techniques
|
DragOver |
||||
Part Three - Programming with .NET Assemblies |
|||||
|
|
|
|
|
|
|
Chapter 9 |
- Understanding .NET Assemblies |
|
||
|
Paint |
|
|
This event is sent whenever the Control has become "dirty" and |
|
|
Chapter 10 |
- Processes, AppDomains, Contexts, and Threads |
|
||
|
|
|
|
needs to be repainted. |
|
|
Chapter 11 |
- Type Reflection, |
|
Late Binding, and Attribute-Based Programming |
|
Part Four - Leveraging the .NET Libraries |
|||||
Chapter 12 |
- Object Serialization and the .NET Remoting Layer |
||||
ChapterMore13Fun- Buildingwitha Betterthe WindowControl(IntroducingClassWindows Forms) |
|||||
Chapter 14 |
- A Better Painting Framework (GDI+) |
To illustrate some of these additional Control members, the following class sets the background color of the Form object to "Tomato" (you just have to love the names of these colors), the opacity to 50 percent
(to generate a semi-transparent main window), and configures the mouse cursor to display an hourglass
Chapter 17 - Data Access with ADO.NET
icon. More important, let's handle the Paint event in order to render a text string into the Form's client area.
Part Five - Web Applications and XML Web Services
Here is the update:
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
using System;
Chapter 20 - XML Web Services
using System.Windows.Forms;
Index
using System.Drawing; // Needed for Color, Brush, and Font types.
List of Figures
List of Tables
public class MainForm : Form
{
...
public MainForm()
{
// Set some properties that we have inherited from Control.
BackColor = Color.Tomato;
Opacity = 0.5d;
this.Cursor = Cursors.WaitCursor;
// Handle the Paint event.
this.Paint += new PaintEventHandler(MainForm_Paint);
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics; g.DrawString("What a head trip...",
new Font("Times New Roman", 20),
|
C# and the .NET Platform, Second Edition |
|
|
new SolidBrush(Color.Black), 40, 10); |
|
} |
by Andrew Troelsen |
ISBN:1590590554 |
|
|
}Apress © 2003 (1200 pages)
This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and
architectural issues for .NET developers.
If you run this application you will see that the Form is indeed transparent!
Painting Basics
Table of Contents
C# and the .NET Platform, Second Edition
The most important aspect of this application is the handling of the Paint event. Notice that the delegate
Introduction
defines a method that takes a parameter of type PaintEventArgs. This type defines two properties to help
you configure the current paint session for the Control as seen in Table 13-12.
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
Table 13-12: Additional Control Properties
Part Two - The C# Programming Language
ChapterPaintEventArgs3 - C# LanguagePropertyFundamentals Meaning in Life
Chapter 4 - Object-Oriented Programming with C#
ClipRectangle |
Gets the rectangle in which to paint |
Chapter 5 - Exceptions and Object Lifetime
ChapterGraphics6 - Interfaces and Collections Gets the Graphics object used during a paint session
Chapter 7 - Callback Interfaces, Delegates, and Events
ChapterThe critical8 - propertyAdvancedofC#PaintEventArgsType Constructionis Graphics,Tec niqueswhich is called to retrieve a Graphics object to use
PartduringThreethe-paintingProgrammingsessionwith. You.NETexamineAssembliesthis class (and GDI+ in general) in greater detail in Chapter 14. ChapterFor now,9 do- Understandingunderstand that.NETtheAssembliesGraphics class defines a number of members that allow you to render
text, geometric shapes and images onto a Control-derived type.
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Finally, in this example you also configured the Cursor property to display an hour-glass symbol whenever the mouse cursor is within the bounding rectangle of this Control. The Cursors type can be assigned to
any member of the Cursors enumeration (e.g., Arrow, Cross, UpArrow, Help, and so forth):
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
public MainForm()
Chapter 15 - Programming with Windows Forms Controls
{
Chapter 16 - The System.IO Namespace
...
Chapter 17 - Data Access with ADO.NET this.Cursor = Cursors.WaitCursor;
Part Five - Web Applications and XML Web Services
}
Chapter 18 - ASP.NET Web Pages and Web Controls Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
IndexSOURCE The MoreControlBehaviors project is included under the Chapter 13 subdirectory.
List ofCODEFigures
List of Tables
The ScrollableControlC# and the .NET Platform,Class Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
ScrollableControl is used to define a small number of members that allow your widget to support vertical
Apress © 2003 (1200 pages)
and horizontal scrollbars. The most intriguing members of the ScrollableControl type would have to be the
This comprehensive text starts with a brief overview of the
AutoScroll propertyC#andlanguagethe relatedand thenAutoScrollMinSizequickly moves topropertykey technical. For example,and assume you wish to ensure that if the end usera chitecturalresizes yourissuesForm,forhorizontal.NET developersand vertical. scrollbars are automatically inserted if the size of the client area is less than or equal to 300×300 pixels. Programmatically, your task is simple:
Table of Contents
// Note that you need to reference the System.Drawing namespace
C# and the .NET Platform, Second Edition
// to gain access to the Size type.
Introduction this.AutoScroll = true;
Part One - Introducing C# and the .NET Platform
this.AutoScrollMinSize= new System.Drawing.Size (300, 300);
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
PartTheTwoScrollableControl- The C# Programmingclass takesLanguagecare of the rest. For example, if you had a Form that contained a
Chapternumber3of-childC# Languageobjects (buttons,F ndamentalslabels, or whatnot), you would find that the scrolling logic ensures the ChaptentirerForm4 - Objectreal estate-Ori ntedis viewableProgramming. For thewithcurrentC# example, simply render a large block of text onto a
Label object (see Figure 13-12).
Chapter 5 - Exceptions and Object Lifetime
Chapter 6 - Interfaces and Collections
Chapter
Chapter
Part
Chapter |
|
Chapter |
and Threads |
Chapter |
Attribute-Based Programming |
Part |
|
Chapter |
Remoting Layer |
Chapter |
Windows Forms) |
ChapterFigure14 - 13A Better-12: ScrollPaintingFormFrameworkGUI |
(GDI+) |
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
At runtime, the Form will automatically display vertical and horizontal scrollbars when any part of the Label
Chapter 17 - Data Access with ADO.NET
is not visible. The ScrollableControl class does define a number of additional members beyond AutoScroll
Part Five - Web Applications and XML Web Services
and AutoScrollMinSize, but not many. Also be aware that when you wish to take greater control over the
Chapter 18 - ASP.NET Web Pages and Web Controls
scrolling process, you are able to create and manipulate individual ScrollBar types (such as HScrollBar
Chapter 19 - ASP.NET Web Applications
and VScrollBar). I'll leave it to you to check out the remaining members using online Help.
Chapter 20 - XML Web Services
IndexSOURCE The ScrollForm project is included under the Chapter 13 subdirectory.
CODE
List of Figures
List of Tables
ContainerControlC# and theClass.NET Platform, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
ContainerControl defines support to manage the focus of a given GUI item. In practice, the behavior
Apress © 2003 (1200 pages)
defined by System.Windows.Forms.ContainerControl is much more useful when you are building a Form
This comprehensive text starts with a brief overview of the
that contains a numberC# languageof childandcontrols,then quicklyand wishmovesto allowto keythetechnicaluser toanduse the Tab key to alternate focus. Using a small setarchitof members,ctural issuesyou canfor .programmaticallyNET developers. obtain the currently selected control, force another to receive focus, and so forth. Table 13-13 gives a rundown of some of the more interesting members.
Table of Contents
C# and the .NET Platform, Second Edition |
|||||||||
|
|
Table 13-13: Members of the ContainerControl Type |
|||||||
|
|
|
Introduction |
|
|
|
|
|
|
PartContainerControlOne - Introducing C# and theMeaning.NET Platformin Life |
|
|
|||||||
|
ChapterMember1 - The Philosophy of |
.NET |
|
|
|||||
|
|
|
|
|
|
||||
|
|
|
Chapter 2 - Building C# Applications |
|
|
||||
|
|
|
ActiveControl ParentForm |
|
|
These properties allow you to obtain and set the active control, as |
|
|
|
Part Two - The C# Programming Language |
|
|
|||||||
|
|
|
|
|
|
|
well as retrieve a reference to the Form that is hosting the item. |
|
|
|
Chapter 3 |
- C# Language Fundamentals |
|
|
|||||
|
|
|
|
||||||
|
|
|
ProcessTabKey() |
|
|
This method allows you to programmatically activate the Tab key to |
|
|
|
|
Chapter 4 |
- Object-Oriented Programming with C# |
|
|
|||||
|
|
|
|
|
|
|
set focus to the next available control. |
|
|
|
Chapter 5 |
- Exceptions and Object Lifetime |
|
|
|||||
|
|
|
|
||||||
Chapter 6 |
- Interfaces and Collections |
On a related note, recall that all descendents of System.Windows.Forms.Control inherit the TabStop and
Chapter 7 - Callback Interfaces, Delegates, and Events
TabIndex properties. As you might be able to guess, these items are used to set the tab order of controls maintained by a parent container, and are used in conjunction with the members supplied by the
ContainerControl class. You revisit the issue of tab order during the discussion of programming controls
Chapter 9 - Understanding .NET Assemblies
(Chapter 15).
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables