CHAPTER 19 BUILDING A BETTER WINDOW WITH (Best web site)
CHAPTER 19 BUILDING A BETTER WINDOW WITH SYSTEM.WINDOWS.FORMS 629 At this point, you should be able to compile and run your program. Verify that you can terminate the application via File .Exit as well as pressing Alt+f and then x on the keyboard. Adding a TextBox to the MenuStrip Now, let s create a new topmost menu item named Change Background Color. The subitem in this case will not be a menu item, but a ToolStripTextBox (see Figure 19-13). Once you have added the new control, rename this control to toolStripTextBoxColor using the Properties window. The goal here is to allow the user to enter the name of a color (red, green, pink, etc.) that will be used to set the BackColor property of the Form. First, handle the LostFocus event on the new ToolStripTextBox member variable within the Form s constructor (as you would guess, this event fires when the TextBox within the ToolStrip is no longer the active UI element): public MainWindow() { … toolStripTextBoxColor.LostFocus += new EventHandler(toolStripTextBoxColor_LostFocus); } Within the generated event handler, you will extract the string data entered within the ToolStripTextBox (via the Text property) and make use of the System.Drawing.Color.FromName() method. This static method will return a Color type based on a known string value. To account for the possibility that the user enters an unknown color (or types bogus data), you will make use of some simple try/catch logic: void toolStripTextBoxColor_LostFocus(object sender, EventArgs e) { try { BackColor = Color.FromName(toolStripTextBoxColor.Text); } catch { } // Just do nothing if the user provides bad data. } Figure 19-13. Adding TextBoxes to a MenuStrip
In case you need quality webspace to host and run your web applications, try our personal web hosting services.