1 Replies - 408 Views - Last Post: 29 November 2012 - 08:56 PM Rate Topic: -----

#1 Flintvic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 12

Trouble passing Listbox info to another form

Posted 29 November 2012 - 08:04 PM

I am writing a program that tracks motors/engines. Within this programs I already used a class and a array in the main form to store all the user's input then as the user clicks the add button it loops the array and displays just the engine ID in a listbox on the same form. Now what I am having trouble with is that I want the user to be able to click on a selectedindex in the listbox and that in itself opens a new form displaying the full information of the motors. I don't know how pass the array and Listbox information in the Main form into the Display form.

I commented in the Display form where I see an error at.
Thanks in advance


Main Form code
:
Public Class frmMain
    Dim MotorInfo(-1) As clsMotor

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        Dim TestString As String ' holds string from txtDescription for validation
        Dim txtIDLength As Integer ' holds length of txtID for validation
        Dim txtStatlength As Integer ' holds length of txtStat for validation

        'Stored in variables
        txtIDLength = txtID.TextLength
        txtStatLength = txtStat.TextLength
        TestString = txtDescription.Text

        'Validation loop
        If txtIDLength <> 5 Then
            MessageBox.Show("Please enter only 5 Numeric Digits" & vbCrLf &
                           vbTab & "Motor ID:")
        ElseIf Not IsNumeric(txtID.Text) Then
            MessageBox.Show("Please enter only 5 Numeric Digits" & vbCrLf &
                           vbTab & "Motor ID:")
        ElseIf IsAlphaBetical(TestString) = False Then
            MessageBox.Show("Please enter only Alphabetical letters" & vbCrLf &
                           vbTab & "Description:")
        ElseIf txtRpm.Text < 10 And txtRpm.Text > 10000 Then
            MessageBox.Show("Please enter digits between 10-10,000" & vbCrLf &
                           vbTab & "Rpm:")
        ElseIf txtVolt.Text < 1 And txtVolt.Text > 500 Then
            MessageBox.Show("Please enter digits between 1-500" & vbCrLf &
                           vbTab & "Volt:")
        ElseIf txtStatlength <> 3 Then
            MessageBox.Show("Please enter only 3 characters" & vbCrLf &
                           vbTab & "Status:")
        Else
            ReDim Preserve MotorInfo(MotorInfo.Length)
            MotorInfo(MotorInfo.Length - 1) = New clsMotor 'establish New instance Address Of class
        End If

        'set values (properties) for Motorinfo
        With MotorInfo(MotorInfo.Length - 1)
            .ID = txtID.Text
            .Description = txtDescription.Text
            .Status = txtStat.Text
            .Rpm = txtRpm.Text
            .Volt = txtVolt.Text
        End With
        Call ListMotors()
    End Sub

    Private Sub ListMotors()
        'lists the motor information that are in MotorInfo array (class)
        lstInfo.Items.Clear() ' clear out list box
        For i As Integer = 0 To MotorInfo.Length - 1
            lstInfo.Items.Add(MotorInfo(i).ID)
        Next
    End Sub
    Public Function IsAlphaBetical(TestString As String) As Boolean

        Dim sTemp As String
        Dim iLen As Integer
        Dim iCtr As Integer
        Dim sChar As String

        'returns true if all characters in a string are alphabetical
        'returns false otherwise or for empty string

        sTemp = TestString
        iLen = Len(sTemp)
        If iLen > 0 Then
            For iCtr = 1 To iLen
                sChar = Mid(sTemp, iCtr, 1)
                If Not sChar Like "[A-Za-z]" Then Exit Function
            Next

            IsAlphaBetical = True
        End If

    End Function

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        'closes the form
        Me.Close()
    End Sub

    Private Sub lstInfo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstInfo.SelectedIndexChanged
        If lstInfo.SelectedIndex > -1 Then
            Me.Hide()
            frmDisplay.ShowDialog()
            Me.Show
        End If
    End Sub
End Class



Display Form
Public Class frmDisplay

    Private Sub Display()

        With MotorInfo(frmMain.lstInfo.SelectedIndex)   < 'I get an MotorInfo isnt declared
            lblMotorID.Text = .ID
            lblDescription.Text = .Description
            lblStat.Text = .Status
            lblRpm.Text = .Rpm
            lblVolt.Text = .Volt
        End With

    End Sub
End Class


Class

Public Class clsMotor
    'clsMotors holds the information of the motors
    Dim strID As String
    Dim strDescrip As String
    Dim strStat As String
    Dim intRpm As Integer
    Dim intVolt As Integer


    Public Property ID As String
        Get
            Return strID
        End Get
        Set(value As String)
            strID = value
        End Set
    End Property

    Public Property Description As String
        Get
            Return strDescrip
        End Get
        Set(value As String)
            strDescrip = value
        End Set
    End Property

    Public Property Status As String
        Get
            Return strStat
        End Get
        Set(value As String)
            strStat = value
        End Set
    End Property

    Public Property Rpm As Integer
        Get
            Return intRpm
        End Get
        Set(value As Integer)
            intRpm = value
        End Set
    End Property

    Public Property Volt As Integer
        Get
            Return intVolt
        End Get
        Set(value As Integer)
            intVolt = value
        End Set
    End Property
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble passing Listbox info to another form

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Trouble passing Listbox info to another form

Posted 29 November 2012 - 08:56 PM

When you create a new object you use the New keyword. You are also dimensioning the array at -1 which does not make sense. Why not use a List(Of clsMotor) instead of an array? Then you use the Add method to add new clsMotor objects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1