5 Replies - 419 Views - Last Post: 10 November 2014 - 10:58 AM Rate Topic: -----

#1 Steen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-September 14

Loop help...

Posted 05 November 2014 - 11:25 AM

-So I'm trying to create a Windows Form Program that displays the year and the team that won the Superbowl in the corresponding year. It has two labels and one button. Each time you click the button I want label 1 and label 2 to change (they should change to the next year and the team that won it that year). I want label 1 to display the team name and label 2 to display the year.
-This is what I have right now, when I click the button label 1 shows "Seattle Seahawks" and label 2 shows "2014" then when I click the button nothing changes. I want to change this so that the first button click displays the first year and the first team that won instead. Then I also need to know how to have it so when I click it displays the next year and corresponding team. Thanks for any help, new at this!

Public Class Form1
    
Dim teamnames() As String = _
    {"Green Bay Packers", "Green Bay Packers", "New York Jets", "Kansas City Cheifs", "Baltimore Colts", " Dallas Cowboys", _
    "Miami Dolphins", "Miami Dolphins", "Pittsburgh Steelers", "Pittsburgh Steelers", "Oakland Raiders", "Dallas Cowboys", _
    "Pittsburgh Steelers", "Pittsburgh Steelers", "Oakland Raiders", "San Fransisco 49ers", " Washington Redskins", _
    "Los Angeles Raiders", "San Fransico 49ers", "Chicago Bears", "New York Giants", "Washington Redskins", "San Fransico 49ers", _
    "San Fransisco 49ers", "New York Giants", "Washington Redskins", "Dallas Cowboys", "Dallas Cowboys", "San Fransico 49ers", _
    "Dallas Cowboys", "Green Bay Packers", "Denver Broncos", "Denver Broncos", "St. Louis Rams", "Baltimore Ravens", _
    "New England Patriots", "Tampa Bay Buccaneers", "New England Patriots", "New England Patriots", "Pittsburgh Steelers", _
    "Indianapolis Colts", "New York Giants", "Pittsburgh Steelers", "Indianapolis Colts", "New York Giants", "Pittsburgh Steelers", _
    "New Orleans Saints", "Green Bay Packers", "New York Giants", "Baltimore Ravens", "Seattle Seahawks"}
    Dim years() As Integer = _
    {"1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", _
    "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", _
    "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", _
    "2009", "2010", "2011", "2012", "2013", "2014"}



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        For loop1 = 0 To teamnames.Length - 1
            Label1.Text = (teamnames(loop1))
        Next loop1



        For loop2 = 0 To years.Length - 1
            Label2.Text = (years(loop2))
        Next loop2





    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

    End Sub
End Class




This is the layout and what displays when you start the program(When you click "GO!" the label's change as I previously said above. :
Attached Image

As you can see I have two arrays.
The first has the teams listed in order of the year they won.
The second has the corresponding years.

Is This A Good Question/Topic? 0
  • +

Replies To: Loop help...

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 791
  • View blog
  • Posts: 3,460
  • Joined: 03-December 12

Re: Loop help...

Posted 05 November 2014 - 11:57 AM

First, This is redundant,

For loop1 = 0 To teamnames.Length - 1
     Label1.Text = (teamnames(loop1))
Next loop1

For loop2 = 0 To years.Length - 1
    Label2.Text = (years(loop2))
Next loop2


You already have a counter that corresponds to the index. Using parallel arrays will eventually get you in to trouble, but that isn't the issue here.

Next this is done inside of the click event, so you will always get the last element in the array.

1 Idea is to,
Use a function. Pass an index value to the function and have it return the values of that index. On a next button click you pass the current value plus 1.
Was This Post Helpful? 1
  • +
  • -

#3 Steen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-September 14

Re: Loop help...

Posted 05 November 2014 - 01:24 PM

View Postastonecipher, on 05 November 2014 - 11:57 AM, said:

Next this is done inside of the click event, so you will always get the last element in the array.

1 Idea is to,
Use a function. Pass an index value to the function and have it return the values of that index. On a next button click you pass the current value plus 1.


So I created a Function but am not really sure how to "pass" an index value to the function and have it return the values of that index..
Was This Post Helpful? 0
  • +
  • -

#4 Steen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-September 14

Re: Loop help...

Posted 05 November 2014 - 02:16 PM

[quote name='Steen' date='05 November 2014 - 01:24 PM' timestamp='1415219061' post='2072411']

View Postastonecipher, on 05 November 2014 - 11:57 AM, said:

So I created a Function but am not really sure how to "pass" an index value to the function and have it return the values of that index..


Is this what the function would look like?
  Function function1(ByVal x As String) As String
        x = teamnames(0)
        Return x

    End Function



If this is correct then how do I code the button click? Sorry very new at this, if you can't tell. Just alot of Google... :P
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 791
  • View blog
  • Posts: 3,460
  • Joined: 03-December 12

Re: Loop help...

Posted 05 November 2014 - 02:51 PM

Sort of...

I will give an example when I am home.
Was This Post Helpful? 0
  • +
  • -

#6 Steen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-September 14

Re: Loop help...

Posted 10 November 2014 - 10:58 AM

View Postastonecipher, on 05 November 2014 - 02:51 PM, said:

Sort of...

I will give an example when I am home.


Example? lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1