Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi there, I'm doing a project for one of my classes and I'm stuck trying to add two matrices. I managed to make the matrix via keypress, but when I input details for the calculate function (cals_click), I cannot find a way to add two matrices (which in design would be under ListA and ListB boxes). How do I approach this?

VB
Private Sub Input_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Input.KeyPress
    Dim x1, x2, x3 As String
    Dim x23 As String
    Dim pos1, pos2 As Integer

    If Asc(e.KeyChar) = 13 Then
        pos1 = InStr(Input.Text, ";")
        x1 = Mid(Input.Text, 1, pos1 - 1)
        x23 = Mid(Input.Text, pos1 + 2)
        pos2 = InStr(x23, ";")
        x2 = Mid(x23, 1, pos2 - 1)
        x3 = Mid(x23, pos2 + 2)

        If MA.Checked Then
            ListA.Items.Add(x1)
            ListA.Items.Add(x2)
            ListA.Items.Add(x3)

        End If
        If MB.Checked Then
            ListB.Items.Add(x1)
            ListB.Items.Add(x2)
            ListB.Items.Add(x3)
        End If
        If MC.Checked Then
            ListC.Items.Add(x1)
            ListC.Items.Add(x2)
            ListC.Items.Add(x3)
        End If
        Input.Text = ""

    End If
End Sub

Private Sub midinstr(ByVal x123 As String, ByRef x1 As Single, ByRef x2 As Single, ByRef x3 As Single) 'byref = input, byval = output
    Dim x23 As String 'seperator
    Dim pos1, pos2 As Integer 'seperator

    pos1 = InStr(x123, ",")
    x1 = Val(Mid(x123, 1, pos1 - 1))
    x23 = Mid(x123, pos1 + 1)
    pos2 = InStr(x23, ",")
    x2 = Val(Mid(x23, 1, pos2 - 1))
    x3 = Val(Mid(x23, pos2 + 1))



End Sub


Private Sub Cals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cals.Click
    Dim A123, a2123, a3123 As String
    Dim a1, a2, a3 As Single
    Dim a21, a22, a23 As Single
    Dim a31, a32, a33 As Single
    Dim r As Double

    If DA.Checked Then
        A123 = ListA.Items(0)
        midinstr(A123, a1, a2, a3) 'from private sub below'
        a2123 = ListA.Items(1)
        midinstr(a2123, a21, a22, a23)
        a3123 = ListA.Items(2)
        midinstr(a3123, a31, a32, a33)

        r = a1 * (a22 * a33 - a23 * a32) - a2 * (a21 * a33 - a23 * a31) + a3 * (a21 * a32 - a22 * a31)

    End If

    If DB.Checked Then
        A123 = ListB.Items(0)
        midinstr(A123, a1, a2, a3) 'from private sub below'
        a2123 = ListB.Items(1)
        midinstr(a2123, a21, a22, a23)
        a3123 = ListB.Items(2)
        midinstr(a3123, a31, a32, a33)

        r = a1 * (a22 * a33 - a23 * a32) - a2 * (a21 * a33 - a23 * a31) + a3 * (a21 * a32 - a22 * a31)
    End If

    R_Val.Text = Str(r)

    If M1.Checked Then
        ListD.Items.Add()



    End If

End Sub
Posted
Comments
Jim Jos 23-May-12 4:05am    
Could you give us your requirement? Why you are adding the values to the list boxes ? Each string in list box has 3 values which is separated by comma..IF it is only adding 3X3 matrix why you are having the calculation for r? You could multidimensional arrays to solve this problem easily..

Ah... typical vb code... (sigh)

You might want to separate the code instead of the magic pushbutton anti-pattern (http://en.wikipedia.org/wiki/Magic_pushbutton[^]).

Without even going into it to deep... the code in the keypress handler probably has some pretty critical code for the rest of the process. List items are added in the handler and used in Cals_click.

Good luck!
 
Share this answer
 
If I understand a bit the posted code, you are using strings for storing matrix items. Please don't do that: matrix is an ordered set of numbers hence you should use numeric variables (arrays of) in your program. This would help you in many ways.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900