Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Building an Application with Third-Party Controls - Silverlight

Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Client Technologies - Microsoft ilverlight and Rich Internet Applications

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Building an Application with Third-Party Controls - Silverlight

Though Silverlight 1.1 Alpha Refresh does not yet include pre-built controls, a number of third-party control libraries have been created.

Building an Application with Third-Party Controls

 

Though Silverlight 1.1 Alpha Refresh does not yet include pre-built controls, a number of third-party control libraries have been created. One such third-party library is Netika’s GOA WinForms library for Silverlight. This library is an implementation of .NET’s Sys-tem.Windows.Form library for both Silverlight and Flash. This allows us to create Silver-light applications by using .NET desktop applications as templates. The free version of GOA WinForms includes 40+ controls, including buttons, text boxes, calendars and more. Netika’s website at  www.netikatech.com includes demos and documentation for all the controls. To download the library, go to  www.netikatech.com/downloads and select the standard Silverlight version of GOA WinForms. After installation, open Visual Studio 2008 and create a new project. Select Visual Basic, then GOA WinForms VB Application in My Templates. Name this project InterestRateCalculator, as we will be creating a Silverlight application that calculates interest. For a GOA WinForms project, the Visual Basic code-behind file is located in MyForm.vb. In this file, you will find an InitializeComponent function (lines 27–42) that creates a Button. Select Build InterestRateCalculator from the Build menu, then open TestPage.html in your browser to see a sample button.

 

Open up the InterestRateCalculatorForWindows project from the examples directory. We are going to be creating a Silverlight application from this desktop application (Fig. 19.24). First, build and run the project to see how the application looks on the desktop. Next, replace the InitializeComponent function in the InterestRateCalcu-lator project’s MyForm.vb with the InitializeComponent in the InterestRateCalcula-torForWindows project’s InterestRateCalculatorForWindows.Designer.vb. Then replace the Friend WithEvents line (line 26) in the InterestRateCalculator project’s MyForm.vb with the Friend WithEvents lines (lines 139–147) in the InterestRateCal-culatorForWindows project’s InterestRateCalculatorForWindows.Designer.vb file. Finally, copy the btnCalculate_Click function from the InterestRateCalculatorFor-Windows project’s InterestRateCalculatorForWindows.vb into the MyForm class in the InterestRateCalculator project’s MyForm.vb.

 

Try to build the InterestRateCalculator project. You will see several errors. This is because not every property of the Windows Form controls has been implemented in GOA WinForms. Looking at Fig. 19.25, you will see that we commented out lines 47, 60, 82,


100, 118 and 128–129. These lines all accessed properties not supported in GOA Win-Forms. We kept these lines as comments to show you the relatively easy process of con-verting a Visual Basic desktop application to a Silverlight 1.1 application when using GOA WinForms controls.

 

1    ' Fig. 19.25: MyForm.vb

 

2    ' Using third-party controls in Silverlight 1.1 Alpha Refresh.

 

3    Public Class MyForm

 

4          Inherits System.Windows.Forms.Form

5

6          Public Sub New()

 

7                 MyBase.New()

 

8                 InitializeComponent()

 

9             End Sub ' New

10

11         Protected Overloads Overrides Sub Dispose( _

 

12               ByVal disposing As Boolean)

 

13               If disposing Then

 

14                      If Not (components Is Nothing) Then

15                            components.Dispose()

 

16                      End If

 

End If

18               MyBase.Dispose(disposing)

 

19         End Sub ' Dispose

20

21    Private components As System.ComponentModel.IContainer

 

22

23         Private Sub InitializeComponent()

 

24               Me.btnCalculate = New System.Windows.Forms.Button

 

25               Me.txtDisplay = New System.Windows.Forms.TextBox

 

26               Me.lblBalance = New System.Windows.Forms.Label

 

27               Me.updYear = New System.Windows.Forms.NumericUpDown

 

28               Me.lblYears = New System.Windows.Forms.Label

 

29               Me.txtInterest = New System.Windows.Forms.TextBox

 

30               Me.lblInterest = New System.Windows.Forms.Label

 

31               Me.txtPrincipal = New System.Windows.Forms.TextBox

 

32               Me.lblPrincipal = New System.Windows.Forms.Label

 

33               CType(Me.updYear, _

 

34                      System.ComponentModel.ISupportInitialize).BeginInit()

 

35               Me.SuspendLayout()

36

37               ' btnCalculate

 

38               Me.btnCalculate.Location = New System.Drawing.Point(197, 14)

 

39               Me.btnCalculate.Name = "btnCalculate"

 

40               Me.btnCalculate.Size = New System.Drawing.Size(75, 23)

 

41               Me.btnCalculate.TabIndex = 17

 

42               Me.btnCalculate.Text = "Calculate"

43

44               ' txtDisplay

 

45               ' the following line was commented out because it accessed

 

46               ' a property that is not supported in GOA WinForms

 

47               ' Me.txtDisplay.BackColor = System.Drawing.SystemColors.Control

 

48               Me.txtDisplay.Location = New System.Drawing.Point(18, 170)

 

49               Me.txtDisplay.Multiline = True

 

50               Me.txtDisplay.Name = "txtDisplay"

 

51               Me.txtDisplay.ReadOnly = True

 

52               Me.txtDisplay.ScrollBars = System.Windows.Forms.ScrollBars.Vertical

 

53               Me.txtDisplay.Size = New System.Drawing.Size(254, 104)

 

54               Me.txtDisplay.TabIndex = 16

 

55               Me.txtDisplay.Font = New Font("Courier New", 10)

56

57               ' lblBalance

 

58               ' the following line was commented out because it accessed

 

59               ' a property that is not supported in GOA WinForms

 

60               ' Me.lblBalance.AutoSize = True

 

61               Me.lblBalance.Location = New System.Drawing.Point(17, 143)

 

62               Me.lblBalance.Name = "lblBalance"

 

63               Me.lblBalance.Size = New System.Drawing.Size(122, 13)

 

64               Me.lblBalance.TabIndex = 15

 

65               Me.lblBalance.Text = "Yearly account balance:"

66

67               ' updYear

 

68               Me.updYear.Location = New System.Drawing.Point(85, 95)

 

69               Me.updYear.Maximum = New Decimal(New Integer() {10, 0, 0, 0})

 

Me.updYear.Minimum = New Decimal(New Integer() {1, 0, 0, 0})

71               Me.updYear.Name = "updYear"

 

72               Me.updYear.ReadOnly = True

 

73               Me.updYear.Size = New System.Drawing.Size(100, 20)

 

74               Me.updYear.TabIndex = 14

 

75               Me.updYear.TextAlign = _

 

76                      System.Windows.Forms.HorizontalAlignment.Right

 

77               Me.updYear.Value = New Decimal(New Integer() {1, 0, 0, 0})

78          

79               ' lblYears

 

80               ' the following line was commented out because it accessed

 

81               ' a property that is not supported in GOA WinForms

 

82               ' Me.lblYears.AutoSize = True

 

83               Me.lblYears.Location = New System.Drawing.Point(17, 102)

 

84               Me.lblYears.Name = "lblYears"

 

85               Me.lblYears.Size = New System.Drawing.Size(37, 13)

 

86               Me.lblYears.TabIndex = 13

 

87               Me.lblYears.Text = "Years:"

88

89               ' txtInterest

 

90               Me.txtInterest.Location = New System.Drawing.Point(85, 58)

 

91               Me.txtInterest.Name = "txtInterest"

 

92               Me.txtInterest.Size = New System.Drawing.Size(100, 20)

 

93               Me.txtInterest.TabIndex = 12

 

94               Me.txtInterest.TextAlign = _

 

95                      System.Windows.Forms.HorizontalAlignment.Right

96

97               ' lblInterest

 

98               ' the following line was commented out because it accessed

 

99               ' a property that is not supported in GOA WinForms

100            ' Me.lblInterest.AutoSize = True

 

101            Me.lblInterest.Location = New System.Drawing.Point(17, 61)

 

102            Me.lblInterest.Name = "lblInterest"

 

103            Me.lblInterest.Size = New System.Drawing.Size(71, 13)

 

104            Me.lblInterest.TabIndex = 11

 

105            Me.lblInterest.Text = "Interest Rate:"

106

107            ' txtPrincipal

 

108              Me.txtPrincipal.Location = New System.Drawing.Point(85, 17)

 

109            Me.txtPrincipal.Name = "txtPrincipal"

 

110            Me.txtPrincipal.Size = New System.Drawing.Size(100, 20)

 

111            Me.txtPrincipal.TabIndex = 10

 

112            Me.txtPrincipal.TextAlign = _

 

113                   System.Windows.Forms.HorizontalAlignment.Right

114

115            ' lblPrincipal

 

116            ' the following line was commented out because it accessed

 

117            ' a property that is not supported in GOA WinForms

 

118            ' Me.lblPrincipal.AutoSize = True

 

119              Me.lblPrincipal.Location = New System.Drawing.Point(17, 20)

 

120            Me.lblPrincipal.Name = "lblPrincipal"

 

121            Me.lblPrincipal.Size = New System.Drawing.Size(50, 13)

 

122            Me.lblPrincipal.TabIndex = 9

 

Me.lblPrincipal.Text = "Principal:"

 

125            ' FrmInterestCalculator

 

126            ' the following two lines were commented out because they

 

