Just got this book and I am really enjoying it. However, I do most of my programming in
VB while all the examples so far are in C#. It's not too hard to convert the code examples in
VB, but I had a bit of a problem getting myFirstWebPart to run. However, it's working fine now, so I thought I'd add my
VB version here just in case anyone else has trouble
Code:
Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
<ToolboxItemAttribute(false)> _
Public Class MyFirstWebPart
Inherits WebPart
Dim lblUserEntry As Label = New Label
Dim txtbxUserEntry As TextBox = New TextBox
Dim lblFinalCost As Label = New Label
Dim txtbxFinalCost As TextBox = New TextBox
Dim btnCalcTax As Button = New Button
Dim totalTax As Double = 0.0
Dim prodTax As Double = 0.11
Protected Overrides Sub CreateChildControls()
lblUserEntry.Text = "Cost of Widget:"
lblFinalCost.Text = "Final Cost: "
btnCalcTax.Text = "Calc."
txtbxUserEntry.Text = "59.30"
Me.Controls.Add(lblUserEntry)
Me.Controls.Add(txtbxUserEntry)
Me.Controls.Add(New LiteralControl("<p>"))
Me.Controls.Add(lblFinalCost)
Me.Controls.Add(txtbxFinalCost)
Me.Controls.Add(New LiteralControl("<p>"))
Me.Controls.Add(btnCalcTax)
'From c#: btnCalcTax.Click += new EventHandler(btnCalcTax_Click)
AddHandler btnCalcTax.Click, AddressOf btnCalcTax_Click
'From c#: Base.CreateChildControls()
MyBase.CreateChildControls()
End Sub
Private Sub btnCalcTax_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim prodCost As Double = Convert.ToDouble(txtbxUserEntry.Text)
totalTax = Math.Round(prodCost - (prodCost * prodTax), 2) * 100 / 100
txtbxFinalCost.Text = totalTax.ToString
End Sub