You’re writing a book reading list application. You’ve already created a main menu and coded event handlers to handle adding and removing book items and changing the book summary font size. Now you want to add tool tips to the book list and text boxes.
Add a ToolTip component to the BooksForm and name it toolTips. Then set the following tooltips:
| titleTextBox |
Enter the title of the book |
| authorTextBox |
Enter the author of the book |
| summaryTextBox |
Enter a summary of the book |
| booksListBox |
Click a book to view its details |
Run the program to verify your code. You should see the appropriate tool tip when you hover over the book list and edit boxes.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Books
{
///
/// Summary description for Form1.
///
public class BooksForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox booksListBox;
private System.Windows.Forms.Panel bookPanel;
private System.Windows.Forms.Label titleLabel;
private System.Windows.Forms.TextBox titleTextBox;
private System.Windows.Forms.Label authorLabel;
private System.Windows.Forms.TextBox authorTextBox;
private System.Windows.Forms.Label summaryLabel;
private System.Windows.Forms.TextBox summaryTextBox;
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.MenuItem fileMenuItem;
private System.Windows.Forms.MenuItem editMenuItem;
private System.Windows.Forms.MenuItem viewMenuItem;
private System.Windows.Forms.MenuItem helpMenuItem;
private System.Windows.Forms.MenuItem aboutMenuItem;
private System.Windows.Forms.MenuItem smallFontMenuItem;
private System.Windows.Forms.MenuItem normalFontMenuItem;
private System.Windows.Forms.MenuItem largeFontMenuItem;
private System.Windows.Forms.MenuItem deleteMenuItem;
private System.Windows.Forms.MenuItem newMenuItem;
private System.Windows.Forms.MenuItem sepMenuItem;
private System.Windows.Forms.MenuItem exitMenuItem;
private System.Windows.Forms.ToolTip toolTips;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private int lastSelectedIndex = -1;
public BooksForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.summaryTextBox = new System.Windows.Forms.TextBox();
this.summaryLabel = new System.Windows.Forms.Label();
this.authorTextBox = new System.Windows.Forms.TextBox();
this.authorLabel = new System.Windows.Forms.Label();
this.titleTextBox = new System.Windows.Forms.TextBox();
this.titleLabel = new System.Windows.Forms.Label();
this.bookPanel = new System.Windows.Forms.Panel();
this.booksListBox = new System.Windows.Forms.ListBox();
this.helpMenuItem = new System.Windows.Forms.MenuItem();
this.exitMenuItem = new System.Windows.Forms.MenuItem();
this.viewMenuItem = new System.Windows.Forms.MenuItem();
this.smallFontMenuItem = new System.Windows.Forms.MenuItem();
this.sepMenuItem = new System.Windows.Forms.MenuItem();
this.aboutMenuItem = new System.Windows.Forms.MenuItem();
this.normalFontMenuItem = new System.Windows.Forms.MenuItem();
this.deleteMenuItem = new System.Windows.Forms.MenuItem();
this.newMenuItem = new System.Windows.Forms.MenuItem();
this.fileMenuItem = new System.Windows.Forms.MenuItem();
this.toolTips = new System.Windows.Forms.ToolTip(this.components);
this.editMenuItem = new System.Windows.Forms.MenuItem();
this.largeFontMenuItem = new System.Windows.Forms.MenuItem();
this.mainMenu = new System.Windows.Forms.MainMenu();
this.bookPanel.SuspendLayout();
this.SuspendLayout();
//
// summaryTextBox
//
this.summaryTextBox.Text = “”;
this.summaryTextBox.Font = new System.Drawing.Font(”Microsoft Sans Serif”, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.summaryTextBox.Multiline = true;
this.summaryTextBox.Size = new System.Drawing.Size(312, 278);
this.toolTips.SetToolTip(this.summaryTextBox, “Enter a summary of the book”);
this.summaryTextBox.Location = new System.Drawing.Point(16, 128);
this.summaryTextBox.TabIndex = 5;
this.summaryTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right));
this.summaryTextBox.Name = “summaryTextBox”;
//
// summaryLabel
//
this.summaryLabel.Location = new System.Drawing.Point(16, 112);
this.summaryLabel.Size = new System.Drawing.Size(100, 16);
this.summaryLabel.TabIndex = 4;
this.summaryLabel.Name = “summaryLabel”;
this.summaryLabel.Text = “Summary”;
//
// authorTextBox
//
this.authorTextBox.Text = “”;
this.authorTextBox.Size = new System.Drawing.Size(312, 20);
this.toolTips.SetToolTip(this.authorTextBox, “Enter the author of the book”);
this.authorTextBox.Location = new System.Drawing.Point(16, 80);
this.authorTextBox.TabIndex = 3;
this.authorTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right));
this.authorTextBox.Name = “authorTextBox”;
//
// authorLabel
//
this.authorLabel.Location = new System.Drawing.Point(16, 64);
this.authorLabel.Size = new System.Drawing.Size(100, 16);
this.authorLabel.TabIndex = 2;
this.authorLabel.Name = “authorLabel”;
this.authorLabel.Text = “Author”;
//
// titleTextBox
//
this.titleTextBox.Text = “”;
this.titleTextBox.Size = new System.Drawing.Size(312, 20);
this.toolTips.SetToolTip(this.titleTextBox, “Enter the title of the book”);
this.titleTextBox.Location = new System.Drawing.Point(16, 32);
this.titleTextBox.TabIndex = 1;
this.titleTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right));
this.titleTextBox.Name = “titleTextBox”;
//
// titleLabel
//
this.titleLabel.Location = new System.Drawing.Point(16, 16);
this.titleLabel.Size = new System.Drawing.Size(100, 16);
this.titleLabel.TabIndex = 0;
this.titleLabel.Name = “titleLabel”;
this.titleLabel.Text = “Title”;
//
// bookPanel
//
this.bookPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.bookPanel.TabIndex = 2;
this.bookPanel.Controls.Add(this.summaryTextBox);
this.bookPanel.Controls.Add(this.summaryLabel);
this.bookPanel.Controls.Add(this.authorTextBox);
this.bookPanel.Controls.Add(this.authorLabel);
this.bookPanel.Controls.Add(this.titleTextBox);
this.bookPanel.Controls.Add(this.titleLabel);
this.bookPanel.Name = “bookPanel”;
this.bookPanel.Size = new System.Drawing.Size(344, 422);
this.bookPanel.Location = new System.Drawing.Point(200, 0);
//
// booksListBox
//
this.booksListBox.Name = “booksListBox”;
this.toolTips.SetToolTip(this.booksListBox, “Click a book to view its details”);
this.booksListBox.Location = new System.Drawing.Point(0, 0);
this.booksListBox.TabIndex = 1;
this.booksListBox.Dock = System.Windows.Forms.DockStyle.Left;
this.booksListBox.Size = new System.Drawing.Size(200, 422);
this.booksListBox.IntegralHeight = false;
this.booksListBox.SelectedIndexChanged += new System.EventHandler(booksListBox_SelectedIndexChanged);
//
// helpMenuItem
//
this.helpMenuItem.MenuItems.Add(this.aboutMenuItem);
this.helpMenuItem.Index = 3;
this.helpMenuItem.Text = “&Help”;
//
// exitMenuItem
//
this.exitMenuItem.Index = 2;
this.exitMenuItem.Text = “E&xit”;
this.exitMenuItem.Click += new System.EventHandler(exitMenuItem_Click);
//
// viewMenuItem
//
this.viewMenuItem.MenuItems.Add(this.smallFontMenuItem);
this.viewMenuItem.MenuItems.Add(this.normalFontMenuItem);
this.viewMenuItem.MenuItems.Add(this.largeFontMenuItem);
this.viewMenuItem.Index = 2;
this.viewMenuItem.Text = “&View”;
//
// smallFontMenuItem
//
this.smallFontMenuItem.RadioCheck = true;
this.smallFontMenuItem.Index = 0;
this.smallFontMenuItem.Text = “Small Font”;
this.smallFontMenuItem.Click += new System.EventHandler(smallFontMenuItem_Click);
//
// sepMenuItem
//
this.sepMenuItem.Index = 1;
this.sepMenuItem.Text = “-”;
//
// aboutMenuItem
//
this.aboutMenuItem.Index = 0;
this.aboutMenuItem.Text = “&About”;
//
// normalFontMenuItem
//
this.normalFontMenuItem.Checked = true;
this.normalFontMenuItem.RadioCheck = true;
this.normalFontMenuItem.Index = 1;
this.normalFontMenuItem.Text = “Normal Font”;
this.normalFontMenuItem.Click += new System.EventHandler(normalFontMenuItem_Click);
//
// deleteMenuItem
//
this.deleteMenuItem.Index = 0;
this.deleteMenuItem.Text = “&Delete”;
this.deleteMenuItem.Click += new System.EventHandler(deleteMenuItem_Click);
//
// newMenuItem
//
this.newMenuItem.Index = 0;
this.newMenuItem.Text = “&New”;
this.newMenuItem.Click += new System.EventHandler(newMenuItem_Click);
//
// fileMenuItem
//
this.fileMenuItem.MenuItems.Add(this.newMenuItem);
this.fileMenuItem.MenuItems.Add(this.sepMenuItem);
this.fileMenuItem.MenuItems.Add(this.exitMenuItem);
this.fileMenuItem.Index = 0;
this.fileMenuItem.Text = “&File”;
//
// editMenuItem
//
this.editMenuItem.MenuItems.Add(this.deleteMenuItem);
this.editMenuItem.Index = 1;
this.editMenuItem.Text = “&Edit”;
//
// largeFontMenuItem
//
this.largeFontMenuItem.RadioCheck = true;
this.largeFontMenuItem.Index = 2;
this.largeFontMenuItem.Text = “Large Font”;
this.largeFontMenuItem.Click += new System.EventHandler(largeFontMenuItem_Click);
//
// mainMenu
//
this.mainMenu.MenuItems.Add(this.fileMenuItem);
this.mainMenu.MenuItems.Add(this.editMenuItem);
this.mainMenu.MenuItems.Add(this.viewMenuItem);
this.mainMenu.MenuItems.Add(this.helpMenuItem);
//
// BooksForm
//
this.Controls.Add(this.bookPanel);
this.Controls.Add(this.booksListBox);
this.ClientSize = new System.Drawing.Size(544, 422);
this.Name = “BooksForm”;
this.Menu = this.mainMenu;
this.Text = “Book Reading List”;
this.Load += new System.EventHandler(booksForm_Load);
this.bookPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new BooksForm());
}
private void booksForm_Load(object sender, System.EventArgs e)
{
booksListBox.Items.AddRange(new object[] {
new Book(”Babe: The Gallant Pig”, “Dick King-Smith”, “How an engaging pig with impeccable manners avoids the fate of most porkers by becoming a champion sheep-herder is the remarkably believable premise of an outstanding animal fantasy. Written with a genuine feeling for English farm life, the book creates a cast of memorable characters captured as well in Mary Rayner?s black-and-white drawings.”),
new Book(”The Best Christmas Pageant Ever”, “Barbara Robinson”, “When the Herdmans ? ?the worst kids in the world? ? decide that they want to be part of the traditional Christmas pageant, the townsfolk anticipate a disaster. Surprisingly, they discover that this unusual interpretation adds new meaning to the Christmas story in the conclusion of a wildly funny, fast-moving book.”),
new Book(”The Book of Three”, “Lloyd Alexander”, “The first of five books in The Prydain Chronicles introduces the principal characters who appear in the series including Assistant Pig Keeper Taran, the Princess Ellowny, Prince Gwydion, the faithful complaining Gurgi, and the vainglorious harpist Fflewddur Fflam. Indirectly inspired by Welsh mythology, flavored with humor, the tales depict the development of Taran into a heroic figure as he and his companions battle the forces of evil in a series of impelling adventures.”),
new Book(”The Borrowers”, “Mary Norton”, “A fully realized miniature world is the setting for an unusual fantasy based on the premise that a diminutive race of people lives by ?borrowing? the necessities of life from human beings. Their ingenuity in adapting items like postage stamps or pins for their purposes is as intriguing as it is inventive. First in a series.”),
new Book(”Bridge to Terabithia”, “Katherine Paterson”, “Ten-year-old Jess Arons never envisioned the possibilities which the world might hold for him until Leslie Burke moved to his rural community in Virginia. First, she proved superior as a runner; then she introduced him to the joys of literature and the imagination as they worked together to create their own special kingdom — Terabithia. Although a tragic accident ends their idyll, Leslie’s legacy endures in the hopeful ending of a beautifully crafted book.”),
new Book(”Charlotte’s Web”, “E. B. White”, “The nature of friendship, the power of advertising, and the foibles of society are deftly explored in an unforgettable story featuring a runt pig who is saved from becoming pork chops by the intervention of a remarkable spider. Expressive black-and-white illustrations, executed in harmony with the text, add further distinction.”),
new Book(”Harriet the Spy”, “Louise Fitzhugh”, “Determined to be a writer, Manhattan-raised sixth-grader Harriet M. Welsch records her gimlet-eyed observations of her world in a notebook which when discovered leads her into an unusual dilemma. With the help of Ole Golly, her eccentric but perceptive ex-nanny, she is able to resolve the situation to her advantage. A milestone in contemporary realism for children.”),
new Book(”The Hobbit; or, There and Back Again”, “J. R. R. Tolkien”, “Bilbo Baggins, a home-loving mild little creature, finds himself enticed by the wizard Gandalf into a series of hazardous adventures involving goblins, Wargs, trolls, and a dragon named Smaug. A hero in spite of himself, Bilbo shows that ordinary individuals are sometimes capable of extraordinary feats. The blending of humor and suspense with a cast of distinctive characters make this unforgettable.”),
new Book(”Humbug Mountain”, “Sid Fleischman”, “A humorous frontier story written in an engaging tall-tale style chronicles the adventures of an itinerant newspaperman and his family as they journey westward in search of a missing relative. Dastardly villains, the unpredictable Missouri River, and an unexpected gold rush guarantee suspense as incidents follow one another at a frenzied pace.”),
new Book(”The Hundred Dresses”, “Eleanor Estes”, “When shabby Wanda Petronski claims that she has one hundred dresses at home, she becomes a target for teasing by classmates who scorn her because she is different. Despite their cruelty, it is Wanda who triumphs and is remembered for her sterling character.”),
new Book(”The Incredible Journey”, “Sheila Burnford”, “Seeking their family, two dogs and a cat traverse over two hundred miles of Canadian wilderness in a breathtaking saga of endurance that creates suspense without sentimentality.”),
new Book(”Island of the Blue Dolphins”, “Scott O’Dell”, “When her tribe leaves their island home, a young Indian girl remains behind with her brother. After his death, she struggles alone to stay alive until rescuers arrive eighteen years later. Based on a true story, this unforgettable first person narrative is one of the great survival stories of the twentieth century.”),
new Book(”Johnny Tremain”, “Esther Forbes”, “When a jealous fellow apprentice precipitates the accident which maims his hand, a bright, observant teenager, unable to continue his training as a silversmith, becomes embroiled in events leading to the opening battle of the American Revolution.”),
new Book(”The Lion, the Witch and the Wardrobe”, “C. S. Lewis”, “Four siblings, evacuees from London during World War II, discover the magic land of Narnia behind a wardrobe in the country home where they are staying with an elderly professor. Drawn into the story of a kingdom under the spell of a white witch, they join forces with the lion Aslan to defeat her in a classic confrontation between good and evil. Perhaps the most familiar of a series which concludes with The Last Battle, 1956.”),
new Book(”Little House in the Big Woods”, “Laura Ingalls Wilder”, “Family solidarity is the memorable element in a story of pioneer life based on the author’s recollections of her own childhood. Details of daily living, the conquest of hardships through faith and determination, and above all the sense of security created by loving parents have endeared the Ingalls family to countless readers. First in a series.”),
new Book(”Many Moons”, “By James Thurber”, “When cosseted Princess Leonore insists that she must have the moon in order to recover from the illness that has incapacitated her, she plunges the court into a frenetic search for a solution. After all the great minds have failed, the court jester triumphs in a logical conclusion to a sparkling spoof of the conventional fairy tale by one of America’s foremost humorists.”),
new Book(”Mary Poppins”, “P. L. Travers”, “The storytelling tone immediately draws readers into events at Number Seventeen Cherry Tree Lane when that impeccable nursemaid Mary Poppins arrives on the East Wind and transforms the lives of her young charges. The episodic structure makes the book suited for reading aloud; the combination of Mary Poppins’s acerbic personality and her magical powers guarantees adventures whether on a trip to the zoo or having afternoon tea. First of a series.”)
});
}
private void booksListBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
Book book;
// Update the current item (if there is one)
if ( lastSelectedIndex >= 0 )
{
book = (Book)booksListBox.Items[lastSelectedIndex];
book.Title = titleTextBox.Text;
book.Author = authorTextBox.Text;
book.Summary = summaryTextBox.Text;
booksListBox.Items[lastSelectedIndex] = book; // Refresh the item in list
}
// Set the new current item
lastSelectedIndex = booksListBox.SelectedIndex;
// Initialize the edit fields
if ( lastSelectedIndex >= 0 )
{
book = (Book)booksListBox.Items[lastSelectedIndex];
titleTextBox.Text = book.Title;
authorTextBox.Text = book.Author;
summaryTextBox.Text = book.Summary;
}
else
{
titleTextBox.Text = “”;
authorTextBox.Text = “”;
summaryTextBox.Text = “”;
}
}
private void exitMenuItem_Click(object sender, System.EventArgs e)
{
Close();
}
private void newMenuItem_Click(object sender, System.EventArgs e)
{
// Add new book to list
int index = booksListBox.Items.Add(new Book(”untitled”, “”, “”));
booksListBox.SelectedIndex = index;
titleTextBox.Focus();
}
private void deleteMenuItem_Click(object sender, System.EventArgs e)
{
// Delete current book from list
int index = booksListBox.SelectedIndex;
if ( index >= 0 ) booksListBox.Items.RemoveAt(index);
}
private void smallFontMenuItem_Click(object sender, System.EventArgs e)
{
smallFontMenuItem.Checked = true;
normalFontMenuItem.Checked = false;
largeFontMenuItem.Checked = false;
// set font small
summaryTextBox.Font = new Font(”Microsoft Sans Serif”,
;
}
private void normalFontMenuItem_Click(object sender, System.EventArgs e)
{
smallFontMenuItem.Checked = false;
normalFontMenuItem.Checked = true;
largeFontMenuItem.Checked = false;
// set font normal
summaryTextBox.Font = new Font(”Microsoft Sans Serif”, 12);
}
private void largeFontMenuItem_Click(object sender, System.EventArgs e)
{
smallFontMenuItem.Checked = false;
normalFontMenuItem.Checked = false;
largeFontMenuItem.Checked = true;
// set font large
summaryTextBox.Font = new Font(”Microsoft Sans Serif”, 16);
}
}
}