Print array list to label

So this is so simple its embarassing. I cannot figure out what the problem is that is causing this. I have a simple calculator that prints the results in a textbox. I then have a button that is supposed to write the result to an arraylist. Then another button that prints the results to a label control. I'm not sure if the arraylist is being populated or not. I try to manually add an item:
resultslist.add("8") but this does not even add to the array as far as I can tell. Please see the code snippet below. Thank You.
<html>
<head runat="server">
    <title></title>
<script runat = "server" language = "VB">
    
    Dim resultslist As New ArrayList
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 + number2
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 - number2
    End Sub


    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 * number2
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
      
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        If TextBox2.Text = "0" Then
            MsgBox("Cannot Divide By Zero")

        Else
            Label1.Text = number1 / number2
        End If
    End Sub

    
    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Label1.Text = "Result"
    End Sub

    Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Label1.Text = "Result"
    End Sub

    
    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist.Add(Label1.Text.ToString)
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each item In resultslist
            Label2.Text = item
        Next
    End Sub
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>ENTD464 Calculator</h1>
    </div>
    <asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="+" Width="16px" />
    <asp:Button ID="Button2" runat="server" Text="-" />
    <asp:Button ID="Button3" runat="server" Text="*" />
    <asp:Button ID="Button4" runat="server" Text="/" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server" Width="70px"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Enabled="False" Text="Result"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="Store" onclick="Button5_Click" 
        Width="101px" />
    <br />
    <br />
    <asp:Button ID="Button6" runat="server" onclick="Button6_Click" 
        Text="Show Array" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </form>
</body>
</html>

Open in new window

LVL 2
psychokraftAsked:
Who is Participating?
 
sachinpatil10dCommented:
Try this
<html>
<head id="Head1" runat="server">
    <title></title>
<script runat = "server" language = "VB">
    
    Dim resultslist As New ArrayList
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            If Not ViewState.Item("resultslist") Is Nothing Then
                resultslist = ViewState.Item("resultslist")
            End If
        End If
    End Sub
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 + number2
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 - number2
    End Sub


    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 * number2
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
      
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        If TextBox2.Text = "0" Then
            MsgBox("Cannot Divide By Zero")

        Else
            Label1.Text = number1 / number2
        End If
    End Sub

    
    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Label1.Text = "Result"
    End Sub

    Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Label1.Text = "Result"
    End Sub

    
    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist.Add(Label1.Text.ToString)
        ViewState.Item("resultslist") = resultslist
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each item In resultslist
            Label2.Text = Label2.Text + item
        Next
    End Sub
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>ENTD464 Calculator</h1>
    </div>
    <asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="+" Width="16px" />
    <asp:Button ID="Button2" runat="server" Text="-" />
    <asp:Button ID="Button3" runat="server" Text="*" />
    <asp:Button ID="Button4" runat="server" Text="/" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server" Width="70px"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Enabled="False" Text="Result"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="Store" onclick="Button5_Click" 
        Width="101px" />
    <br />
    <br />
    <asp:Button ID="Button6" runat="server" onclick="Button6_Click" 
        Text="Show Array" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </form>
</body>
</html>

Open in new window

0
 
Kiran SonawaneProject LeadCommented:
When you click on button, page is postback and you lost the arraylist data. You can store the listdata either in session or viewstate
0
 
CodeCruiserCommented:
Change

   Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist.Add(Label1.Text.ToString)
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each item In resultslist
            Label2.Text = item
        Next
    End Sub

to


   Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist = Session("results")
        resultslist.Add(Label1.Text.ToString)
        Session("results") = resultslist
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist = Session("results")
        For Each item In resultslist
            Label2.Text = item
        Next
    End Sub



Make sure the resultslist is added to the session in page load

Page_Load
If Not Page.IsPostBack Then
   Session("results") = resultslist
End If
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.