- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index
12.3.3PLACING IMAGES ON OUR BUTTONS
We have created the buttons in our PhotoEditDlg form, and created bitmaps for these buttons within our project. The next step is to reconfigure our buttons to display these images. A button can contain both an image and text string, or only an image, or only text. For our example, we will include both the image and text on our buttons.
The steps required are as follows:
ADD THE BITMAP IMAGES TO THE BUTTONS
|
|
Action |
Result |
|||
|
|
|
|
|
|
|
1 |
In the PhotoEditDlg.cs [Design] |
See the graphic in step 2. |
||||
|
window, modify the Next button to |
|
||||
|
display its text on the left side and |
|
||||
|
image on the right side of the button. |
|
||||
|
|
Settings |
|
|||
|
|
|
|
|
|
|
|
|
Property |
Value |
|
||
|
|
ImageAlign |
MiddleRight |
|
||
|
|
TextAlign |
MiddleLeft |
|
||
|
|
|
|
|
|
|
2 |
Assign the NextButton.bmp bitmap to |
|
||||
|
the Image property of the btnNext |
|
||||
|
control. |
|
|
|
|
|
|
How-to |
|
|
|
|
|
|
a. If necessary, save any changes to |
|
||||
|
the NextButton.bmp file. |
|
||||
|
b. Click on the Image item in the Prop- |
|
||||
|
erties window. |
|
|
|
|
|
|
c. Click the … button. |
|
||||
|
d. Locate and open the NextBut- |
|
||||
|
ton.bmp file. |
|
|
|
|
|
|
|
|
|
|
|
|
3 |
Modify the Prev button to display its |
|
||||
|
text on the right side and image on the |
|
||||
|
left side of the button. |
|
||||
|
|
Settings |
|
|||
|
|
|
|
|
|
|
|
|
Property |
Value |
|
||
|
|
ImageAlign |
MiddleLeft |
|
||
|
|
TextAlign |
MiddleRight |
|
||
|
|
|
|
|
|
|
4 |
Assign the PrevButton.bmp bitmap to |
|
||||
|
the Image property of the btnPrev |
|
||||
|
control. |
|
|
|
|
|
|
|
|
|
|
|
|
402 |
CHAPTER 12 A .NET ASSORTMENT |
As you can see, the ImageAlign and TextAlign properties are used to set the location of the image and text within the button area. These take their values from the ContentAlignment enumeration, with all nine possible combinations of Top, Middle, and Bottom with Left, Center, and Right represented. Both properties use the MiddleCenter value by default.
Assigning the Image property is a simple matter of locating the desired file in the file system. The source code generated in the InitializeComponent method by this action is rather interesting. Here is an excerpt of the PhotoEditDlg.cs file.
private void InitializeComponent() |
Load the PhotoEditDlg |
b |
||||
{ |
||||||
|
resources |
|
|
|
||
System.Resources.ResourceManager resources |
|
|
|
|
|
|
|
|
|
|
|
||
= new System.Resources.ResourceManager(typeof(PhotoEditDlg)); |
|
|
||||
|
|
|||||
. . . |
|
|
|
|
|
|
this.btnNext = new System.Windows.Forms.Button(); |
|
|
|
|
||
this.btnPrev = new System.Windows.Forms.Button(); |
|
|
|
|
||
. . . |
|
|
|
|
|
|
// |
|
Load a specific |
|
|
|
|
// btnNext |
|
bitmap resource |
c |
|||
// |
|
|
|
|
|
|
this.btnNext.Image = ((System.Drawing.Bitmap) |
|
|
|
|
|
|
(resources.GetObject("btnNext.Image"))); |
|
|
|
|||
|
|
|
||||
this.btnNext.ImageAlign |
|
|
|
|
|
|
= System.Drawing.ContentAlignment.MiddleRight; |
|
|
|
|
||
. . . |
|
|
|
|
|
|
this.btnNext.Text = "Nex&t"; |
|
|
|
|
|
this.btnNext.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
//
// btnPrev
//
this.btnPrev.Image = ((System.Drawing.Bitmap) (resources.GetObject("btnPrev.Image"))); C
this.btnPrev.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
. . .
}
You will note that the names of our original files, NextButton.bmp and PrevButton.bmp, do not appear in this listing. Instead, these files are encapsulated in a cul- ture-specific resource for our library. A culture-specific resource is a text string, image, and other language or culture-specific object used in an application, library, dialog, or other construct. The term resource is often used as an abbreviation for such objects. A few comments on the previous code are in order:
bVisual Studio creates a resource file specifically for the MyPhotoAlbum project. This file is called PhotoEditDlg.resx and appears in the MyPhotoAlbum project directory. When your program is compiled in Visual Studio, this file is compiled into a
.resources file based on the fully qualified name of the dialog. The resgen.exe compiler is used to generate resource files from the command line. In our application, the
IMAGE BUTTONS |
403 |
.resources file, called Manning.MyPhotoAlbum.PhotoEditDlg.resources, appears in the obj directory under the MyPhotoAlbum project directory, and is included in the final MyPhotoAlbum.dll assembly produced by the compiler. The
ager class is part of the System.Resources namespace, and loads the .resources file for the given object type, in this case the
EditDlg type, so that the specific resources in this file may be accessed.
cA specific resource, in these cases our bitmap files, is loaded from the .resources file using the GetObject method. This returns the object corresponding to the given name, which can safely be typecast to the more appropriate Bitmap class.
The important points to take away from this discussion are that .resx files are used to encapsulate language-specific objects, and get compiled into .resources files for access by a program. These concepts are the basis for localization support in .NET, permitting the resources required to run a program in the United States to be encapsulated and later converted to run the same program in France, New Zealand, or Botswana with language and cultural requirements taken into account.
As long as we’re here, take a quick look at the PhotoEditDlg.resx file. Such files use an XML format to encapsulate resource objects, permitting graphical programs such as Visual Studio as well as text editors like Notepad to view and edit their contents. An excerpt of this file, including the btnNext control’s image definition, is shown as it appears on my computer. We won’t go into the details of XML or the .resx file format here, but it is useful to see how the bitmap for the Next button, named
btnNext.Image, is specified. Be careful not to change these entries, as you may adversely affect your program.
<?xml version="1.0" encoding="utf-8"?>
<root>
. . .
<xsd:schema id="root" . . . >
. . .
</xsd:schema>
. . .
<data name="btnNext.Image" type="System.Drawing.Bitmap, System.Drawing, Version=1.0.3300.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Qk1OAQAAAAAAAHYAAAAoAAAAEgAAABIAAAABAAQAAAAAAAAAAADEDgAAxA4AABAAAAAQAAAAAAAA/wAA
gP8AgAD/AICA/4AAAP+AAID/gIAA/8DAwP+AgID/AAD//wD/AP8A/////wAA//8A/////wD/////////
/////////wASAP///////////wASAP///////////wASAP/////wD////wASAP/////wAP///wASAP//
////AA///wASAP//////8AD//wASAP///////wAP/wASAP8AAAAAAAAA/wASAP8AAAAAAAAA/wASAP//
/////wAP/wASAP//////8AD//wASAP//////AA///wASAP/////wAP///wASAP/////wD////wASAP//
/////////wASAP///////////wASAP///////////wASAA==
</value>
</data>
. . .
</root>
404 |
CHAPTER 12 A .NET ASSORTMENT |