[ Pobierz całość w formacie PDF ]
.NET controls, such as the height of acombo box.Location propertyThis property allows you to specify the location of a control with respect tothe top left corner of its container.The property takes a Point data type,which represents an x and y coordinate pair.This property replaces the Topand Left properties used in Visual Basic 6. Module 6: Using Windows Forms 33Text PropertyIn earlier versions of Visual Basic, you used different methods to set the textdisplayed in the various controls.For instance, Forms and Label controlshave a Caption property, whereas TextBox controls have a Text property.InVisual Basic.NET, any textual property of a control is determined by the Textproperty.This provides consistency within Visual Basic, and with theother.NET -compatible languages.The following example shows how to initialize a Button control in theForm_Load or InitializeComponent procedures.Button1.Top = 20Button1.Height = 50Button1.Left = 20Button1.Width = 120Button1.Text = "Click Me"34 Module 6: Using Windows FormsUsing Control MethodsTopic ObjectiveTo explain some of the newmethods common to manycontrols.BringToFront and SendToBackLead-inButton1.BringToFront(Button1.BringToFront( ))Many controls share someButton2.SendToBack(Button2.SendToBack( ))new methods.FocusTextBox1.Focus(TextBox1.Focus( ))TextBox1.SelectAll(TextBox1.SelectAll( ))Many of the Windows Forms controls share some new common methodsbecause they inherit from the same base classes.BringToFront and SendToBackYou can use the BringToFront method of a control to place it in front of othercontrols, and the SendToBack method to place it behind all other controls.Inearlier versions of Visual Basic, you can achieve this functionality by settingthe ZOrder property of a control.The following example shows how torearrange the order of controls at run time:Button1.BringToFront( )Button2.SendToBack( )FocusYou can use this method to set the focus to a specific control.It is similar to theSetFocus method used in Visual Basic 6.The following example shows howto check the Text property of a TextBox control and return focus to the controlif the text is not valid:If TextBox1.Text "password" ThenMessageBox.Show("Incorrect password")TextBox1.Focus( )TextBox1.SelectAll( )End IfWhen trapping focus events, you should use the Enter and Leave events, ratherthan the GotFocus and LostFocus events. Module 6: Using Windows Forms 35Creating MenusTopic ObjectiveTo explain the new changesto menu creation.Menu ClassesLead-inMenus have been greatlyCreating Menus at Design Timeenhanced inUse the Menu DesignerVisual Basic.NET.Creating Menus at Run TimeDimDim mnuMain As New MainMenu( )mnuMain As New MainMenu( )DimDim mnuItem1 As New MenuItem, mnuItem2 As New MenuItem( )mnuItem1 As New MenuItem, mnuItem2 As New MenuItem( )mnuItem1.TextmnuItem1.Text = "File"= "File"mnuMain.MenuItems.Add(mnuItem1)mnuMain.MenuItems.Add(mnuItem1)mnuItem2.TextmnuItem2.Text = "Exit"= "Exit"mnuMain.MenuItems(0).MenuItems.Add(mnuItem2)mnuMain.MenuItems(0).MenuItems.Add(mnuItem2)AddHandler mnuItem2.Click,AddHandler mnuItem2.Click, AddressOf NewExitHandlerAddressOf NewExitHandlerMenuMenu = mnuMain= mnuMainIn Visual Basic.NET, the process of creating menus is very different from thatof Visual Basic 6.You can have more than one menu system per form, whichreduces the complexity of creating dynamic menus, and you can createContextMenus directly without designing them as top-level menus first.Menu ClassesThere are three main classes that you will use when creating menus:MainMenuYou use the MainMenu class to create a standard Windows menu bar at thetop of a form.ContextMenuYou use the ContextMenu class to define pop-up menus associated withparticular controls.MenuItemYou use the MenuItem class to define menu items within a MainMenu or aContextMenu.36 Module 6: Using Windows FormsCreating Menus at Design TimeYou can use the Menu Designer to create your menus at design time, which isDelivery Tipsomething you cannot do in Visual Basic 6.You can also design and edit yourDemonstrate how to add amenus in-place, rather than in a separate dialog box.menu to a form by using theMenu Designer.Most students shouldunderstand this conceptquickly, so this should notbe a lengthy discussion.Creating Menus at Run TimeYou can add or edit menus at run time by using the MainMenu, ContextMenu,and MenuItem classes.Each of these classes contains a MenuItems collectionthat has Add and Remove methods.The following example shows how todynamically create menus:Dim mnuMain As New MainMenu( )Dim mnuItem1 As New MenuItem( )Dim mnuItem2 As New MenuItem( )mnuItem1.Text = "File"mnuMain.MenuItems.Add(mnuItem1)mnuItem2.Text = "Exit"mnuMain.MenuItems(0).MenuItems.Add(mnuItem2)AddHandler mnuItem2.Click, AddressOf NewExitHandlerMenu = mnuMain Module 6: Using Windows Forms 37Providing User HelpTopic ObjectiveTo explain the ways inwhich to provide Help.ErrorProvider ControlLead-inThere are several ways toError icon appears next to control, and messageprovide Help inappears like a ToolTip when mouse pauses over iconVisual Basic.NET.Used mainly for data bindingHelpProvider ControlPoints to.chm,.hlp, or.html Help fileControls provide Help information by means ofHelpString or HelpTopic propertiesVisual Basic.NET allows you to create user Help in a number of ways by usingcontrols.Each of these controls is placed in the component tray for anindividual form
[ Pobierz całość w formacie PDF ]