02 VB Net CG
02 VB Net CG
1 Events and Delegates Summary Total Cycle8 OCR 1 Lesson 15: Objectives Getting Started JAM 15.D.1 15.D.2 Using Web Services in a Web Application Summary Total
10 105 5 20 5 30 30 20 5 115
5 15 2 30 30 15 8 12 115 2 45 2 20 15 5 5 10 104
OCR 2
Lesson 16: Objectives Getting Started JAM 16.D.1 Deploying Components JAM Deploying Web Services Summary Total
NIIT
JAM 9.D.1 9.D.2 9.P.1 Creating a Graphical Interface 9.D.3 9.D.4 JAM Summary Total OCR 2 Lesson 10: Objectives Getting Started JAM 10.D.1 Threads in Visual Basic .NET JAM Summary Total Cycle6 OCR 1 Lesson 11: Objectives 11.D.1 11.P.1 11.D.2 11.D.3 11.D.4 Summary Total OCR 2 Lesson 12: Objectives 12.D.1 12.D.2 12.D.3 12.P.1 Summary Total Cycle7 OCR 1 Lesson 13: Objectives Getting Started 13.D.1 13.P.1
NIIT
2 25 15 20 5 15 15 2 5 116 5 25 5 30 30 10 10 115
5 25 15 15 20 20 10 110 5 30 20 20 30 10 115 5 30 30 30
Common Dialog Classes JAM Summary Total Cycle3 OCR 1 Lesson 5 : Objectives Getting Started 5.D.1 5.P.1 Summary Total OCR 2 Lesson 6: Objectives 6.D.1 6.D.2 6.D.3 6.D.4 Summary Total Cycle4 OCR 1 Lesson 7: Objectives 7.D.1 Maintaining Data Concurrency 7.P.1 Summary Total OCR 2 Lesson 8: Objectives Getting Started 8.D.1 8.D.2 8.P.1 8.D.3 Summary Total Cycle5 OCR 1 Lesson 9: Objectives Getting Started
136 Programming in Visual Basic .NET-Coordinator Guide
25 5 5 117
5 30 50 15 10 110 5 15 30 15 30 10 105
5 45 20 30 10 110 5 5 20 20 25 20 10 105
2 10
NIIT
SESSION PLAN
Cycle # Cycle1 OCR 1 Lesson 1: Objectives Getting Started .NET Framework JAM Introduction to Visual Basic .NET Visual Studio .NET IDE Summary Total OCR 2 Lesson 2: Objectives Getting Started Visual Basic .NET Language Features JAM 2.D.1 JAM 2.P.1 Summary Total Cycle2 OCR 1 Lesson 3: Objectives Getting Started 3.D.1 3.D.2 3.P.1 JAM Summary Total OCR 2 Lesson 4: Objectives Getting Started 4.D.1 4.P.1 JAM 5 20 30 20 25 2 5 107 2 30 20 25 5 5 15 15 5 10 45 10 105 5 15 30 5 20 5 20 10 110 Activity/Problem No. Duration (In Mins)
NIIT
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim result As Integer Dim sr As SoccerRanker = New SoccerRanker() sr.PlayerPosition = TextPosition.Text sr.NormalGoalsFor = TextNGoalsFor.Text sr.NormalGoalsAgainst = TextNGoalsAgainst.Text sr.PenaltyGoalsFor = TextPGoalsFor.Text sr.PenaltyGoalsAgainst = TextPGoalsAgainst.Text If sr.PlayerPosition = "MidFielder" Then sr.ShotsTaken = TextShotTaken.Text End If result = sr.CalculatePoints() MsgBox("The total number of points is " & result) End Sub Private Sub TextPosition_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextPosition.LostFocus If Not (TextPosition.Text = "GoalKeeper" Or TextPosition.Text = "MidFielder" Or TextPosition.Text = "Forward") Then MsgBox("Position should be either of GoalKeeper, MidFielder, or Forward (Case Sensitive)") End If End Sub End Class
NIIT
Public Property ShotsTaken() As Integer Get Return shottaken End Get Set(ByVal Value As Integer) shottaken = Value End Set End Property Public Function CalculatePoints() As Double Select Case playposition Case Is = "GoalKeeper" tpoints = (ngoalsfor * 3 + 4 * pgoalsfor) (ngoalsagainst + pgoalsagainst) Return tpoints Case Is = "MidFielder" tpoints = (ngoalsfor * 2) + shottaken Return tpoints Case Is = "Forward" tpoints = (ngoalsfor * 4 + pgoalsfor * 3) (ngoalsagainst + pgoalsagainst) Return tpoints End Select End Function End Class
Q2. Solution: Design a user control and add text boxes and buttons to it. Add the component already created to calculate the number of points earned by a player. Here we have assumed the namespace ( name of the project if no namespace has been specified) to be SoccerLib. Imports SoccerLib Public Class MyUserControl Inherits System.Windows.Forms.UserControl
NIIT
End Property Public Property PenaltyGoalsAgainst() As Integer Get Return pgoalsagainst End Get Set(ByVal Value As Integer) pgoalsagainst = Value End Set End Property Public Property NormalGoalsAgainst() As Integer Get Return ngoalsagainst End Get Set(ByVal Value As Integer) ngoalsagainst = Value End Set End Property Public Property PlayerPosition() As String Get Return playposition End Get Set(ByVal Value As String) playposition = Value End Set End Property Public ReadOnly Property TotalPoints() As String Get Return tpoints End Get End Property
NIIT
NIIT
To be able to use the Web service that contains a Web method for providing details about a query, perform the following steps: 1. 2. Right-click the DisplayCustOrderDet project in the Solution Explorer window and select Add Web Reference from the shortcut menu. This opens the Add Web Reference dialog box. In the Address text box, enter the path of your Web service as shown below: http://localhost/CustOrderData/service1.asmx?WSDL 3. 4. 5. 6. Click the Go To ( ) button.
Click the Add Reference button. On performing this task, Visual Studio .NET will generate the proxy class for the Web service. In the Solution Explorer window, expand the Web References folder. Right-click the localhost folder and select Rename from the shortcut menu. Rename the folder as WebService1. Import the Web service namespace by adding the following code at the top of the code-behind file: Imports DisplayCustOrderDet.WebService1
7.
Type the following code in the WebForm1 class: Private Sub btnGetData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetData.Click Dim WebServiceObj As New CustOrderData() Dim dsQryDet As New DataSet() dsQryDet = WebServiceObj.CustOrder(txtCustID.Text, txtFromDt.Text, txtToDt.Text) dgOrderDet.DataSource = dsQryDet.Tables(0).DefaultView dgOrderDet.DataBind() End Sub
8.
Select Debug Start. In the form set the values of various controls, as given below: Customer ID: C003 From Date: 06/12/2001 To Date: 06/20/2001
9.
Click the Get Data button and check whether the details about orders are displayed in the DataGrid control.
NIIT
6. 7.
In the Solution Explorer window, select Form1.vb and click the View Code button. Import the Web service namespace by adding the following code at the top of the code-behind file: Imports DispCustOrderDet.WebService1
8.
Type the following code in the Form1 class: Private Sub btnGetData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetData.Click Dim table1 As New DataTable() Dim dsOrderData As New DataSet() Dim WebServiceObj As New CustOrderData() dsOrderData = WebServiceObj.CustOrder(txtCustID.Text, dtFromDt.Value.Date.ToString, dtToDt.Value.Date.ToString) table1 = dsOrderData.Tables(0) dgOrderDet.DataSource = table1.DefaultView End Sub
9.
Select Debug Start. In the form set the values of various controls, as given below: Customer ID: C003 From Date: June 12, 2001 To Date: June 20, 2001
10. Click the Get Data button and check whether the details about orders are displayed in the DataGrid control.
UGP 3
1. Select File New Project. In the New Project dialog box, select Visual Basic Projects from the Project Types pane. Select ASP.NET Web Application from the Templates pane. Specify the name of the project as DisplayCustOrderDet and Click the OK button. In the ToolBox, click the Web Forms tab. Create three Label controls and set their Text property to Customer ID, From Date, and To Date, respectively. Create three TextBox controls and set their ID property to txtCustID, txtFromDt, and txtToDt respectively. Create a Button control. Set its Text property to Get Data and ID property to btnGetData. Create a DataGrid control and set its ID property to dgOrderDet. In the Solution Explorer window, click the Show All Files button. Expand WebForm1.aspx. Double-click WebForm1.aspx.vb to switch to the Code Editor window.
2. 3. 4. 5. 6. 7.
NIIT
SqlQry = "Select Inv, Date, CustID, Cost, Advance from CustOrder Where CustID = '" + CustID + "' and [Date] Between '" + StartDt + "' and '" + EndDt + "'" Dim CmdObj As New SqlDataAdapter(SqlQry, SqlConStr) Dim DsObj As New DataSet() CmdObj.Fill(DsObj, "ORDERDATA") Return DsObj End Function 6. 7. Save the file. To test the Web service, select Debug Start. This opens the Internet Explorer window. Click the CustOrder link. This opens a new page. Specify the value of the CustID parameter as C003. Specify the value of the StartDt parameter as 06/10/2001 and the value of the EndDt parameter as 06/20/2001. Click the Invoke button. Check whether the Web service returns the order details in an XML format.
UGP 2
1. Select File New Project. In the New Project dialog box, select Visual Basic Projects from the Project Types pane. Select Windows Application from the Templates pane. Change the name of the project to DispCustOrderDet. Create three Label controls in the form and set their Text property to Customer ID, From Date, and To Date, respectively. Create a TextBox control and set its Name property to txtCustID. Create two DateTimePicker controls and set their Name properties to dtFromDt and dtToDt, respectively. Create a Button control and set its Name property to btnGetData. Set the Text property of the control to Get Data. Create a DataGrid control and set the Name property of the control to dgOrderDet.
2. 3. 4. 5. 6.
To be able to use the Web service that contains a Web method for providing details about a query, perform the following steps: 1. 2. Right-click the DispCustOrderDet project in the Solution Explorer window and select Add Web Reference from the shortcut menu. This opens the Add Web Reference dialog box. In the Address text box, enter the path of your Web service as shown below: http://localhost/CustOrderData/service1.asmx?WSDL 3. 4. 5. Click the Go To ( ) button.
Click the Add Reference button. On performing this task, Visual Studio .NET will generate the proxy class for the Web service. In the Solution Explorer window, expand the Web References folder. Right-click the localhost folder and select Rename from the shortcut menu. Rename the folder as WebService1.
NIIT
End Class End Namespace List the steps that you will follow to create a single deployable unit containing these two files. Ans: To create a single deployable unit containing the two files, CalcDisc.vb and ProductDet.vb, perform the following steps given: 1) Build the GetProdDet class into a module by using the following statement: vbc /t:module GetProdDet.vb /r:system.dll /r:system.data.dll 2) Compile the CalcDiscountAmt class file by executing the following statement at the command prompt: vbc /addmodule:GetProdDet.netmodule /t:module CalcDisc.vb 3) Create a multi-file assembly by using the Al.exe utility by typing the following statement at the command prompt: al /out:App.dll /t:lib GetProdDet.netmodule CalDisc.netmodule
NIIT
privileges, they will not be able to install an application. However, depending on the availability of time, faculty may allow students to create a deployment project and build the .msi file.
Additional Inputs
Demo
The demo for this lesson is stored in the Solutions\VB.NET\Lesson16 folder. To install the Data Entry Application, double-click the Setup.exe file located in the Solutions\VB.NET\Lesson 16\16.D.1\CallCenter\Debug folder.
Ans: c) Registry editor Consider the following files: CalcDisc.VB Imports System.Data Imports System.Data.SQL Imports DBCon Public Class CalcDiscountAmt Call functions from the GetProdDet class Calculate the discounted price End Class
NIIT
4. 5. 6. 7. 8. 9.
In the Address text box, type the address as http://localhost/GetQueryData/service1.asmx?WSDL and press the Enter key. Click the Add Reference button. In the Solution Explorer window, right-click the newly created (localhost) folder, and select Rename from shortcut menu. Type the name of the folder as WebService1. In the Solution Explorer window, select Form1.vb and click the View Code button. Press the Enter key after the Imports statement. Execute the project by selecting Debug Start.
Ans: Components use object model-specific protocols, such as Internet Inter-ORB Protocol (IIOP) and Remote Method Invocation (RMI), for communicating with client applications. In contrast to components, Web services use Internet standards, such as HTTP and XML, to communicate with client applications. This communication approach makes Web services independent of any language or platform. Any computer that has access to the Internet can easily access a Web service. This also enables a number of applications residing on a variety of software and hardware platforms to exchange data. Thus, Web services architecture takes the best features of components and combines them with the Web. 2. How can you locate the Web services developed by a Web service provider?
Ans: Web services providers publish the information about their Web services at centralized locations called Web service directories. They provide information about their Web services by publishing the .disco file. This XML-based file contains links to other resources that describe the Web service. You can use the Add Web Reference dialog box to browse such Web directories and locate a Web service that suits your requirements. 3. Which attribute is used to describe a Web service?
Ans: The WebService attribute is used to provide a description about a Web service.
Lesson Sixteen
Experiences
This lesson discusses various project templates included in Visual Studio .NET for creating deployment projects. Discuss each template and explain the situation in which the template will be used. After explaining deployment project templates, demonstrate the use of various deployment editors. Steps for performing various tasks, such as adding files, registry keys, and launch conditions, are given in the book. You can refer to these steps and demonstrate how each editor can be used for different purposes. The demo given in this lesson uses the Data Entry application created in Lesson 9. Therefore, ensure that the application exists on the computer before you start with this lesson. It is important to note that the setup.exe and the .msi files generated in the demo can be executed only by users belonging to the Administrators group. Since students cannot be given administrative
NIIT
communication protocol for Web services. State that SOAP is an XML-based protocol that runs over HTTP. In other words, SOAP = HTTP + XML. While discussing the demo, state the need for a Web service in the given scenario. Demonstrate each step involved in the creation of a Web service. Discuss the code model for a Web service. Explain the <WebService()> and <WebMethod()> attributes of a Web service. In the next demo, explain the process of locating and using a Web service in an application. In addition, state that the process of locating and using a Web service in an application is the same in case of both Windows- and Web-based applications. This lesson also includes a simple ASP.NET Web application. While discussing this application, discuss some of the commonly used controls in ASP.NET Web application. Demonstrate the use of the design and HTML views of the Web Form Designer. State that the code for application logic can be provided in either the HTML file or the code-behind file.
Additional Inputs
The demos for this lesson are stored in the Solutions\VB.NET\Lesson15\ folder. To be able to work with Web projects, you must have IIS 4.0 or later installed on the computer. In addition, you must be a member of the Administrators or VS Developers group. The instructions for executing demos are given below:
15.D.1
Perform the following steps to create the Web service: 1. 2. 3. 4. 5. Select File New Project. Select Visual Basic Projects from the Project Types pane. Select ASP.NET Web Service from the Templates pane. Specify the location as http://localhost/GetQueryData. Click the OK button. Switch to the Code Editor window by pressing the F7 key. Remove the existing code and paste the code given in the Service1.asmx.vb file provided on the CD. In the code, check value of the SqlConStr variable, which contains the connection string. Change the string as per the requirements. (For example, if the name of the database server is NIIT-CATS, the connection string accordingly.) To execute the Web service, press the F5 key.
7.
15.D.2
The files for this demo are stored in the Solutions\VB.NET\Lesson 15\15.D.2\DisplayQueryStatus folder. If the project does not work properly (it may give an error when you click the Query button on the form), perform the following steps: 1. 2. 3. Open the project in Visual Studio .NET. In the Solution Explorer window, expand the Web References folder. Delete the WebService1 folder by selecting it and pressing the Del key. Right-click the project (DisplayQueryStatus) in the Solution Explorer window and select Add Web Reference from the shortcut menu to invoke the Add Web Reference dialog box.
NIIT
10. Add the following code in the Click event of the Button1: Dim ic As CInterestCalculator = New CInterestCalculator() If (Val(txtPrincipal.Text) > 0) Then ic.PrincipalAmount = txtPrincipal.Text End If If (Val(txtTime.Text) > 0) Then ic.NumberOfYears = txtTime.Text End If If (Val(txtRate.Text) > 0) Then ic.InterestRate = txtRate.Text End If Dim interest = ic.CalculateInterest() Dim amount = ic.CalculateAmount() MsgBox("Total interest is " & interest) MsgBox("Total amount is " & amount) 11. Build the application using the Build Solution option from the Build menu.
Note
You can test the InterestCalculationCtl control by performing the following steps: 1. 2. 3. 4. Create a new Windows Application project with the project name as Interest_Calculation_Test. Add the component InterestCalculationCtl to the Toolbox by selecting Customize Toolbox option from the short-cut menu. Drag the InterestCalculationCtl control to Form1. Execute the application.
Lesson Fifteen
Experiences
Begin the session by revising components. Then, state the limitations of various object models, such as COM and CORBA, in case of Web applications. Then, introduce Web services. State that Web services are reusable components. However, they use HTTP and XML for communicating with client applications. Therefore, they can be used from a computer that has an Internet connection. Discuss various Web service enabling technologies. Explain the role of each of these technologies in the development and discovery of a Web service. In addition, discuss the role of SOAP as a standard
NIIT
End Get End Property Public Function CalculateInterest() As Double interest = (principal * rate * years) / 100 Return interest End Function Public Function CalculateAmount() As Double amount = interest + principal Return amount End Function End Class 4. Build the application using the Build Solution option from the Build menu.
UGP 2
1. 2. 3. Create a new Windows Control Library project with the name Interest_Calculation _Control. Change the class name and the VB file name to InterestCalculationCtl.vb by right-clicking UserControl1.vb and selecting the Rename option from the short-cut menu. Add the component CInterestCalculator to the Toolbox.
Note
To add the component CInterestCalculator, right-click Toolbox and select Customize Toolbox. In the Customize Toolbox dialog box, click the .NET Framework Components tab and then click the Browse button. Navigate to the bin folder under the Interest_Calculation_Component folder and select Interest_Calculation_Component.dll and click the Open button. 4. 5. 6. 7. 8. Add the component CInterestCalculator to InterestCalculationCtl form from the Toolbox.. Add three Label controls and three TextBox controls to the form Form1. The Text property of the Label controls should be Principal, Time, and Rate respectively. Remove the default values of the Text property for all three TextBox controls. Specify the Name property of the TextBox controls as txtPrincipal, txtTime, and txtRate respectively. Add a button to the form. Change the Text property of the button to Calculate. Add the following code as the first line in the Code window:
Imports Interest_Calculation_Component 9. Change the class name InterestCalculationCtl in the code window.
NIIT
Return rate End If End Get Set(ByVal Value As Double) If (Value >= 0) Then rate = Value End If End Set End Property Public Property PrincipalAmount() As Double Get If (principal > 0) Then Return principal End If End Get Set(ByVal Value As Double) If (Value > 0) Then principal = Value End If End Set End Property Public ReadOnly Property TotalInterestAmount() As Double Get Return interest End Get End Property Public ReadOnly Property TotalAmount() As Double Get Return amount
NIIT
18. Drag the CategoryValidatorCtl control to Form1. 19. Execute the application.
Public Class CInterestCalculator Inherits System.ComponentModel.Component Private years As Double Private rate As Double Private principal As Double Private interest As Double Private amount As Double Public Property NumberOfYears() As Double Get If (years > 0) Then Return years End If End Get Set(ByVal Value As Double) If (Value > 0) Then years = Value End If End Set End Property Public Property InterestRate() As Double Get If (rate > 0) Then
NIIT
7.
Note
To add the component CategoryDeterminator, right-click Toolbox and select Customize Toolbox. In the Customize Toolbox dialog box, click the .NET Framework Components tab and then click the Browse button. Navigate to the bin folder under the Category_Calculation_Component folder and select Category_Calculation_Component.dll and click the Open button. 8. 9. Add the component CategoryDeterminator to CategoryValidatorCtl form from the Toolbox. Add three Label controls and three TextBox controls to the form CategoryValidatorCtl. The Text property of the Label controls should be Hours logged, Hours Worked, and Category respectively.
10. Remove the default values of the Text property for all three TextBox controls. Specify the Name property of the TextBox controls as txtHrslogged, txtHrsWorked, and txtCategory respectively. 11. Add a button to the form. Change the Text property of the button to Calculate. 12. Add the following code as the first line in the Code window: Imports Category_Calculation_Component 13. Change the class name CategoryValidatorCtl in the code window. 14. Add the following code in the Click event of the Button1: Dim cd As CategoryDeterminator cd = New CategoryDeterminator() If (Val(txtHrslogged.Text)>0) then cd.HoursLogged = Val(txtHrslogged.Text) End If If (Val(txtHrslogged.Text) > 0) Then cd.HoursWorked = Val(txtHrsWorked.Text) End If txtCategory.Text = cd.CalculateCategory() If (Val(txtHrslogged.Text) < Val(txtHrsWorked.Text)) Then MsgBox("Hours worked cannot be more than Hours logged") End If 15. Build the application using the Build Solution option from the Build menu. 16. Create a new Windows Application project with the project name as Employee_Category_Test. 17. Add the component CategoryValidatorCtl to the Toolbox by selecting Customize Toolbox option from the short-cut menu.
NIIT
If (hrsworked > 0) Then Return hrsworked End If End Get Set(ByVal Value As Double) hrsworked = Value End Set End Property Public Function CalculateCategory() As String Dim productivity As Integer If (hrsworked > 0 And hrslogged > 0) Then productivity = (hrsworked / hrslogged) * 10 Select Case productivity Case 1 To 3 Return "Average" Case 4 To 6 Return "Good" Case 7 To 10 Return "Excellent" End Select End If If (hrsworked = 0 Or hrslogged = 0) Then Return "Enter Correct Values" End If End Function End Class 4. 5. 6. Build the application using the Build Solution option from the Build menu. Create a new Windows Control Library project with the name Employee_Category _Control. Change the class name and the VB file name to CategoryValidatorCtl.vb by right-clicking UserControl1.vb and selecting the Rename option from the short-cut menu.
NIIT
4.
Public Class Form1 Inherits System.Windows.Forms.Form Event UserEvent(dim x as Integer) End Class Ans. Remove Dim keyword from the parameter list 5. Fill in the blank:
Ans. In Visual Basic .NET, delegates are reference types based on the System.Delegates class.
Public Class CategoryDeterminator Inherits System.ComponentModel.Component Private hrslogged As Double Private hrsworked As Double Public Property HoursLogged() As Double Get If (hrslogged > 0) Then Return hrslogged End If End Get Set(ByVal Value As Double) hrslogged = Value End Set End Property Public Property HoursWorked() As Double Get
NIIT
The following is an example of adding a list control when users clicks a button: Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim lstNewListBox As New ListBox() lstNewListBox.size = New Size(150, 200) lstNewlistBox.Location = New Point(50,50) lstNewListBox.Items.Add(First item) lstNewListBox.Items.Add(Second item) Me.Controls.Add(lstNewListBox) End Sub
FAQs
Q1. Can we add a control at run time? Ans. Yes, you can add a control at run time (Refer to Additional Inputs given in lesson 14). Q2. What is side-by-side deployment? Ans. Side-by-side deployment is the process of deploying two different versions of an assembly. Q3. How can we resize more than one control at the same time? Ans. You can resize more than one control at the same time by selecting the controls together.
Ans. The OnPaint() method. 2. What format string will you use to specify a date in the year-month-date format?
Ans. yy-MM-dd 3. How will you ensure that an entire control is redrawn?
Ans. By adding the following code to the constructor of the control: ResizeDraw=True
NIIT
salary = Value Else MsgBox("Employee salary should be between $3000 to $35000") End If End Set End Property End Class Create a test application by the name Employdetails_com. Add a button and three label controls and three textbox controls. Add the following code to the Click event of the button in the test application. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ed As DataValidator = New DataValidator() ed.EmployeeID = TextEmpId.Text ed.EmployeeAge = Val(TextAge.Text) ed.EmployeeSalary = Val(TextSalary.Text) End Sub
Lesson Fourteen
Experiences
Connect to the last unit of components. Tell students that as all controls in Java are JavaBean classes, similarly all controls are components. Emphasize that controls are components that either have a graphical interface or are used in the user interface. To explain user controls, you can use the example of an application in which users can interact with a database. The screen that the users use to view or change data has many controls. All the controls on the screen can be used as one unit. While teaching the rendering of a graphical interface of a customized control, emphasize upon the fact that the OnPaint() method does not belong to controls only. This method needs to be overridden to draw any component. Tell students that they will never call the OnPaint() method directly in their code.
Additional Input
Adding Controls at Run time
You can add controls to forms at run time. This capability was available in the earlier versions of VB also. However, because Visual Basic .NET is based on object-oriented technology, adding controls becomes even easier.
NIIT
Return empId End Get Set(ByVal Value As String) If Value.Length > 5 Then MsgBox("Employee ID should be of the form E123") ElseIf Value Like "[E][0-9][0-9][0-9]" Then empId = Value Else MsgBox("Employee ID should be of the form E123") End If End Set End Property Public Property EmployeeAge() As Integer Get Return age End Get Set(ByVal Value As Integer) If Value >= 21 And Value <= 60 Then age = Value Else MsgBox("Employee age can be from 21 to 60 years") End If End Set End Property Public Property EmployeeSalary() As Double Get Return salary End Get Set(ByVal Value As Double) If Value >= 3000 And Value <= 35000 Then
NIIT
2.
Ans: To avoid DLL Hell, Visual Basic .NET: 1 Isolates applications 2 Enforces Windows NTs Last Known Good 3 Enforces file versioning 3. What is a version number?
Ans: A version number has four parts: the major build version, the minor build version, the build and the revision. It is used by the runtime to determine whether or not two versions are compatible. The major and minor build numbers are used to perform compatibility check. A change in these two numbers signifies a different version. A change in the build number signifies a small change in the form of a patch or bug fixing.
Ans: The program will be developed as a component because then you can use it across applications. 2. Why does an assembly need a shared name?
Ans: An assembly needs a shared name to avoid conflicts with other assemblies in the Global Assembly Cache. 3. How will you use a component?
NIIT
You can tell students that components that deal with business logic, such as calculating price, fit into the middle tier. The presentation tier involves more of visual components. You can compare components to JavaBeans. You can remind them that they had learned about certain standards regarding the writing of a JavaBean. A similar standard is established by IComponent, and in turn, by the Component class of Visual Basic .NET. While covering COM and ActiveX, do not add more content than what is provided in the lesson. Tell students that COM and ActiveX will not be developed in Visual Basic .NET. Emphasize the fact that the use of COM is allowed in Visual Basic .NET for backward compatibility only. Polymorphism is an important concept that the students know well. Here, emphasize upon implementing interfaces.
FAQs
1. What is DLL Hell?
Ans: It is a situation in which one version of a DLL overwrites another version. DLL Hell is a disadvantage that comes with COM. GUIDs are sometimes altered and registry settings are changed, which renders the application that was using the overwritten DLL unable to execute.
NIIT
Instead of: custid = lbcustid.SelectedItem() result = dt.Rows.Find(custid) txtfname.Text = dt.Columns(1).ToString txtlname.Text = dt.Columns(2).ToString txtaddress.Text = dt.Columns(3).ToString txtphone.Text = dt.Columns(4).ToString txtmail.Text = dt.Columns(5).ToString 8. Remove the breakpoint by right-clicking the line and selecting Remove Breakpoint from the shortcut menu. Run the application again.
UGP 3
1. 3. Open the application DiscountApp Modify the code for the Click event of the Discounted Price button as follows: Try cost = txtcost.Text discount = txtdiscount.Text result = discount / 100 * cost discount = cost result MsgBox(discount,MsgBoxStyle.Information, "Result") Catch err As System.InvalidCastException MsgBox(" Please enter a numeric value", MsgBoxStyle.OKOnly, "Error") txtcost.Text = "" txtdiscount.Text = "" txtcost.Focus() End Try
Lesson Thirteen
Experiences
NIIT
27. To associate the created HTML Help file with the HelpProvider control, right-click the HelpProvider1 control from the component tray and select Properties from the shortcut menu. 28. The Help file to be associated with the HelpProvider control is specified using the HelpNamespace property of the HelpProvider control. The HelpNamespace property is present under the Misc category in the Properties window. 29. When the HelpNamespace property is clicked, a button appears to the right in the Properties window. Click the displayed button. 30. In the Open Help File dialog box, select the compiled chm file and click Open. 31. Right-click the CrystalReportViewer control and select Properties from the shortcut menu. 32. Enter Using Main Report Window as the keyword in the HelpKeyword on HelpProvider1 property and select KeywordIndex as the HelpNavigator on HelpProvider1 property. Set the ShowHelp on HelpProvider1 property to True.
UGP 2
1. 2. 3. 4. 5. 6. Open the application CustData. In the Windows Form Designer, double-click the Button1 control. The code for the Click event of Button1 is displayed in the Code Editor window. Right-click the line custid = lbcustid.SelectedItem()in the code. Then, select Insert Breakpoint from the shortcut menu. To run the application, select Debug from the menu bar. Then, select Start from the Debug menu. The application runs till it reaches the breakpoint. Execute the application step-by-step by using Step Into from the Debug menu to check each line of code. Add the following lines: custid = lbcustid.SelectedItem() result = dt.Rows.Find(custid) txtfname.Text = result(1).ToString txtlname.Text = result(2).ToString txtaddress.Text = result(3).ToString txtphone.Text = result(4).ToString txtmail.Text = result(5).ToString
NIIT
S.No.
HTML Title
Content size<br> Search Text - Used to search for a text in the Crystal Report<br>
To be saved as
12. To create an index file, click the Index tab. A message box is displayed confirming whether to create a new index file or open an existing one. 13. By default, Create a new index file radio button is selected. Click OK. 14. In the Save As dialog box, enter the name and the path for the index file and click Save. 15. To insert a keyword, click the Insert a keyword icon on the toolbar. 16. The Index Entry dialog box is displayed. Type Using Group Tree in the Keyword text box and click the Add button. The Path or URL dialog box is displayed. 15. Click the Browse button and select usinggrouptree.htm from the Open dialog box. Click Open. 16. Click OK. Again click OK to close the Index Entry dialog box. 17. Similarly, insert the following keywords:
Keyword Using Main Report Window Using Report Toolbar File to linked with the keyword usingmainreportwindow.htm usingtoolbar.htm
18. To create a content page, click the Contents tab. A message box is displayed confirming whether to create a new contents file or opening an existing one. Click OK. Enter the path and name for the Contents file as tocforCrystalReportHelp and click Save. 19. Click the Insert a page icon on the toolbar. 20 In the Table of Contents Entry dialog box, type Using Group Tree in the Entry title text box and enter usinggrouptree.htm in the File or URL text box. 21. Similarly, add the following content pages:
Entry title Using Main Report Window Using Report Toolbar File to linked with the title usingmainreportwindow.htm usingtoolbar.htm
22. You can specify a default HTML file for your project. When the Help file is displayed to a user, the HTML file set as the default file for the project is opened and displayed automatically. To set a default file for the project, click the Project tab and then click the Change project options icon from the toolbar. 23. Enter the file usingmainreportwindow.htm as the default file in the Default file combo box and click OK. 24. Compile the Project file. 25. Open the application project and open the form designer. 26. Drag a HelpProvider control to the form.
NIIT
7. 8. 9.
To create a new HTML page, click File from the menu bar and then click New from the File menu. Select HTML File in the New dialog box and click OK. Enter the title for the HTML page as Using the group tree and click OK.
10. Write the following text after the <Body> tag in the HTML source page in the right side of the window and save the HTML page as usinggrouptree.htm. Group tree is used to display the groups into which the Crystal Report is divided. For example, if the Crystal Report is grouped to display data on a weekly basis, the Group Tree would contain the weeks into which the Crystal Report has been broken down.<br> To view the data for a group, click that group in the Group Tree.<br> The data for the group is outlined with a red line. 11. Similarly, create HTML pages as displayed below:
S.No. 1. HTML Title Using the Main Report Window Content The Main Report window is used to display the Crystal Report. The data of the Crystal Report can be viewed in the Main Report window.<br> This window displays: Field names<br> Field values<br> Page header<br> Report header<br> Page footer<br> Report footer<br> The various toolbar icons of the Report view are:<br> Go to First Page - Used to move to the first page of the Crystal Report<br> Go to Previous Page - Used to move to the previous page of the Crystal Report<br> Go to Next Page- Used to move to the next page of the Crystal Report<br> Go to Last Page - Used to move to the last page of the Crystal Report<br> Goto Page- Used to move to the page specified by the user<br> Close Current View - Used to close the currently open Crystal Report view <br> Print Report - Used to print the Crystal Report<br> Refresh - Used to refresh the Crystal Report view<br> Export Report - Used to export the Crystal Report to other formats, such as Adobe Acrobat, Microsoft Excel, Microsoft Rich Text, HTML, and Microsoft Word<br> Toggle Group Tree - Used to toggle the state of the Group Tree to the opposite of the current state. For example, if the Group Tree is visible in the Crystal Report, clicking the Toggle Group Tree hides the Group Tree, and if the Group Tree is hidden, clicking the Toggle Group Tree icon performs the opposite action<br> Zoom - Used to display the Crystal Report in a customized To be saved as usingmainreportwindow.htm
2.
usingtoolbar.htm
NIIT
6. 7. 8.
In the Properties window of the ToolBar control, click the ImageList property and then select ImageList1 from the drop-down list. Click the Buttons property and then click the button that appears to the right of the Properties window. The ToolBarButton Collection Editor is displayed. To add a button in the ToolBar control, click the Add button. ToolBarButton1 is added in the Members list. 1. 2. 3. Click the ImageIndex property of ToolBarButton1 to represent the button as an image. From the drop-down list, select 0. The image displayed next to the index number is the image of the button. Set the Text property of ToolBarButton1 as blank.
9. 10.
Similarly, add two more buttons to the toolbar and add images for them and click OK to close the ToolBarButton Collection Editor. Add the following code for the Load event of the form: Dim response As MsgBoxResult response = MsgBox("Do you want to view tool tips for the toolbar icons", MsgBoxStyle.YesNo, "Information") If response = MsgBoxResult.Yes Then ToolBarButton1.ToolTipText = "Save" ToolBarButton2.ToolTipText = "Open" ToolBarButton3.ToolTipText = "New" End If
11.
NIIT
Lesson Twelve
Experiences
This session contains more practice than theory. Start the session by explaining a scenario in which a new user might need help while working with an application. The user might need to know about how to perform an action or information about an application component, such as buttons and icons. The students should clearly understand the function of a HelpProvider control and its properties. All the three demos use the same Windows application. Therefore, before starting 12.D.2 and 12.D.3, ask the students to remove the HelpProvider control and all other setting added while doing the previous demos.