5 Replies - 661 Views - Last Post: 26 February 2015 - 03:22 PM Rate Topic: -----

#1 Siha89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-February 15

Is there a way to access an array made on another form?

Posted 26 February 2015 - 06:05 AM

I am trying to make a weighted grade calculator for an entire class. This first form stores an array of Students' names from an input box.

I would like to access it in the main form where the average and overall grade is calculated. So that I can display the student names and a list and select a student and be able to view the grade values.
I also have two other separate forms for each type of weighted grade assignment. The values are stored to the arrays from a textbox in these two forms rather than an input box.

I am trying to either view the array in the main form or copy its contents to a combo box in the main form but I havent figured out a way to do either.

I have tried to access the StudentNames' array from the first form in the main form by:
   Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        
        frmFirst.arrStudents.CopyTo(cbStudentChanged)

    End Sub


And I also tried because on the first form the array items are stored in a list box:
 Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
               frmFirst.lstStudents.Items.CopyTo(cbChangeStudent)

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Is there a way to access an array made on another form?

#2 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 87
  • View blog
  • Posts: 561
  • Joined: 19-May 14

Re: Is there a way to access an array made on another form?

Posted 26 February 2015 - 06:06 AM

make the array a public array maybe that would do the job.

or make another function on that form wich returns te data and can write data to the array.

This post has been edited by RamonRobben: 26 February 2015 - 06:07 AM

Was This Post Helpful? 1
  • +
  • -

#3 Siha89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-February 15

Re: Is there a way to access an array made on another form?

Posted 26 February 2015 - 06:21 AM

Thank you. Oh wow I cant believe I missed that! Making it public enabled me to add the items to the combo box on the main form. But now when I enter this code and try to go the next form I get an index is outside of an array error message. I entered 3 names and stored them to the array from the listbox.
  Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        lblCurrentStudent.Text = frmFirst.lstStudents.SelectedItem.ToString
        cbChangeStudent.Items.Add(frmFirst.arrStudentsName(0))
        cbChangeStudent.Items.Add(frmFirst.arrStudentsName(1))
        cbChangeStudent.Items.Add(frmFirst.arrStudentsName(2))

    End Sub



First Form Code:
Option Explicit On
Option Strict Off
Option Infer Off
Public Class frmFirst
    Public arrStudentsName()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim strNewStudent As String
        Dim i As Integer
        i = 0
        strNewStudent = InputBox("Enter the students name:")
        lstStudents.Items.Add(strNewStudent)
        lstStudents.SelectedIndex = 0
        ReDim Preserve arrStudentsName(i)
        arrStudentsName(i) = strNewStudent
        i += 1
    End Sub

    Private Sub btnGrades_Click(sender As Object, e As EventArgs) Handles btnGrades.Click
        frmMain.Show()
        Me.Hide()

    End Sub

    Private Sub btnAddStudent_Click(sender As Object, e As EventArgs) Handles btnAddStudent.Click
        Dim arrStudentsName()
        Dim strNewStudent As String
        Dim i As Integer
        i = 0
        strNewStudent = InputBox("Enter the students name:")
        lstStudents.Items.Add(strNewStudent)
        ReDim Preserve arrStudentsName(i)
        arrStudentsName(i) = strNewStudent
        i += 1
    End Sub


Was This Post Helpful? 0
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1492
  • View blog
  • Posts: 3,786
  • Joined: 01-February 13

Re: Is there a way to access an array made on another form?

Posted 26 February 2015 - 07:30 AM

Hi,

There are a few things you will need to change. The first thing i recommend doing is keep Option Strict turned On.
Option Explicit On
Option Strict On 'keep option strict on
Option Infer Off



That will help you learn to code a little better in my opinion. It will show you that you need to use the As clause to declare your Public Array like this. It is an Array of Strings.
Public arrStudentsName() As String = {}


Then, if you are going to add a student from more than one place in your code like how you are adding one in the Form Load event and from the btnAddStudent click event then you would save a bit of code by placing the Add Student code in a small Sub and just calling the Sub from both places.

So you could add a Sub, lets call it AddStudent kind of like this to your frmFirst code.
    Private Sub AddStudent()
        Dim i As Integer = arrStudentsName.Length 'you want this to start at the length that the array already is, not from 0
        Dim strNewStudent As String
        strNewStudent = InputBox("Enter the students name:")
        If strNewStudent.Trim <> "" Then
            lstStudents.Items.Add(strNewStudent)
            lstStudents.SelectedIndex = 0
            ReDim Preserve arrStudentsName(i)
            arrStudentsName(i) = strNewStudent
        End If
    End Sub



Now in the Form Load event and from the btnAddStudent click event you can just call the AddStudent Sub. That saves you from repeating the same code in more than one spot.
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        AddStudent()
    End Sub

    Private Sub btnAddStudent_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddStudent.Click
        AddStudent()
    End Sub



Now, in the frmMain Form`s Load event you don`t know how many elements are in the array so you can`t hard code it to add 3 items to the ComboBox because, there may be less than 3 or maybe more than 3 names.

So, you can add the array to the combobox items using a loop to loop through each item in the array and add each one OR you can just use the AddRange method of the ComboBox.Items to add the whole array to the listbox in one line of code.

You would also need to Clear the items before adding them or you will add all the names every time the form is loaded. That means there would be several duplicates added. You only want the current Names added.
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        lblCurrentStudent.Text = frmFirst.lstStudents.SelectedItem.ToString
        cbChangeStudent.Items.Clear() 'clear the names from the ComboBox
        cbChangeStudent.Items.AddRange(frmFirst.arrStudentsName) 'Add all the Current Names
    End Sub



That should get you going in a better direction. There are better and more preferred ways of doing things like this but, this looks like homework and i am not sure if you are ready to learn all the new code for doing it the preferred ways.

This post has been edited by IronRazer: 26 February 2015 - 07:36 AM

Was This Post Helpful? 1
  • +
  • -

#5 Siha89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-February 15

Re: Is there a way to access an array made on another form?

Posted 26 February 2015 - 02:23 PM

Thanks a lot! That really helped now I think I can do the rest. :)
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1492
  • View blog
  • Posts: 3,786
  • Joined: 01-February 13

Re: Is there a way to access an array made on another form?

Posted 26 February 2015 - 03:22 PM

You`re Welcome. Glad it helped. 8)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1