Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 79 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
79
Dung lượng
769,23 KB
Nội dung
284 Chapter 7 • Creating Windows Forms RectangleToClient Gets the client coordinates and size of a specified rectangle. RectangleToScreen Gets the client coordinates and size for a specified rectangle. Refresh (inherited from Forces the control to invalidate and immediately Control) repaint itself and any children. RemoveOwnedForm Removes a form from the list of owned forms. Also sets the owner of the removed form to a null reference (in Visual Basic Nothing). ResetBackColor (inherited Resets the back color to be based on the from RichControl) parent’s back color. ResetBindings (inherited Resets the DataBindings property to its default from RichControl) value. ResetCursor (inherited Resets the Cursor property to its default value. from RichControl) ResetFont (inherited from Resets the font to be based on the parent’s font. RichControl) ResetForeColor (inherited Resets the fore color to be based on the parent’s from RichControl) fore color. ResetRightToLeft Resets RightToLeft to be the default. (inherited from RichControl) ResetText (inherited from Resets the text to its default value. Control) ResumeLayout (inherited Overloaded. Resumes normal layout logic. from Control) ResumeLayout (inherited Overloaded. Resumes normal layout logic. from Control) RTLTranslateAlignment Overloaded Converts the current alignment to (inherited from RichControl) the appropriate alignment to support right-to- left text. RTLTranslateContent [Overloaded. Converts the current alignment to (inherited from the appropriate alignment to support right-to- RichControl) left text. RTLTranslateHorizontal Converts the specified HorizontalAlignment to (inherited from RichControl) the appropriate HorizontalAlignment to support right-to-left text. www.syngress.com Table 7.2 Continued Method Description Continued 153_VBnet_07 8/15/01 12:31 PM Page 284 Creating Windows Forms • Chapter 7 285 RTLTranslateLeftRight Converts the specified LeftRightAlignment to the (inherited from RichControl) appropriate LeftRightAlignment to support right- to-left text. Scale (inherited from Overloaded. Scales the control and any child Control) controls. ScaleCore (inherited from Performs the work of scaling the entire control Control) and any child controls. Select (inherited from Activates this control. Control) SelectNextControl Selects the next control following ctl. (inherited from Control) SendMessage (inherited Overloaded. Sends a Win32 message to the from Control) control. SendMessage (inherited Overloaded. Sends a Win32 message to the from Control) control. SendToBack (inherited Sends this control to the back of the z-order. from Control) SetAutoScrollMargin Sets the size of the auto-scroll margins. (inherited from ScrollableControl) SetBounds (inherited Overloaded. Sets the bounds of the control. from Control) SetBoundsCore (inherited Performs the work of setting the bounds of the from RichControl) control. SetClientSizeCore Performs the work of setting the size of the (inherited from Control) client area of the control. SetDesktopBounds Sets the bounds of the form in desktop coordinates. SetDesktopLocation Sets the location of the form in desktop coordinates. SetLocation (inherited Sets the location of this control. from Control) SetNewControls Arranges an array of controls on a form. SetSize (inherited from Sets the size of this control. Control) www.syngress.com Table 7.2 Continued Method Description Continued 153_VBnet_07 8/15/01 12:31 PM Page 285 286 Chapter 7 • Creating Windows Forms SetStyle (inherited from Sets the current value of the specified bit in Control) the control’s style. NOTE: This is control style, not the Win32 style of the hwnd. ShouldPersistAutoScrollMargin Indicates whether the AutoScrollMargin (inherited from property should be persisted. ScrollableControl) ShouldPersistAutoScrollMinSize Indicates whether the AutoScrollMinSize (inherited from property should be persisted. ScrollableControl) ShouldPersistAutoScrollPosition Indicates whether the AutoScrollPosition (inherited from property should be persisted. ScrollableControl) ShouldPersistBackColor Indicates whether the BackColor property should be persisted. ShouldPersistBindings Indicates whether bindings should be (inherited from RichControl) persisted. ShouldPersistCursor (inherited Returns true if the cursor should be persisted from RichControl) in code gen. ShouldPersistFont (inherited Returns true if the font should be persisted from RichControl) in code gen. ShouldPersistForeColor Indicates whether the ForeColor property should be persisted. ShouldPersistIcon Indicates whether the Icon property should be persisted. ShouldPersistLocation Determines if the Location property needs to (inherited from Control) be persisted. ShouldPersistRightToLeft Returns true if the RightToLeft should be (inherited from RichControl) persisted in code gen. ShouldPersistSize (inherited Determines if the Size property needs to be from Control) persisted. ShouldPersistText (inherited Determines if the Text property needs to be from Control) persisted. ShouldPersistTransparencyKey Indicates whether the TransparencyKey property should be persisted. Show (inherited from Control) Makes the control display by setting the visible property to true. www.syngress.com Table 7.2 Continued Method Description Continued 153_VBnet_07 8/15/01 12:31 PM Page 286 Creating Windows Forms • Chapter 7 287 ShowDialog Overloaded. Displays this form as a modal dialog box. SuspendLayout (inherited Suspends the layout logic for the control. from Control) ToString (inherited from Object) Returns a String that represents the current Object. Update (inherited from Control) Forces the control to paint any currently invalid areas. UpdateBounds (inherited from Overloaded. Updates the bounds of the Control) control. UpdateStyles (inherited from Forces styles to be reapplied to the handle. Control) This function will call CreateParams to get the styles to apply. UpdateZOrder (inherited from Updates this control in its parent’s z-order. Control) Validate (inherited from Validates the last unvalidated control and its ContainerControl) ancestors up through, but not including, the current control. WndProc Processes Windows messages. WndProcException (inherited Processes Windows exceptions. from RichControl) Similarly, you can use other methods to achieve the behavior that you need. In the following sections, we will look how to create forms that have a specific application. Creating Windows Forms The form is the primary vehicle for user interaction within a Windows-based application.You can combine controls and code to collect information from the user and respond to it, work with data stores, and query and write to the Registry and file system on the user’s computer.To achieve these results,Visual Basic .NET allows you to create modal, modeless, and top-most forms; we will discuss these form types in the following sections.Visual Basic .NET also allows you to create new instances of a form in different ways. For example, each of the following snippets creates a new instance frmNewDialog of a form frmDialog: www.syngress.com Table 7.2 Continued Method Description 153_VBnet_07 8/15/01 12:31 PM Page 287 288 Chapter 7 • Creating Windows Forms Dim frmNewDialog As frmDialog() frmNewDialog = New frmDialog() Or: Dim frmNewDialog As New frmDialog() Or: Dim frmNewDialog As frmDialog = New frmDialog() Notice how the Set keyword is conspicuously absent.Also notice the paren- theses following the form class. In Visual Basic .NET, parentheses are added to the names of forms, classes, and collections—if you omit them, the code editor will add them for you. In the first snippet, declaring the new form frmNewDialog as type frmDialog allows to you access the properties and methods of the frmDialog class using the Complete Word window. However, a new instance of the form is not created until the second statement, which includes the New keyword. As with all objects in the .NET Framework, forms are instances of classes. When you add a form, you can choose whether it inherits from the Form class provided by the framework, or from a form you’ve previously created.The frame- work also allows you to inherit from existing forms to add functionality or modify existing behavior. Displaying Modal Forms A modal form must be closed before you can continue working with the rest of the application. In many Windows-based applications, the user needs to click OK or Cancel on a dialog box to be able to switch to another form:The dialog box is modal. Modal dialog boxes are useful when displaying important messages because they require an acknowledgement from the user.You can display a form as a modal dialog box by using the ShowDialog method.The following snippet shows just how: Dim frmProperties As frmDialog = New frmDialog() frmProperties.ShowDialog() You should be familiar with code execution following the ShowDialog method. If a form is shown modally, the code following the ShowDialog method does not execute until the form is closed.This differs from code execution if a form is shown as a modeless, as you will see in the next section. www.syngress.com 153_VBnet_07 8/15/01 12:31 PM Page 288 Creating Windows Forms • Chapter 7 289 Displaying Modeless Forms Contrary to a modal form, a modeless form allows the user to shift the focus between the form and another form without closing the initial form. Modeless forms are useful when you want the user to refer to one form from another, such as with tool windows and Help windows. However, modeless forms can be a handful because the user can access them in an unpredictable order.This compli- cates your task as the developer to keep the state of your application consistent. You can easily display a form as a modeless dialog box.To display a modeless dialog box, use the Show method, as shown in the following code: Dim frmToolbox As frmDialog = New frmDialog() frmToolbox.Show() Execution of code following the Show method differs from execution of code following the ShowDialog method.When a form is shown modelessly, the code following the Show method is executed immediately after the form is displayed. When showing multiple forms, at times you may want to keep a form on top of other windows.The following section discusses top-most forms. Displaying Top-Most Forms A top-most form stays in front of non-topmost forms even when inactive. In Windows 2000, a top-most form stays in front of other forms within its applica- tion. In Windows 98, a top-most form stays in front of all forms in all applica- tions.Top-most forms are useful for creating floating tool windows and Help windows in front of other windows in your application. In a Windows Forms application, you can easily make a form the top-most form by using the TopMost property.The following snippet shows how: frmToolbox.TopMost = True After creating a form, you will often want to make stylistic changes to it, such as changing its borders, resizing it, or setting its location.We cover these topics in the following sections. Changing the Borders of a Form After you add a form to your project at design time or create a form in code, you can choose from several border styles to determine its look. Apart from control- ling the look of the borders of a form, the BorderStyle property influences how the caption bar is displayed along with the buttons that appear on it. Moreover, www.syngress.com 153_VBnet_07 8/15/01 12:31 PM Page 289 290 Chapter 7 • Creating Windows Forms the BorderStyle property also affects the resizing behavior of a form.Table 7.3 describes the settings for the BorderStyle property. Table 7.3 Settings for the BorderStyle Property Setting Description None No border or border-related elements. Used for startup forms. Fixed 3D Used when 3D border effect is desired. Not resizable. Can include control-menu box, title bar, and Maximize and Minimize buttons on the title bar. Creates a raised border relative to the body of the form. Fixed Dialog Used for dialog boxes. Not resizable. Can include control-menu box, title bar, and Maximize and Minimize buttons on the title bar. Creates a recessed border relative to the body of the form. Fixed Single Not resizable. Can include control-menu box, title bar, and Maximize and Minimize buttons. Resizable using only Maximize and Minimize buttons. Creates a single line border. Fixed Tool Window Used for tool windows. Displays a nonsizable window with a Close button and title bar text in a reduced font size. The form does not appear in the Windows taskbar. Sizable (Default) Often used as main window. Resizable. Can include control-menu box, title bar, Maximize button, and Minimize button. Can be resized using control- menu box, Maximize and Minimize buttons on the title bar, or by using the mouse pointer at any edge. Sizable Tool Window Used for tool windows. Displays a sizable window with a Close button and title bar text in a reduced font size. The form does not appear in the Windows taskbar. N OTE All border styles except the None setting feature the Close button on the right-hand side of the title bar. www.syngress.com 153_VBnet_07 8/15/01 12:31 PM Page 290 Creating Windows Forms • Chapter 7 291 You can set the border style of a form at design time or at runtime.To set the border style of a form at design time: 1. From the View menu, click Properties Window. 2. In the Properties Window, click BorderStyle and select the appro- priate border style. You can also change the border style at runtime using one of the values of the FormBorderStyle enumeration. For example, the following sample code set the border style of a form to FixedDialog: frmProperties.BorderStyle = FormBorderStyle.FixedDialog If you choose a border style that allows a Maximize and Minimize button in the title bar, you can choose to disable either or both of the buttons.This is handy when you are satisfied with all attributes of a particular border style except the Maximize or Minimize button.You can disable the Maximize and Minimize buttons using the MaximizeBox and MinimizeBox properties.The following snippet disables the Maximize button of a form: frmProperties.MaximizeBox = False You can disable the Minimize button in similar fashion.The following section discusses resizing forms. Resizing Forms As in previous version of Visual Basic, you can use the Width and Height proper- ties to resize a form. However,Visual Basic .NET also allows you to resize a form by setting its Size property. In addition, in Visual Basic .NET you can quickly change form size by increments.Which method you use to resize a form depends largely on your preference. First, let’s look at how to resize a form the old-fashioned way, by setting the Width and Height properties.This is useful when you want to change either form width or form height, and not both.The following snippet sets the form height to 50 pixels: frmPalette.Height = 50 You can achieve the same result by using the Size object, which specifies the width and the height in that order.The following code also changes only the www.syngress.com 153_VBnet_07 8/15/01 12:31 PM Page 291 292 Chapter 7 • Creating Windows Forms form height to 50 pixels.The frmPalette.Width parameter maintains the current width of the form: frmPalette.Size = New Size(frmPalette.Width, 50) We have now revealed the true power of the Size object: to change both height and width in one statement. For example, you could set the form size to 50 by 50 pixels as follows: frmPalette.Size = New Size(50, 50) In Visual Basic .NET you can also quickly change form size by increments. The following example sets the form height to 50 pixels higher than the current setting: frmPalette.Height += 50 WARNING Do not try to implicitly set the width and height of the Size object to quickly change the form size by increments. The following code will not change the form size. The Size property returns a Size structure con- taining a copy of the form width and height, and the height member of this copied structure is incremented by 50. However, the copied and incremented structure is then discarded: frmPalette.Size.Height += 50 Setting Location of Forms After you create a form, you can specify where it is to be displayed on the com- puter screen.When a form first appears, the StartPosition property determines the position of the form.The default setting of the StartPosition is WindowsDefaultLocation, which allows the operating system to compute the best location for the form at startup based on the hardware. For example, the user may have a system with multiple monitors or a different screen size and resolution, which can cause the form location to change unpredictably. www.syngress.com 153_VBnet_07 8/15/01 12:31 PM Page 292 Creating Windows Forms • Chapter 7 293 NOTE A form’s location as you see it may differ from the form’s location as the user sees it. To an extent, you can control the location of a form using its Location prop- erty.You can change the x-coordinate and the y-coordinate of a form by using the Left and To p properties, as in previous versions of Visual Basic.The following example changes the form’s y-coordinate to the 100-pixel point: frmPalette.Top = 100 In Visual Basic .NET, you can also achieve the same result by using the Location object and its X and Y properties.The following snippet also adjusts the form’s y-coordinate to the 100-pixel point: frmPalette.Location.Y = 100 However, the power of the Location object lies in that you can use it to change both the x-coordinate and the y-coordinate of a form simultaneously.The following code adjusts both the x-coordinate and the y-coordinate to the respec- tive 100-pixel points: frmPalette.Location = New Point(100, 100) WARNING Do not try to implicitly set the x-coordinate and y-coordinate of the Location object to quickly change the form’s location by increments. The following code will not change the form’s location. The Location prop- erty returns a Location structure containing a copy of the form’s x-coor- dinate and y-coordinate, and the y-coordinate of this copied structure is incremented by 100. However, the copied and incremented structure is then discarded: frmPalette.Location.Y += 100 www.syngress.com 153_VBnet_07 8/15/01 12:31 PM Page 293 [...]... point (50 , 50 ): txtLabel.Location = New Point (50 , 50 ) www.syngress.com 153 _VBnet_07 8/ 15/ 01 12:31 PM Page 3 05 Creating Windows Forms • Chapter 7 You can also use the Left and Right properties or the X and Y properties of the Location object to change one control coordinate at a time Both of the following statements adjust the x-coordinate of the text box to the 50 -pixel point: txtLabel.Left = 50 txtLabel.Location.X... method of the parent form and the MDILayout enumeration.You can choose from four values of the MDILayout enumeration, which are described in Table 7 .5 www.syngress.com 299 153 _VBnet_07 300 8/ 15/ 01 12:31 PM Page 300 Chapter 7 • Creating Windows Forms Table 7 .5 Settings of the MDILayout Enumeration Setting Description ArrangeIcons Displays child form icons arranged along the lower portion of the parent... ControlRemoved (inherited from Control) Occurs when the form is closed Occurs when the form is closing Occurs when a new form is added Occurs when a form is removed Continued www.syngress.com 153 _VBnet_07 8/ 15/ 01 12:31 PM Page 2 95 Creating Windows Forms • Chapter 7 Table 7.4 Continued Event Description Deactivate Occurs when the form loses focus and is not the active form DoubleClick (inherited from Occurs when... (inherited from Occurs when a key is pressed while the form Control) has focus KeyUp (inherited from Occurs when a key is released while the form Control) has focus Continued www.syngress.com 2 95 153 _VBnet_07 296 8/ 15/ 01 12:31 PM Page 296 Chapter 7 • Creating Windows Forms Table 7.4 Continued Event Description Layout (inherited from Control) Leave (inherited from Control) LostFocus (inherited from Control)... adjust the x-coordinate of the text box to the 50 -pixel point: txtLabel.Left = 50 txtLabel.Location.X = 50 You can also quickly change a control’s location by increments.The following example adjusts the x-coordinate of our text box to 50 pixels farther than the current setting: txtLabel.Left += 50 WARNING Do not try to implicitly set the x-coordinate and y-coordinate of the Location object to quickly... control-menu box, and Maximize and Minimize buttons (but they usually do not include the latter three) s A dialog box has a recessed border relative to the body of the form www.syngress.com 3 05 153 _VBnet_07 306 8/ 15/ 01 12:31 PM Page 306 Chapter 7 • Creating Windows Forms You can use the dialog boxes that are predefined in the NET Framework or create your own Displaying Message Boxes A message box displays... AnyColor Indicates the name used in code to identify the dialog Enables and disables the Define Custom Colors button Controls whether any color can be selected Continued www.syngress.com 153 _VBnet_07 8/ 15/ 01 12:31 PM Page 3 15 Creating Windows Forms • Chapter 7 Table 7.11 Continued Property Description Color FullOpen The color selected in the dialog Controls whether the custom color section of the dialog... the Network button is displayed For example, you can use the AllowPrintToFile property to enable the Print To File check box Let’s look at how this would appear in code: www.syngress.com 3 15 153 _VBnet_07 316 8/ 15/ 01 12:31 PM Page 316 Chapter 7 • Creating Windows Forms With PrintDialog1 AllowPrintToFile = True ShowDialog() End With The Print dialog box is related to the Print Preview dialog box, which... would look like the following snippet: cboTopLeftRight.Anchor = AnchorStyles.TopLeftRight NOTE Windows Forms controls are anchored to the top and left form edges by default www.syngress.com 301 153 _VBnet_07 302 8/ 15/ 01 12:31 PM Page 302 Chapter 7 • Creating Windows Forms Figure 7.3 The Combo Box Anchored to the Top, Left, and Right Edges of a Form after Resizing You can choose from 16 different anchor... anchored to any edges of its container The control is anchored to the right edge of its container Bottom BottomLeft BottomLeftRight BottomRight Left LeftRight None Right Continued www.syngress.com 153 _VBnet_07 8/ 15/ 01 12:31 PM Page 303 Creating Windows Forms • Chapter 7 Table 7.6 Continued Setting Description Top TopBottom The control is anchored The control is anchored its container The control is anchored . enumeration, which are described in Table 7 .5. www.syngress.com 153 _VBnet_07 8/ 15/ 01 12:31 PM Page 299 300 Chapter 7 • Creating Windows Forms Table 7 .5 Settings of the MDILayout Enumeration Setting. control. Control) www.syngress.com Table 7.2 Continued Method Description Continued 153 _VBnet_07 8/ 15/ 01 12:31 PM Page 2 85 286 Chapter 7 • Creating Windows Forms SetStyle (inherited from Sets the current. following code also changes only the www.syngress.com 153 _VBnet_07 8/ 15/ 01 12:31 PM Page 291 292 Chapter 7 • Creating Windows Forms form height to 50 pixels.The frmPalette.Width parameter maintains