简体   繁体   中英

How to add a table from toolbox and write vb.Net code in visual studio 2012 express

I am learning VB.net. I am following Charpter 3 of Brian Siler's special edition (2001) using MS VB.net. It looks thing has been changed not too much with VB, but I met a problem in adding table from toolbox.

In the Toolbox (versions from Visual Studio 2012 Express; ASP.net 2012), to build a VB.net project, there are items like TextBox and Table. To build a web app, I can add TextBox, Label, etc and coding them without problems. However, when I add table, there is a problem.

As with Textbox and Label, I rename the table ID (to tblAmortize ) first. Then I input the codes from their book, and got an error:

'tblAmortize' is not declared. It may be inaccessible due to its protection level"

As I know, the TextBox item, such as txtPrincipal.Text does not need to be declared when we use it. But it seem this "table" tblAmortize item needs to be declared first, or needs to be build up a link because tblAmortize is located in Form 2 while btnShowDetail_Click is in Form 1 . The full code is as following:


Public Class Hello_Web
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim intTerm As Integer                'Term, in years
        Dim decPrincipal As Decimal           'Principal amount $
        Dim decIntRate As Decimal             'Interst rate %
        Dim dblPayment As Double              'Monthly payment $

        Dim decMonthInterest As Decimal       'Interest part of monthly payment
        Dim decTotalInterest As Decimal = 0   'Total interest paid
        Dim decMonthPrincipal As Decimal      'Principal part of monthly pament
        Dim decTotalPrincipal As Decimal = 0  'Remaining principal

        Dim intMonth As Integer               'Current month
        Dim intNumPayments As Integer         'Number of monthly payments
        Dim i As Integer                      'Temporary loop counter
        Dim rowTemp As TableRow               'Temporary row object
        Dim celTemp As TableCell              'Temporary cell object

        If Not IsPostBack Then        'Evals true first time browser hits the page
            'SET Up THE TABLE HEADER ROW
            rowTemp = New TableRow()
            For i = 0 To 4
                celTemp = New TableCell()
                rowTemp.Cells.Add(celTemp)
                celTemp = Nothing
            Next
            rowTemp.Cells(0).Text = "Payment"
            rowTemp.Cells(1).Text = "Interest"
            rowTemp.Cells(2).Text = "Principal"
            rowTemp.Cells(3).Text = "Total Int."
            rowTemp.Cells(4).Text = "Balance"
            rowTemp.Font.Bold = True
            tblAmortize.Rows.Add(rowTemp)


            'PULL VALUES FROM SESSION VARIABLES

            intTerm = Convert.ToInt32(Session("term"))
            decPrincipal = Convert.ToDecimal(Session("Principal"))
            decIntRate = Convert.ToDecimal(Session("IntRate"))
            dblPayment = Convert.ToDecimal(Session("decPayment"))

            'CALCULATE AMORTIZATION SCHEDULE
            intNumPayments = intTerm * 12
            decTotalPrincipal = decPrincipal
            For intMonth = 1 To intNumPayments
                'Determine Values For the Current Row
                decMonthInterest = (decIntRate / 100) / 12 * decTotalPrincipal
                decTotalInterest = decTotalInterest + decMonthInterest
                decMonthPrincipal = Convert.ToDecimal(dblPayment) - decMonthInterest
                If decMonthPrincipal > decPrincipal Then
                    decMonthPrincipal = decPrincipal
                End If
                decTotalPrincipal = decTotalPrincipal - decMonthPrincipal

                'Add the values to the table
                rowTemp = New TableRow()
                For i = 0 To 4
                    celTemp = New TableCell()
                    rowTemp.Cells.Add(celTemp)
                    celTemp = Nothing
                Next i

                rowTemp.Cells(0).Text = intMonth.ToString
                rowTemp.Cells(1).Text = Format(decMonthInterest, "$###0.00")
                rowTemp.Cells(2).Text = Format(decMonthPrincipal, "$###0.00")
                rowTemp.Cells(3).Text = Format(decTotalInterest, "$###0.00")
                rowTemp.Cells(4).Text = Format(decTotalPrincipal, "$###0.00")

                tblAmortize.Rows.Add(rowTemp)
                rowTemp = Nothing
                'PrevFormat()

            Next intMonth
        End If

    End Sub


    Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim decPrincipal As Decimal
        Dim decIntRate As Decimal
        Dim intTerm As Integer, dblPayment As Double

        'Store the principle in the variable decprincipal
        'decPrincipal = CDbl(txtPrincipal.Text)
        decPrincipal = (txtPrincipal.Text)

        'Convert interest rate to its decimal equivalent
        ' i.e. 12.75 becomes 0.1275
        decIntRate = CDbl(txtIntrate.Text) / 100

        'Convert annual interest rate to monthly
        '  by dividing by 12 (months in a year)
        decIntRate = decIntRate / 12

        'Convert number of years to number of months
        '  by multiplying by 12 (months in a year)
        intTerm = CDbl(txtTerm.Text) * 12

        'Calculate and display the monthly payment.
        '  The format function makes the displayed number look good.
        dblPayment = decPrincipal * (decIntRate / (1 - (1 + decIntRate) ^ -intTerm))
        txtPayment.Text = Format(dblPayment, "$#,##0.00")

        'Add Amortization Schedule
        '  The Schedule button will be shown only after you click the Calculate Payment LinkButton
        btnShowDetail.Visible = True
    End Sub

    Protected Sub btnShowDetail_Click(sender As Object, e As EventArgs) Handles btnShowDetail.Click

        'Storetext box values in session variables
        Session.Add("Principal", txtPrincipal.Text)
        Session.Add("IntRate", txtIntrate.Text)
        Session.Add("Term", txtTerm.Text)
        Session.Add("Payment", txtPayment.Text)

        'Display the Amortization form
        Response.Redirect("frmAmort.aspx")
    End Sub
End Class

While you declare the table you must write runat = "Server"

This is the code sample

<Table ID = "tblMyTable" runat = "Server">
    .
    .
    .
</Table>

Now you can use your table in code.

If you can already access the table in Form2's Code and you want it to be accessed in Form1's Code and if you don't know how to do it then here is the solution :

Dim tbl as new Table
tbl = CType(Form1.FindControl("tblMyTable"),Table)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM