Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 110 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
110
Dung lượng
6,3 MB
Nội dung
Place a button in the bottom-right corner of the form, and set the Anchor property for the button to Bottom and Right. Then place two text boxes in the FlowLayoutPanel, keeping their default sizes. The form you have created should now look much like that in Figure 14-14. Figure 14-14 Now run the application. The initial layout will be similar to the design-time layout. However, if you resize the form to about half of its original width, the layout of the text boxes will change. Since there is no longer enough room for them to be arranged side by side, the arrangement will automatically switch, and the form will look more like that in Figure 14-15. Figure 14-15 Padding and Margin properties To assist in positioning controls in the FlowLayoutPanel, all controls have a new property called Margin. There are settings for Margin.Left, Margin.Right, Margin.Top, and Margin.Bottom. These settings determine how much space is reserved around a control when calculating its automatic position in a FlowLayoutPanel. You can see the Margin property in action by changing the Margin property for one or more of the text boxes in the previous example. If you change all the Margin settings for the first Textbox to 20 pixels, for example, and run the application, the form will look like Figure 14-16. 518 Chapter 14 17_575368 ch14.qxd 10/7/05 11:01 PM Page 518 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 14-16 The first text box now has a 20 pixel separation from all the other controls in the FlowLayoutPanel, as well as a 20 pixel separation from the edges of the FlowLayoutPanel itself. The Padding property is for the FlowLayoutPanel or other container control. When a control is embedded into a FlowLayoutPanel, the Padding.Left, Padding.Right, Padding.Top, and Padding.Bottom properties of the FlowLayoutPanel determine how far away from the inside edge of the container to position the control. You can see the Padding property in action by changing the Padding property for the FlowLayoutPanel in the previous example. If you set all Padding settings to 20 pixels, and reset the Margin property for the first Textbox back to the default, then the form will look like Figure 14-17 in the visual designer. Figure 14-17 519 Windows Forms 17_575368 ch14.qxd 10/7/05 11:01 PM Page 519 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Notice that all the controls in the FlowLayoutPanel are now at least 20 pixels from the edges. The Padding property is also applicable for other container controls, if the contained controls have their Dock property set. If the settings for Padding are not zero, a docked control will be offset from the edge of the container by the amount specified by the Padding property. TableLayoutPanel Control The other new control for dynamic layout is the TableLayoutPanel. This control consists of a table of rows and columns, resulting in a rectangular array of cells. You can place one control in each cell. The dimensions of the columns and rows can be controlled by setting some key properties. For columns, set the number of columns with the ColumnCount property, and then control each individual column with the ColumnStyles collection. When you click on the button for the ColumnStyles collection, you’ll get a designer window that allows you to set two key properties for each column— the SizeType and Width properties. SizeType can be set to one of the following enumerations: ❑ Absolute — Sets the column width to a fixed size in pixels ❑ AutoSize — Indicates that the size of the column should be managed by the TableLayoutPanel, which will allocate width to the column depending on the widest control contained in the column ❑ Percent — Sets the percentage of the TableLayoutPanel to use for the width of the column The Width property is only applicable if you do not choose a SizeType of AutoSize. It sets either the number of pixels for the width of the column (if the SizeType is Absolute) or the percentage width for the column (if the SizeType is set to Percent). Similarly, for rows, there is a RowCount property to set the number of rows, and a RowStyles collection to manage the size of the rows. Each row in RowStyles has a SizeType, which works the same way as SizeType does for Columns, except that it manages the height of the row instead of the width of a col- umn. The Height property is used for rows instead of a Width property, but it works in a corresponding way. Height is either the number of pixels (if SizeType is Absolute) or a percentage of the height of the TableLayoutPanel (if SizeType is Percent). If SizeType is AutoSize, then a row is sized to the height of the tallest control in the row. Extender Provider Controls There is a new family of controls in Windows Forms that can only be used in association with other controls. Each of these controls, called extender provider controls, causes new properties to appear for every other control on the form. Extender provider controls have no visible manifestation, so they appear in the component tray. The three extender provider controls currently available are the HelpProvider, the ToolTip, and the ErrorProvider. All three controls work in basically the same way. Each extender provider control 520 Chapter 14 17_575368 ch14.qxd 10/7/05 11:01 PM Page 520 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com implements the properties that are “attached” to other controls. The best way to see how this works is to go through an example, so let’s do that with a ToolTip control. ToolTip The ToolTip control is the simplest of the extender providers. It adds just one property to each control, named ToolTip on ToolTip1 (assuming the ToolTip control has the default name of ToolTip1). This property works exactly the same way the ToolTipText property works in VB6, and in fact, replaces it. To see this in action, create a Windows Forms application. On the blank Form1 that is created for the project, place a couple of buttons. Take a look at the properties window for Button1. Notice that it does not have a ToolTip property of any kind. Drag over the ToolTip control, which will be placed in the component tray. Go back to the properties window for Button1. A property named ToolTip on ToolTip1 is now present. Set any string value you like for this property. Now run the project, and hover the mouse pointer over Button1. You will see a tooltip containing the string value you entered for the ToolTip on ToolTip1 property. HelpProvider The HelpProvider control allows controls to have associated context-sensitive help available by pressing F1. When a HelpProvider control (named HelpProvider1 by default) is added to a form, all controls on the form get these new properties, which show up in the controls’ Properties window. Property Usage HelpString on HelpProvider1 Provides a pop-up tooltip for the control when shape F1 is pressed while the control has the focus. If the HelpKeyword and HelpNavigator properties (see later) are set to provide a valid reference to a help file, then the HelpString value is ignored in favor of the information in the help file. HelpKeyword on Provides a keyword or other index to use in a help HelpProvider1 file for context-sensitive help for this control. The HelpProvider1 control has a property that indicates the help file to use. This replaces the HelpContextID property in VB6. HelpNavigator on Contains an enumerated value that determines how the HelpProvider1 value in HelpKeyword is used to refer to the help file. There are several possible values for displaying such elements as a topic, an index, or a table of contents in the help file. ShowHelp on Determines whether the HelpProvider control is active HelpProvider1 for this control. 521 Windows Forms 17_575368 ch14.qxd 10/7/05 11:01 PM Page 521 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Filling in the HelpString property immediately causes the control to provide tooltip help when F1 is pressed while the control has the focus. The HelpProvider control has a property to point to a help file (either an HTML help file or a Win32 help file), and the help topic in the HelpTopic property points to a topic in this file. ErrorProvider The ErrorProvider control presents a simple, visual way to indicate to a user that a control on a form has an error associated with it. The added property for controls on the form when an ErrorProvider control is used is called Error on ErrorProvider1 (assuming the ErrorProvider has the default name of ErrorProvider1). Setting this property to a string value causes the error icon to appear next to a control, and for the text to appear in a tooltip if the mouse hovers over the error icon. Here is a screen with several text boxes, and an error icon next to one (with a tooltip). The error icon and tooltip are displayed and managed by the ErrorProvider control, as shown in Figure 14-18. The ErrorProvider control’s default icon is the red circle with an exclamation point. When the Error property for the text box is set, the icon will blink for a few moments, and hovering over the icon will cause the tooltip to appear. The code for this behavior in the example screen is explained in the next topic. Figure 14-18 Properties of Extender Providers In addition to providing other controls with properties, extender provider controls also have properties of their own. For example, the ErrorProvider control has a property named BlinkStyle. When it is set to NeverBlink, the blinking of the icon is stopped for all controls that are affected by the ErrorProvider. 522 Chapter 14 17_575368 ch14.qxd 10/7/05 11:01 PM Page 522 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Other properties of the ErrorProvider allow you to change things such as the icon used, and where the icon will appear in relation to the field that has the error. For instance, you might want the icon to show up beneath a field, instead. You can also have multiple error providers on your form. For example, you may wish to give the user a warning rather than an error. A second error provider with a yellow icon could be used to provide this feature. Working with Extender Provider Controls in Code Setting the Error property in the previous example can be done with the Property window, but this is not very useful for on-the-fly error management. However, setting the Error property in code is not done with typical property syntax. By convention, extender provider controls have a method for each property they need to set, and the arguments for the method include the associated control and the property setting. To set the Error property in the previous example, the following code was used: ErrorProvider1.SetError(txtName, “You must provide a location!”) The name of the method to set a property is the word Set prefixed to the name of the property. This line of code shows that the Error property is set with the SetError (), method of the ErrorProvider. There is a corresponding method to get the value of the property, and it is named with Get prefixed to the name of the property. To find out what the current Error property setting for txtName is, you would use the following line: sError = ErrorProvider1.GetError(txtName) Similar syntax is used to manipulate any of the properties managed by an extender provider control. The discussion of the tooltip provider earlier talked about setting the tooltip property in the Properties window. To set that same property in code, the syntax would be ToolTip1.SetToolTip(Button1, “New tooltip for Button1”) Advanced Capabilities for Data Entry New for Windows Forms 2.0 are a couple of advanced capabilities for data entry. Textbox and Combobox controls now have autocompletion capabilities, and a new MaskedTextbox allows entry of formatted input such as phone numbers. Autocompletion Responsive user interfaces help users accomplish their purposes, thereby making them more productive. One classic way to do this is with autocompletion. An example of autocompletion is IntelliSense in Visual Studio. Using IntelliSense, the user only has to type in a few letters, and Visual Studio presents a list of probable entries matching those letters. If what the user wants is in the list, the user need only indicate that, rather than typing the entire entry. 523 Windows Forms 17_575368 ch14.qxd 10/7/05 11:01 PM Page 523 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Autocompletion is available in Windows Forms 2.0 with text boxes and combo boxes. Both use a set of properties to control how autocompletion works and where the list of entries available to the user comes from. To see autocompletion in action, create a Windows Application project. Drag a text box from the toolbox onto the blank Form1 created for the project. Set the AutocompleteMode for the text box to Suggest in the Property window. Then set the AutocompleteSource to CustomSource. Finally, click the button in the setting window for AutoCompleteCustomSource. You’ll see a window for entering entries that is very similar to the window for entering items for a list box or combo box. Enter the following items into the dialog: Holder Holland Hollis Holloway Holly Holstein Holt Now, start the project, and type “Hol” into the text box. As soon as you start typing, a drop-down will appear that contains entries matching what you’ve typed, which includes all seven elements in the list. If you then type another 1, the list will decrease to four elements that begin with Holl. (See Figure 14-19.) If you then type an o, the list will contain only the entry Holloway. The AutoCompleteMode has two other modes. The Append mode does not automatically present a drop-down, but instead appends the rest of the closest matching entry to the text in the Textbox or ComboBox, and highlights the untyped characters. This allows the closest matching entry to be placed in the textarea without the user explicitly selecting an entry. The SuggestAppend mode combines Suggest and Append. The current best match is displayed in the textarea, and the drop-down with other possibilities is automatically displayed. This mode is the one most like IntelliSense. You can also set the list of items to be included in the autocompletion list at runtime, and this would be the most common usage scenario. A list of items from a database table would typically be loaded for autocompletion. MaskedTextbox Control The new features summary discussed the addition of a MaskedTextbox control, which fills in for the old VB6 MaskedEdit control. If you have used MaskedEdit in VB6, the MaskedTextbox will feel quite familiar. After dragging a MaskedTextbox control to a form, you will typically want to first set the mask associated with the control. You can do this in the property window by selecting the Mask property, but you can also click the right-pointing arrow on the right side of the MaskedTextbox. In either case, you can either construct a mask manually or select one of the commonly used masks from a list. 524 Chapter 14 17_575368 ch14.qxd 10/7/05 11:01 PM Page 524 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com If you need to create your own mask, you will need to design it based on a set of formatting characters: Mask Character Description # Digit placeholder. . Decimal placeholder. The actual character used is the one specified as the decimal placeholder in your international settings. This character is treated as a literal for masking purposes. , Thousands separator. The actual character used is the one specified as the thousands separator in your international settings. This character is treated as a literal for masking purposes. : Time separator. The actual character used is the one specified as the time separator in your international settings. This character is treated as a literal for masking purposes. / Date separator. The actual character used is the one specified as the date separator in your international settings. This character is treated as a literal for masking purposes. \ Treat the next character in the mask string as a literal. This allows you to include the #, &, A, and ? characters in the mask. This character is treated as a literal for masking purposes. & Character placeholder. Valid values for this placeholder are ANSI characters in the following ranges: 32–126 and 128–255. > Convert all the characters that follow to uppercase. < Convert all the characters that follow to lowercase. A Alphanumeric character placeholder (entry required). For example: a–z, A–Z, or 0–9. a Alphanumeric character placeholder (entry optional). 9 Digit placeholder (entry optional). For example: 0–9. C Character or space placeholder (entry optional). This operates exactly like the & placeholder, and ensures compatibility with Microsoft Access. ? Letter placeholder. For example: a–z or A–Z. Literal All other symbols are displayed as literals; that is, as themselves. 525 Windows Forms 17_575368 ch14.qxd 10/7/05 11:01 PM Page 525 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Literal characters are simply inserted automatically by the MaskedTextbox control. If you have literal characters for the parentheses in a phone number, for example, the user need not type these for them to show up in the textarea of the control. As an example of a mask, suppose that you have an account number that must consist of exactly two uppercase letters and five digits. You could construct a mask of >??00000. The first character forces all letters to uppercase. The two question marks specify two required alphabetic characters, and the five zeros specify five required digits. Once you have set the Mask for the MaskedTextbox, all entries into the control will be coerced to the Mask pattern. Keystrokes that don’t fit will be thrown away. You have two options for fetching the text information entered by the user in a MaskedTextbox. The InputText property will get the entry without any of the literal placeholder characters. For example, if you are using a mask for phone number, InputText will just return the ten digits in the phone number. If you want the text exactly as it appears in the control, then you should use the normal Text property. Validating Data Entry Most controls that you place on a form require that their content be validated in some way. A text box might require a numeric value only or simply require that the user provide any value and not leave it blank. The ErrorProvider control just covered makes this task significantly easier than it was in previous versions. To illustrate the use of this control in data validation, create a new Windows application project and change the Text property for the blank Form1 to Validating Demo. Then place two text boxes on the form that will hold a username and password, as shown in Figure 14-19. Figure 14-19 526 Chapter 14 17_575368 ch14.qxd 10/7/05 11:01 PM Page 526 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Name the first text box UserNameTextBox and the second text box PasswordTextBox. You also need to drag an ErrorProvider control onto the form, which will cause it to appear in the Component Tray. In the next section, you’ll add the code that will simply verify that the user has filled in both text boxes and given a visual indication, via the ErrorProvider, if either of the fields has been left blank. The Validating Event The Validating event fires when your control begins its validation. It is here that you need to place your code that will validate your control, and set a visual indication for the error. Insert the following code to see this in action: Private Sub UserNameTextBox_Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles UserNameTextBox.Validating If userNameTextbox.Text = “” Then ErrorProvider1.SetError(UserNameTextBox, “User Name cannot be blank”) Else ErrorProvider1.SetError(UserNameTextBox, “”) End If End Sub Private Sub PasswordTextBox_Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles PasswordTextBox.Validating If passwordTextbox.Text = “” Then ErrorProvider1.SetError(PasswordTextBox, “Password cannot be blank”) Else ErrorProvider1.SetError(PasswordTextBox, “”) End If End Sub Run the program and the tab between the controls without entering any text to get the error message. You’ll see an icon blink next to each of the text box controls, and if you hover over an error icon, you’ll see the appropriate error message. There is also a Validated event that fires after a control’s Validating event. It can be used, for example, to do a final check after other events have manipulated the contents of the control. The CausesValidation Property The CausesValidation property determines if the control will participate in the validation events on the form. A control with a CausesValidation setting of True (it is True by default) will have two effects: ❑ The control’s Validating/Validated events will fire when appropriate. ❑ The control will trigger the Validating/Validated events for other controls. It is important to understand that the validation events fire for a control not when the focus is lost but when the focus shifts to a control that has a CausesValidation value of True. 527 Windows Forms 17_575368 ch14.qxd 10/7/05 11:01 PM Page 527 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... handle it: Me.LayoutMdi(MDILayout.TileVertical) 5 46 Windows Forms To see an example of the rearrangement, suppose that the MDI form in Figure 1 4-2 6 is rearranged with the MDILayout.TileVertical option It would then look similar to the image in Figure 1 4-2 7 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 4-2 7 Dialog Forms In VB6 and earlier, forms were shown with the Show... earlier, forms were shown with the Show method, and this technique is still used in VB. NET In both VB6 and VB. NET, the Show method by default displays modeless forms, which are forms that allow the user to click off them onto another form in the application In VB6 , dialog boxes were displayed with the vbModal parameter (or a hard-coded value of 1) after the form’s Show method This caused the form to be a... Version - http://www.simpopdf.com ❑ ❑ Has the same functionality as in VB6 but with a new Node Tree Editor that allows you to visually design the tree VScrollBar ❑ Unchanged Retired Controls The following list outlines the controls from VB6 that you won’t find in VB. Net and how to reproduce their functionality: ❑ Spinner ❑ ❑ Use the DomainUpDown or NumericUpDown control Line and Shape ❑ ❑ ❑ VB. NET has... key when you do it The screen shot in Figure 1 4-2 3 shows the result after Item1 has been moved and Item3 has been copied a few times Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 4-2 3 Panel and GroupBox Container Controls In VB6 , a Frame control can be used as a container to group controls A set of option buttons (the VB6 version of radio buttons) placed in a frame... are also often used in VB6 to separate areas of a form into functional areas, or to group controls for showing and hiding If a frame is hidden, all the controls in it are hidden Sometimes, frames in VB6 are used with a border (with or without a title for the frame) and other times without a border The functionality in the frame control for VB6 is divided into two controls in VB. NET They are called... between VB6 and VB. NET In VB6 , an MDI parent form can only contain controls that have a property called Align This property determines to which side of the MDI parent form the control is supposed to be docked Typical controls like buttons and text boxes cannot be added directly to an MDI parent form They must be added to a container control, such as a PictureBox, which has an Align property In VB. NET,... VB. NET A Windows Form has a ShowDialog() method that takes the place of the Show method with the vbModal parameter Here is code for showing a modal dialog in VB. NET: Dim frmDialogForm As New DialogForm frmDialogForm.ShowDialog() DialogResult It is common when showing a dialog form to need to get information about what action the user selected This was often done with a custom property in VB6 , but VB. NET... upgraded from its predecessor in VB6 In essence, the DataGrid is a front-end user interface to the data objects in the NET Framework You can find more information on this control in Chapter 16 DateTimePicker ❑ ❑ Formerly known as a DTPicker DomainUpDown — New! ❑ ❑ Can hold a collection of objects and will display the ToString() result of an item in the collection ❑ ❑ A simple one-line version of a list box... ToolStrip will look like the one in Figure 1 4-2 1 Figure 1 4-2 1 You can now handle events on any of these toolbar elements the same way you would any other controls You can double-click to get a Click event routine or access the event routines through the drop-downs in the Code Editor Run your program and, using the mouse, grab the dotted handle on the far-left edge of the toolbar If you drag this to... in the component tray and click the button in the properties window for the Items property You’ll see a designer dialog that looks like Figure 1 4-2 0 529 Chapter 14 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 4-2 0 The drop-down in the upper left contains the different types of items that can be placed on the toolbar Add one each of the following types, with the setting . will look like Figure 1 4-1 6. 518 Chapter 14 17_575 368 ch14.qxd 10/7/05 11:01 PM Page 518 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 4-1 6 The first text box. and password, as shown in Figure 1 4-1 9. Figure 1 4-1 9 5 26 Chapter 14 17_575 368 ch14.qxd 10/7/05 11:01 PM Page 5 26 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Name the. like Figure 1 4-2 0. 529 Windows Forms 17_575 368 ch14.qxd 10/7/05 11:01 PM Page 529 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 4-2 0 The drop-down in the upper