Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 135 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
135
Dung lượng
2,56 MB
Nội dung
Part II: Windows Programming 504 2. Name the three controls ToolStripMenuItemBold , ToolStripMenuItemItalic , and ToolStripMenuItemUnderline . 3. Add a ToolStrip to the form. In the Actions Window, click Insert Standard Items. Select and delete the items for Cut, Copy, Paste, and the Separator after them. When you insert the ToolStrip , the RichTextBox may fail to dock properly. If that happens, change the Dock style to none and manually resize the control to fill the form. Then change the Anchor property to Top , Bottom , Left , Right . 4. Create three new buttons and a separator at the end of the toolbar by selecting Button three times and Separator once. (Click on the last item in the ToolStrip to bring up those options.) 5. Create the final two items by selecting ComboBox from the drop - down list and then adding a separator as the last item. 6. Select the Help item and drag it from its current position to the position as the last item in the toolbar. 7. The first three buttons are going to be the Bold, Italic, and Underline buttons, respectively. Name the controls as shown in the following table: ToolBarButton Name Bold button ToolStripButtonBold Italic button ToolStripButtonItalic Underline button ToolStripButtonUnderline ComboBox ToolStripComboBoxFonts 8. Select the Bold button, click on the ellipses ( … ) in the Image property, select the Project Resource File radio button, and click Import. If you ’ ve downloaded the source code for this book, use the three images found in the folder Chapter16\Toolbars\ Images: BLD.ico , ITL.ico , and UNDRLN.ico . Note that the default extensions suggested by Visual Studio do not include ICO , so when browsing for the icons you will have to choose Show All Files from the drop - down. 9. Select BLD.ico for the image of the Bold button. 10. Select the Italic button and change its image to ITL.ico . 11. Select the Underline button and change its image to UNDRLN.ico . c16.indd 504c16.indd 504 3/24/08 3:54:02 PM3/24/08 3:54:02 PM Chapter 16: Advanced Windows Forms Features 505 12. Select the ToolStripComboBox . In the Properties panel, set the properties shown in the following table: Property Value Items MS Sans Serif Times New Roman DropDownStyle DropDownList 13. Set the CheckOnClick property for each of the Bold, Italic, and Underline buttons to true . 14. To select the initial item in the ComboBox, enter the following into the constructor of the class: public Form1() { InitializeComponent(); this.ToolStripComboBoxFonts.SelectedIndex = 0; } 15. Press F5 to run the example. You should see a dialog that looks like Figure 16 - 6 . Figure 16-6 Adding Event Handlers You are now ready to add the event handlers for the items on the menu and toolbars. You already have handlers for the Save, New, and Open items on the menu, and obviously the buttons on the toolbar should behave in exactly the same way as the menu. This is easily achieved by assigning the Click events of the buttons on the toolbars to the same handlers that are used by the buttons on the menu. Set the events as follows: ToolStripButton Event New MenuItemNew_Click Open MenuItemOpen_Click Save MenuItemSave_Click c16.indd 505c16.indd 505 3/24/08 3:54:02 PM3/24/08 3:54:02 PM Part II: Windows Programming 506 Now it ’ s time to add handlers for the Bold, Italic, and Underline buttons. As these buttons are check buttons, you should use the CheckedChanged event instead of the Click event, so go ahead and add that event for each of the three buttons. Add the following code: private void ToolStripButtonBold_CheckedChanged(object sender, EventArgs e) { Font oldFont; Font newFont; bool checkState = ((ToolStripButton)sender).Checked; // Get the font that is being used in the selected text. oldFont = this.richTextBoxText.SelectionFont; if (!checkState) newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold); else newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold); // Insert the new font and return focus to the RichTextBox. this.richTextBoxText.SelectionFont = newFont; this.richTextBoxText.Focus(); this.ToolStripMenuItemBold.CheckedChanged -= new EventHandler(ToolStripMenuItemBold_CheckedChanged); this.ToolStripMenuItemBold.Checked = checkState; this.ToolStripMenuItemBold.CheckedChanged += new EventHandler(ToolStripMenuItemBold_CheckedChanged); } private void ToolStripButtonItalic_CheckedChanged(object sender, EventArgs e) { Font oldFont; Font newFont; bool checkState = ((ToolStripButton)sender).Checked; // Get the font that is being used in the selected text. oldFont = this.richTextBoxText.SelectionFont; if (!checkState) newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic); else newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic); // Insert the new font. this.richTextBoxText.SelectionFont = newFont; this.richTextBoxText.Focus(); this.ToolStripMenuItemItalic.CheckedChanged -= new EventHandler(ToolStripMenuItemItalic_CheckedChanged); c16.indd 506c16.indd 506 3/24/08 3:54:03 PM3/24/08 3:54:03 PM Chapter 16: Advanced Windows Forms Features 507 this.ToolStripMenuItemItalic.Checked = checkState; this.ToolStripMenuItemItalic.CheckedChanged += new EventHandler(ToolStripMenuItemItalic_CheckedChanged); } private void ToolStripButtonUnderline_CheckedChanged(object sender, EventArgs e) { Font oldFont; Font newFont; bool checkState = ((ToolStripButton)sender).Checked; // Get the font that is being used in the selected text. oldFont = this.richTextBoxText.SelectionFont; if (!checkState) newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline); else newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline); // Insert the new font. this.richTextBoxText.SelectionFont = newFont; this.richTextBoxText.Focus(); this.ToolStripMenuItemUnderline.CheckedChanged -= new EventHandler(ToolStripMenuItemUnderline_CheckedChanged); this.ToolStripMenuItemUnderline.Checked = checkState; this.ToolStripMenuItemUnderline.CheckedChanged += new EventHandler(ToolStripMenuItemUnderline_CheckedChanged); } The event handlers simply set the correct style to the font used in the RichTextBox . The three final lines in each of the three methods deal with the corresponding item in the menu. The first line removes the event handler from the menu item. This ensures that no events trigger when the next line runs, which sets the state of the Checked property to the same value as the toolbar button. Finally, the event handler is reinstated. The event handlers for the menu items should simply set the Checked property of the buttons on the toolbar, allowing the event handlers for the toolbar buttons to do the rest. Add the event handlers for the CheckedChanged event and enter this code: private void ToolStripMenuItemBold_CheckedChanged(object sender, EventArgs e) { this.ToolStripButtonBold.Checked = ToolStripMenuItemBold.Checked; } private void ToolStripMenuItemItalic_CheckedChanged(object sender, EventArgs e) c16.indd 507c16.indd 507 3/24/08 3:54:03 PM3/24/08 3:54:03 PM Part II: Windows Programming 508 { this.ToolStripButtonItalic.Checked = ToolStripMenuItemItalic.Checked; } private void ToolStripMenuItemUnderline_CheckedChanged(object sender, EventArgs e) { this.ToolStripButtonUnderline.Checked = ToolStripMenuItemUnderline.Checked; } The only thing left to do is allow users to select a font family from the ComboBox . Whenever a user changes the selection in the ComboBox , the SelectedIndexChanged is raised, so add an event handler for that event: private void toolStripComboBoxFonts_SelectedIndexChanged(object sender, EventArgs e) { string text = ((ToolStripComboBox)sender).SelectedItem.ToString(); Font newFont = null; // Create a new font with the correct font family. if (richTextBoxText.SelectionFont == null) newFont = new Font(text, richTextBoxText.Font.Size); else newFont = new Font(text, richTextBoxText.SelectionFont.Size, richTextBoxText.SelectionFont.Style); richTextBoxText.SelectionFont = newFont; } Now run the code. You should be able to create a dialog that looks something like what is shown in Figure 16 - 7 . The toolbar has been moved a bit to the right to also show the menu. Figure 16-7 c16.indd 508c16.indd 508 3/24/08 3:54:04 PM3/24/08 3:54:04 PM Chapter 16: Advanced Windows Forms Features 509 StatusStrip The last of the small family of strip controls is the StatusStrip . This control represents the bar that you find at the bottom of the dialog in many applications. The bar is typically used to display brief information about the current state of the application — a good example is Word ’ s display of the current page, column, line, and so on in the status bar as you are typing. The StatusStrip is derived from the ToolStrip , and you should be quite familiar with the view that is presented as you drag the control onto your form. Three of the four possible controls that can be used in the StatusStrip — ToolStripDropDownButton , ToolStripProgressBar , and ToolStripSplitButton — were presented earlier. That leaves just one control that is specific to the StatusStrip : the StatusStripStatusLabel , which is also the default item you get. StatusStripStatusLabel Properties The StatusStripStatusLabel is used to present the user with information about the current state of the application, with text and images. Because the label is actually a pretty simple control, not a lot of properties are covered here. The following two are not specific to the label, but nevertheless can and should be used with some effect: Property Value AutoSize AutoSize is on by default, which isn ’ t really very intuitive because you don ’ t want the labels in the status bar to jump back and forth just because you changed the text in one of them. Unless the information in the label is static, always change this property to false . DoubleClickEnable Specifies whether the DoubleClick event will fire, which means your users get a second place to change something in your application. An example of this is allowing users to double - click on a panel containing the word Bold to enable or disable bolding in the text. In the following Try It Out, you create a simple status bar for the example you ’ ve been working on. The status bar has four panels, three of which display an image and text; the last panel displays only text. Try It Out StatusStrip Follow these steps to extend the small text editor you ’ ve been working on: 1. Double - click the StatusStrip in the ToolBox to add it to the dialog. You may need to resize the RichTextBox on the form. 2. In the Properties panel, click the ellipses ( … ) in the Items property of the StatusStrip . This brings up the Items Collection Editor. c16.indd 509c16.indd 509 3/24/08 3:54:04 PM3/24/08 3:54:04 PM Part II: Windows Programming 510 3. Click the Add button four times to add four panels to the StaturStrip . Set the following properties on the panels: Panel Property Value 1 Name toolStripStatusLabelText Text Clear this property AutoSize False DisplayStyle Text Font Arial; 8,25pt; style=Bold Size 259,17 TextAlign Middle Left 2 Name toolStripStatusLabelBold Text Bold DisplayStyle ImageAndText Enabled False Font Arial; 8.25pt; style=Bold Size 47, 17 Image BLD ImageAlign Middle - Center 3 Name toolStripStatusLabelItalic Text Italic DisplayStyle ImageAndText Enabled False Font Arial; 8.25pt; style=Bold Size 48, 17 Image ITL ImageAlign Middle - Center c16.indd 510c16.indd 510 3/24/08 3:54:04 PM3/24/08 3:54:04 PM Chapter 16: Advanced Windows Forms Features 511 Panel Property Value 4 Name toolStripStatusLabelUnderline Text Underline DisplayStyle ImageAndText Enabled False Font Arial; 8.25pt; style=Bold Size 76, 17 Image UNDRLN ImageAlign Middle - Center 4. Add this line of code to the event handler at the end of the ToolStripButtonBold_ CheckedChanged method: toolStripStatusLabelBold.Enabled = checkState; 5. Add this line of code to the event handler at the end of the ToolStripButtonItalic_ CheckedChanged method: toolStripStatusLabelItalic.Enabled = checkState; 6. Add this line of code to the event handler at the end of the ToolStripButtonUnderline_ CheckedChanged method: toolStripStatusLabelUnderline.Enabled = checkState; 7. Select the RichTextBox and add the TextChanged event to the code. Enter the following code: private void richTextBoxText_TextChanged(object sender, EventArgs e) { toolStripStatusLabelText.Text = “Number of characters: “ + richTextBoxText.Text.Length; } When you run the application you should have a dialog that looks like the one shown in Figure 16 - 8 . c16.indd 511c16.indd 511 3/24/08 3:54:05 PM3/24/08 3:54:05 PM Part II: Windows Programming 512 Figure 16-8 SDI and MDI Applications Traditionally, three kinds of applications can be programmed for Windows: Dialog - based applications : These present themselves to the user as a single dialog from which all functionality can be reached. Single - document interfaces (SDI) : These present themselves to the user with a menu, one or more toolbars, and one window in which the user can perform some task. Multiple - document interfaces (MDI) : These present themselves to the user in the same manner as an SDI, but are capable of holding multiple open windows at one time. Dialog - based applications are usually small, single - purpose applications aimed at a specific task that needs a minimum of data to be entered by the user or that target a very specific type of data. An example of such an application is shown in Figure 16 - 9 — the Windows Calculator. ❑ ❑ ❑ Figure 16-9 Single - document interfaces are each usually aimed at solving one specific task because they enable users to load a single document into the application to be worked on. This task, however, usually involves a lot of user interaction, and users often want the capability to save or load the result of their work. Good examples of SDI applications are WordPad (shown in Figure 16 - 10 ) and Paint, both of which come with Windows. c16.indd 512c16.indd 512 3/24/08 3:54:05 PM3/24/08 3:54:05 PM Chapter 16: Advanced Windows Forms Features 513 Figure 16-10 However, only one document can be open at any one time, so if a user wants to open a second document, then a fresh instance of the SDI application must be opened, and it will have no reference to the first instance. Any configuration you do to one instance is not carried over into the other. For example, in one instance of Paint you might set the drawing color to red, and when you open a second instance of Paint, the drawing color is the default, which is black. Multiple - document interfaces are much the same as SDI applications, except that they are able to hold more than one document open in different windows at any given time. A telltale sign of an MDI application is the inclusion of the Window menu just before the Help menu on the menu bar. An example of an MDI application is Adobe Reader, shown in Figure 16 - 11 . Figure 16-11 c16.indd 513c16.indd 513 3/24/08 3:54:06 PM3/24/08 3:54:06 PM [...]... control, from a label with a nifty design to full-blown grid controls In Figure 16- 15, the box at the bottom, UserControl1, represents a new control Figure 16- 15 User controls inherit from the System.Windows.Forms.UserControl class, but custom controls derive from the System.Windows.Forms.Control class 52 5 c16.indd 52 5 3/24/08 3 :54 :10 PM Part II: Windows Programming A couple of things are assumed when working... display on the toolbar, menu, and status bar is always in sync by changing the text “Bold” to be bold when it is enabled and otherwise not Do the same with Italic and Underlined 53 5 c16.indd 53 5 3/24/08 3 :54 :13 PM c16.indd 53 6 3/24/08 3 :54 :14 PM 17 Using Common Dialogs The last three chapters looked at various aspects of programming Windows Forms applications, and how to implement such things as menus, toolbars,... container This is done by setting the form’s MdiParent property as shown in the preceding code Notice that the constructor you are using includes the parameter parent 51 5 c16.indd 51 5 3/24/08 3 :54 :07 PM Part II: Windows Programming Because C# does not provide default constructors for a class that defines its own constructor, the preceding code prevents you from creating an instance of the form that is... EventArgs e) { LayoutMdi(MdiLayout.TileHorizontal); } private void ToolStripMenuItemCascasde_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.Cascade); } 52 2 c16.indd 52 2 3/24/08 3 :54 :09 PM Chapter 16: Advanced Windows Forms Features 5 Change the constructor of the frmEditor dialog as follows: public frmEditor(frmContainer parent, int counter) { InitializeComponent(); this.ToolStripComboBoxFonts.SelectedIndex... you to arrange the dialogs in a standard manner 52 3 c16.indd 52 3 3/24/08 3 :54 :09 PM Part II: Windows Programming The changes to the constructors and New item simply ensure that the dialogs are numbered Run the application now and you should see something like what is shown in Figure 16-14 Figure 16-14 Creating Controls Sometimes the controls that ship with Visual Studio just won’t meet your needs The... control 52 4 c16.indd 52 4 3/24/08 3 :54 :10 PM Chapter 16: Advanced Windows Forms Features This chapter focuses on user controls, because designing and drawing a custom control from scratch is beyond the scope of this book Chapter 33, on GDI+, gives you the means to draw items by yourself, and from there you should then be able to move on to custom controls easily ActiveX controls as used in Visual Studio... the parent this.MdiParent = parent; } 5 Change the MergeAction property of the menu item with the text &File to Replace and the same property of the item with the text &Format to MatchOnly Change the AllowMerge property of the toolbar to False 6 Add a MenuStrip to the frmContainer form Add a single item to the MenuStrip with the text &File 51 7 c16.indd 51 7 3/24/08 3 :54 :07 PM Part II: Windows Programming... would just add a breakpoint somewhere, press F5, and see what happens If you are still unfamiliar with debugging, then refer to Chapter 7 for a detailed explanation A control needs a container in which to display itself, and you have to supply it with one You do that in the following Try It Out by creating a Windows application project 53 0 c16.indd 53 0 3/24/08 3 :54 :12 PM Chapter 16: Advanced Windows Forms... -1 MergeAction MatchOnly MergeIndex -1 MergeAction Insert MergeIndex 2 MergeAction Insert MergeIndex 3 &New - &Save 52 0 c16.indd 52 0 3/24/08 3 :54 :08 PM Chapter 16: Advanced Windows Forms Features Item Property Value Save &As MergeAction Insert MergeIndex 4 MergeAction Insert MergeIndex 5 MergeAction Insert MergeIndex 6 MergeAction Insert MergeIndex 7 MergeAction Insert MergeIndex 8 Name ToolStripMenuItemClose... that are specific to the child window when the child is in focus, and leave the rest of the items to the main window to display The following properties control the behavior of menu items: 51 8 c16.indd 51 8 3/24/08 3 :54 :08 PM Chapter 16: Advanced Windows Forms Features Property Description MergeAction Specifies how an item should behave when it is to be merged into another menu The possible values are . using includes the parameter parent . c16.indd 51 5c16.indd 51 5 3/24/08 3 :54 :07 PM3/24/08 3 :54 :07 PM Part II: Windows Programming 51 6 Because C# does not provide default constructors for a class. looks like the one shown in Figure 16 - 8 . c16.indd 51 1c16.indd 51 1 3/24/08 3 :54 : 05 PM3/24/08 3 :54 : 05 PM Part II: Windows Programming 51 2 Figure 16-8 SDI and MDI Applications Traditionally,. Paint, both of which come with Windows. c16.indd 51 2c16.indd 51 2 3/24/08 3 :54 : 05 PM3/24/08 3 :54 : 05 PM Chapter 16: Advanced Windows Forms Features 51 3 Figure 16-10 However, only one document can