127            ' accessed properties that are not supported in GOA WinForms

 

128            ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

 

129            ' Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

 

130            Me.ClientSize = New System.Drawing.Size(289, 288)

 

131            Me.Controls.Add(Me.btnCalculate)

 

132            Me.Controls.Add(Me.txtDisplay)

 

133            Me.Controls.Add(Me.lblBalance)

 

134            Me.Controls.Add(Me.updYear)

 

135            Me.Controls.Add(Me.lblYears)

 

136            Me.Controls.Add(Me.txtInterest)

 

137            Me.Controls.Add(Me.lblInterest)

 

138            Me.Controls.Add(Me.txtPrincipal)

 

139            Me.Controls.Add(Me.lblPrincipal)

 

140            Me.Name = "FrmInterestCalculator"

 

141            Me.Text = "Interest Calculator"

 

142            CType(Me.updYear, _

 

143                   System.ComponentModel.ISupportInitialize).EndInit()

 

144            Me.ResumeLayout(False)

 

145            Me.PerformLayout()

 

146     End Sub ' InitializeComponent

147

148     Friend WithEvents btnCalculate As System.Windows.Forms.Button

 

149     Friend WithEvents txtDisplay As System.Windows.Forms.TextBox

 

150     Friend WithEvents lblBalance As System.Windows.Forms.Label

 

151     Friend WithEvents updYear As System.Windows.Forms.NumericUpDown

 

152     Friend WithEvents lblYears As System.Windows.Forms.Label

 

153     Friend WithEvents txtInterest As System.Windows.Forms.TextBox

 

154     Friend WithEvents lblInterest As System.Windows.Forms.Label

 

155     Friend WithEvents txtPrincipal As System.Windows.Forms.TextBox

 

156     Friend WithEvents lblPrincipal As System.Windows.Forms.Label

157      

158     Public Shared Sub Main()

 

159            Application.Run(New MyForm)

 

160     End Sub ' Main

161

162     Private Sub btnCalculate_Click(ByVal sender As System.Object, _

 

163            ByVal e As System.EventArgs) Handles btnCalculate.Click

164

165            Dim principal As Decimal

 

166            Dim rate As Double

 

167            Dim year As Integer

 

168            Dim amount As Decimal

169      

170            principal = Convert.ToDecimal(Me.txtPrincipal.Text)

 

171            rate = Convert.ToDouble(Me.txtInterest.Text)

 

172            year = Convert.ToInt32(Me.updYear.Value)

173

174            txtDisplay.Text = String.Format("{0,-6}{1}" & vbCrLf, "Year", _

 

175                   "Amount on Deposit")

176

177             For yearCounter As Integer = 1 To year

 

178                   amount = principal * Convert.ToDecimal( _

 

179                         Math.Pow(1 + rate / 100, yearCounter))

 

180                   txtDisplay.Text &= String.Format("{0,-6:D}{1:C}" & vbCrLf, _

 

181                         yearCounter, amount)

182             Next

 

183      End Sub ' btnCalculate_Click

 

184    End Class ' MyForm

 

Fig. 19.25 | Using third-party controls in Silverlight 1.1 Alpha Refresh.

 

 

Build the InterestRateCalculator project, then open up TestPage.html in a web browser. You will see an "AG_E_UNKNOWN_ERROR" error message because the application is not running from a web server. You can safely ignore this error message for now. Test the application, and compare it with the desktop version (Fig. 19.24). Some of the con-trols function slightly differently, as GOA WinForms is not an exact replica of the stan-dard Windows Forms controls.

 

The InitializeComponent function (lines 23–146) was generated in the Interest-RateCalculatorForWindows project using Visual Studio’s design mode. TextBoxes are used to input the principal and interest-rate amounts, and a NumericUpDown control is used to input the number of years for which we want to calculate interest.

 

The btnCalculate_Click function (lines 162–183) handles btnCalculate’s Click event (line 163). Lines 165 and 168 declare two Decimal variables, principal and amount. Line 166 declares rate as type Double, and line 167 declares year as type Integer. Lines 170–171 take the Text from the txtPrincipal and txtInterest text boxes, convert them to the correct type, then store the value in the corresponding variable. Line 172 takes the Value from the updYear NumericUpDown, converts it to an integer, and stores the value to year. Lines 174–175 set the txtDisplay’s Text to display "Year" and "Amount on Deposit" column headers followed by a carriage return. These are formatted using the String.Format method.

 

Lines 177–182 count from 1 to year in increments of 1. Lines 178–179 perform a calculation based on the following formula:

 

a = p(1 + r) n

where a is the amount, p is the principal, r is the rate and n is the year. Lines 180–181 set txtDisplay’s Text to display two columns containing the current yearCounter and amount values.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.