Netframwork 2.0 (phần 13) pot

50 323 0
Netframwork 2.0 (phần 13) pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lesson 2: Using User Assistance Controls and Components 575 Table 12-6 Important Methods of the HelpProvider Component Method Description SetHelpKeyword Sets the keyword for a control, such as an index keyword or a topic keyword. This keyword will be passed to the help file when help is shown. The HelpNavigator setting determines how the keyword is used. SetHelpNavigator Sets the HelpNavigator for a control. This property deter- mines how the help file is displayed. SetHelpString Sets the help string for a control. If the HelpNamespace prop- erty is not set to a file, the help string will be displayed in a popup box over the control when the F1 button is pressed. SetShowHelp Sets whether help is shown for a particular control. Setting the HelpNavigator The SetHelpNavigator method allows you to determine how help is displayed for a particular control. The SetHelpNavigator method requires two parameters: a control that the HelpNavigator will be set for, and a member of the HelpNavigator enumeration. The following example demonstrates how to set the Help- Navigator for a Button control named Button1: ' VB HelpProvider1.SetHelpNavigator(Button1, HelpNavigator.Find) // C# helpProvider1.SetHelpNavigator(Button1, HelpNavigator.Find); Table 12-7 describes the members of the HelpNavigator enumeration. Table 12-7 Members of the HelpNavigator Enumeration Member Description AssociateIndex The help file opens to the index entry for the first letter of the specified keyword. Find The help file opens to the search page. Index The help file opens to the index. 576 Chapter 12 Enhancing Usability Table 12-7 Members of the HelpNavigator Enumeration Member Description KeywordIndex The help file opens to the topic with the specified keyword if one exists; otherwise, the index entry closest to the specified key- word is displayed. TableOfContents The help file opens to the table of contents. Topic The help file opens to a specified topic if the topic exists. TopicId The help file opens to a topic indicated by a numeric topic identifier. Configuring Help for a Control The following example demonstrates how to use the HelpProvider component to configure help for a control named Button1. ' VB ' This example assumes that a help file has been compiled for your ' application and the path to that file has been set in the HelpNamespace property. ' Sets help for Button1 to be shown. HelpProvider1.SetShowHelp(Button1, True) ' Sets the help keyword for the control HelpProvider1.SetHelpKeyword(Button1, "Button1") ' Sets the HelpNavigator for the control HelpProvider1.SetHelpNavigator(Button1, HelpNavigator.KeywordIndex) // C# // This example assumes that a help file has been compiled for your // application and the path to that file has been set in the HelpNamespace property. // Sets help for Button1 to be shown. helpProvider1.SetShowHelp(button1, true); // Sets the help keyword for the control helpProvider1.SetHelpKeyword(button1, "button1"); // Sets the HelpNavigator for the control helpProvider1.SetHelpNavigator(button1, HelpNavigator.KeywordIndex); Playing Sound Files and System Sounds Sound files and system sounds can be used to communicate information or alerts to your users. The SoundPlayer class in the System.Media namespace encapsulates all of the functionality required to play sound files. � To play a sound file with the SoundPlayer class 1. Create an instance of the SoundPlayer class that specifies the sound file to be played, as shown here: Lesson 2: Using User Assistance Controls and Components 577 ' VB Dim aPlayer As New System.Media.SoundPlayer("C:\mySoundFiles\Boom.wav") // C# System.Media.SoundPlayer aPlayer = new System.Media.SoundPlayer(@"C:\mySoundFiles\Boom.wav"); 2. Play the sound by calling the Play method, as shown: ' VB aPlayer.Play // C# aPlayer.Play(); You can play system sounds by accessing the System.Media.SystemSounds class. This class contains members that represent each of the system sounds, and they can be played by calling the Play method. � To play a system sound Call the Play method of the appropriate system sound, as shown here: ' VB System.Media.SystemSounds.Beep.Play() // C# System.Media.SystemSounds.Beep.Play(); Using the Timer Component to Raise an Event at Regular Intervals The Timer component allows you raise the Timer.Tick event at regular intervals. By writing code to handle this event, you can schedule your application to call methods or take other actions at predefined intervals. For example, you might want to display the current time in a Label control and update it every minute. The key property of the Timer component is the Interval property. The Interval prop- erty specifies the number of milliseconds between intervals of the Tick event. For example, if the Interval property is set to 2000, the Tick event will be raised every two seconds. The Interval property can be set in the Properties window or in code, as shown in the following example: ' VB Timer1.Interval = 2000 // C# timer1.Interval = 2000; 578 Chapter 12 Enhancing Usability Another important property of the Timer component is the Enabled property. If the Enabled property is set to False, the Timer.Tick event will not be raised. When the Enabled property is set to True, the Timer.Tick event will be raised at a frequency deter- mined by the Interval property. The Timer.Start and Timer.Stop methods allow you to start and stop the Timer, respec- tively. These methods are simply shorthand ways of setting the Enabled property. Call- ing the Timer.Start method sets the Timer.Enabled property to True and commences regular firing of the Tick event. Calling the Timer.Stop method sets the Timer.Enabled property to False and ceases firing of the Tick event. The following example demonstrates a handler for the Timer.Tick event. This sample code updates the text in Label1 with the current time every time the Tick event is raised. ' VB Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Timer1.Tick Label1.Text = Now.ToLongTimeString End Sub // C# private void timer1_Tick(object sender, EventArgs e) { label1.Text = DateTime.Now.ToLongTimeString; } Using the HScrollBar and VScrollBar Controls The HScrollBar and VScrollBar controls are controls that are designed to provide easy navigation through a long list of items or values by scrolling within an application or control. The HScrollBar control represents a horizontal scroll bar and the VScrollBar control represents a vertical scroll bar, but the two controls function identically other- wise. The ScrollBar controls consist of two scrollbar buttons at either end of the con- trol, a slider, and a slider track in which the slider can move. The user can position the slider and thereby change the Value property. The ScrollBar controls are distinct from the scrollbars that appear in Forms or scrollable controls—those scroll bars are integral parts of the control or form that they function with. The ScrollBar controls are designed for independent operation or operation with controls that do not normally have scrollbars, such as the PictureBox control. The important properties of the ScrollBar controls are shown in Table 12-8. Lesson 2: Using User Assistance Controls and Components 579 Table 12-8 Important Properties of the Scrollbar Controls Property Description LargeChange The amount the Value property changes when the user presses the PageUp or PageDown keys or clicks in the ScrollBar track. Maximum The maximum value for the scrollbar. In HScrollBar, the Value property is equal to the Maximum property when the scrollbar slider is all the way to the right in the scroll track. In VScrollBar, the Value property is equal to the Maximum property when the scroll- bar slider is all the way at the bottom of the scroll track. Minimum The minimum value for the scrollbar. In HScrollBar, the Value prop- erty is equal to the Minimum property when the scrollbar slider is all the way to the left in the scroll track. In VScrollBar, the Value property is equal to the Minimum property when the scrollbar slider is all the way at the top of the scroll track. SmallChange The amount the Value property changes when the user presses one of the arrow keys or clicks a scrollbar button. Value The current value of the Scrollbar control. This property reflects the current location of the scrollbar slider. Persisting Application Settings Between Sessions The .NET Framework allows you to persist property values between user sessions in the form application settings and access and change those settings at run time. You might, for example, create settings that determine the color scheme of your applica- tion or settings that “remember” user names or data connection strings. Persisting settings is a two-step process. First, you create a setting and give it a unique name and a value. The value can be any object and should be appropriate for the prop- erty that you want to persist. Second, the property that you want to persist is bound to the setting. At run time, the settings can be accessed, changed, and saved if need be. Creating Settings at Design Time Visual Studio 2005 provides user interface tools that allow you to create settings quickly and easily. You can use the Settings editor (shown in Figure 12-2) to create and edit new settings. 580 Chapter 12 Enhancing Usability Figure 12-2 The Settings editor for creating and editing new settings The Settings editor has four properties that must be set when they are created. The Name must be unique in the application. The Type property represents the type of set- ting, which should be appropriate for the property you want to bind it to. The Scope property must be set to either Application or User. Settings that are scoped for the application are read-only at run time and cannot be changed by the user. Settings that are scoped for the user are read-write at run time and can be changed and saved by the user. Finally, a value must be provided for the setting. This value must be of the type indicated by the Type property. � To create a setting at design time 1. In Solution Explorer, right-click the project and choose Properties. 2. In the main window, select the Settings tab. 3. Set the Name, Type, Scope, and Value for the new setting. 4. If your application has not yet been saved, choose Save All from the File menu to save your application. Once a setting has been created, you can bind it to a property in the Properties window. � To bind a setting to a property at design time 1. In the Properties window for a control, expand (ApplicationSettings) and then click the button next to (PropertyBinding) to open the Application Settings window. 2. Locate the property that you want to bind to a setting. Select the appropriate set- ting from the drop-down box to the right of the property name. If no appropriate setting exists, you can create a new setting by clicking the (New…) link in the bot- tom of the drop-down box. Lesson 2: Using User Assistance Controls and Components 581 Accessing Settings at Run Time You can read the values of your settings at run time and change and save the values of user-scoped settings. In Visual Basic, settings are exposed via the My object, and changes made to any user-scoped settings values are automatically saved. In C#, you can access settings through the Properties.Set- tings.Default object, but any changes made must be saved manually. At design time, individual settings appear in Intellisense as properties of the Settings object and can be treated in code as such. The following example demonstrates how to change the value of a user-scoped string setting named TitleSetting: ' VB My.Settings.TitleSetting = "This is the new title" // C# Properties.Settings.DefaultSettings.TitleSetting = "This is the new Title"; Properties.Settings.DefaultSettings.Save(); Quick Check 1. What is the purpose of the Timer component? 2. What is the purpose of the ErrorProvider component? Quick Check Answers 1. The Timer component is used to execute code at regular intervals. 2. The error provider is used to provide a visual cue to users when a validation error occurs. Lab: Practice with User Assistance Controls In this lab, you will create a small application that uses a timer to display and update the current time in a label, and you will use the functionality and many of the controls that have been discussed in this lesson. Although the end result of the lesson is some- what contrived, it will allow you to practice with the different aspects of this lesson. � Exercise 1: Creating a Simple Digital Clock 1. In Visual Studio, create a new Windows Forms application. 2. From the Toolbox, drag a Label control and a Timer component onto the form. 3. Select the Timer component. In the Properties window, set the Interval property to 1000 and the Enabled property to True. 4. In the Designer, double-click the Timer component to open the default event handler for the Timer1.Tick event. Add the following code to this method: 582 Chapter 12 Enhancing Usability ' VB Label1.Text = Now.ToLongTimeString // C# label1.Text = System.DateTime.Now.ToLongTimeString(); 5. Press F5 to compile and run your application. Note that the current time is updated in the Label control every second. � Exercise 2: Using the PropertyGrid 1. From the Toolbox, drag a PropertyGrid control onto the form that you created in the last exercise. 2. In the Properties window, set the Dock property of the PropertyGrid control to Right. 3. In the Properties window, set the SelectedObject property of the PropertyGrid con- trol to Timer1. 4. Press F5 to compile and run the application. You can now set the properties of the Timer at run time via the PropertyGrid control. � Exercise 3: Providing Tooltips for Your Application 1. From the Toolbox, drag a ToolTip component onto the form you created in Exer- cise 1, “Creating a Simple Digital Clock.” 2. Select Label1. In the Properties window, set the tooltip on ToolTip1. 3. Press F5 to compile and run your application. Note that your label now has a tooltip associated with it. � Exercise 4: Using Settings 1. In Solution Explorer, for the application you started in Exercise 1, right-click the project and choose Properties. 2. In the Properties pane, choose the Settings tab. 3. In the Settings window, create a setting with the following properties: Property Value Name IntervalSetting Type Integer Scope User Value 1000 Lesson 2: Using User Assistance Controls and Components 583 4. In the Designer, select Timer1. In the Properties window, expand (Application Settings) and then click the button next to (PropertyBinding) to open the Appli- cation Settings window. 5. In the Application Settings window, bind the Interval property to the IntervalSetting setting. 6. In the Designer, select the Form. In the Properties window, click the events but- ton to list the events for the form. Double-click the cell next to the FormClosing event to open the default event handler for the Form1.FormClosing event. Add the following code to this event handler: ' VB My.Settings.IntervalSetting = Timer1.Interval // C# Properties.Settings.Default.IntervalSetting = timer1.Interval; Properties.Settings.Default.Save(); 7. From the File menu, choose Save All. 8. Press F5 to compile and run your application. Any changes that you make to the interval property will be persisted between sessions. Lesson Summary ■ The PropertyGrid control allows you to set properties for controls in your appli- cation through a grid-style user interface. The SelectedObject property indicates the object for which the properties can be set. ■ The ProgressBar control allows you to inform the user about progress for time- consuming processes such as file downloads. The Minimum and Maximum prop- erties represent the minimum and maximum values for the ProgressBar, and the Value control represents the current value. ■ The StatusStrip control is a ToolStrip control that is designed to display information about the status of the application. ToolStripStatusLabel and ToolStripProgressBar are ToolStripItem controls that are designed to work with the StatusStrip. ■ The ToolTip component allows you to display tooltips for the controls on your form. ■ The ErrorProvider component allows you to display errors in user input in your application. You can validate user input by adding code to the Validating event handler. If the user input is not valid, use the SetError method of the error pro- vider to set the error. 584 Chapter 12 Enhancing Usability ■ The HelpProvider component enables you to integrate HTML Help 1.x files (.chm and .htm) with your application. The HelpNamespace property represents the location of the help file that is associated with the HelpProvider component. You can call SetShowHelp to set F1 help for a control and SetHelpKeyword to set the keyword that will be passed on to the help file. ■ The SoundPlayer component allows you to play sound files. You can use the System- Sounds class to play system sounds at run time. ■ The Timer component allows you to execute actions at regular intervals. You can handle the Timer.Tick event to define code that is executed at regular intervals. The Interval property determines how frequently the Timer.Tick event is raised. ■ The HScrollBar and VScrollBar controls are designed to provide a user interface that allows rapid access to a large range of values or a long list of options. The Minimum and Maximum properties represent the minimum and maximum val- ues for the control; the Value property represents the current value and is tied to the current position of the slider. ■ Application settings allow you to persist settings between application settings. Application-scoped settings are read-only and user-scoped settings are read- write at run time. You can access settings via the My.Settings object in Visual Basic or the Properties.Settings.Default object in C#. Lesson Review The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form. NOTE Answers Answers to these questions and explanations of why each choice is right or wrong are located in the “Answers” section at the end of the book. 1. Which of the following code examples will advance the value of a progress bar named ProgressBar1 by 10? (Choose all that apply.) A. ' VB ProgressBar1.Step = 10 ProgressBar1.PerformStep // C# progressBar1.Step = 10; progressBar1.PerformStep(); [...]... simplest way to implement an asynchronous method call is to call BeginInvoke, do some work, and then call EndInvoke on the same thread that BeginInvoke was called on While this approach is simplest, a potential disadvantage is that the EndInvoke call will block execution of the thread until the asynchronous operation is completed if it has not completed yet Thus your main thread might still be nonresponsive . code, as shown in the following example: ' VB Timer1.Interval = 20 00 // C# timer1.Interval = 20 00; 578 Chapter 12 Enhancing Usability Another important property of the Timer component. Studio 20 05 provides user interface tools that allow you to create settings quickly and easily. You can use the Settings editor (shown in Figure 12- 2) to create and edit new settings. 5 80 Chapter. ProgressBar1.Step = 10 // C# progressBar1.Step = 10; C. ' VB ProgressBar1.Increment( 10) // C# progressBar1.Increment( 10) ; D. ' VB ProgressBar1.Step = 10 ProgressBar1.Increment

Ngày đăng: 07/07/2014, 05:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan