 |

06-19-2007, 08:35 AM
|
|
Visual Studio 2005
Does anybody here have experience with Visual Studio 2005? Would you mind helping out a beginner? I'm not necessarily looking for THE answer, but I just think there's bound to be a better way than I've done it here. Any pointers or advice would be appreciated.
I am studying arrays and loops and I have to create a matrix calculator. I have two 3x3 matrices to be added together to create a third results matrix. Nine 2 character input text boxes are provided for input per matrix.
I already have created a program, but I'm looking for alternatives. Specifically, I'm trying figure out if I could have handled the input matrix populations and the calculations by using an array loop. The following is my code:
============
Option Explicit On
Option Strict On
Public Class frmMatrixCalculator
Dim arMatrix1(0 To 2, 0 To 2) As Integer
Dim arMatrix2(0 To 2, 0 To 2) As Integer
-----
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
'Declaring Matrix1 and it's corresponding text input boxes.
arMatrix1(0, 0) = CInt(txtM1_00.Text)
arMatrix1(0, 1) = CInt(txtM1_01.Text)
arMatrix1(0, 2) = CInt(txtM1_02.Text)
arMatrix1(1, 0) = CInt(txtM1_10.Text)
arMatrix1(1, 1) = CInt(txtM1_11.Text)
arMatrix1(1, 2) = CInt(txtM1_12.Text)
arMatrix1(2, 0) = CInt(txtM1_20.Text)
arMatrix1(2, 1) = CInt(txtM1_21.Text)
arMatrix1(2, 2) = CInt(txtM1_22.Text)
'Declaring Matrix2 and it's corresponding text input boxes.
arMatrix2(0, 0) = CInt(txtM2_00.Text)
arMatrix2(0, 1) = CInt(txtM2_01.Text)
arMatrix2(0, 2) = CInt(txtM2_02.Text)
arMatrix2(1, 0) = CInt(txtM2_10.Text)
arMatrix2(1, 1) = CInt(txtM2_11.Text)
arMatrix2(1, 2) = CInt(txtM2_12.Text)
arMatrix2(2, 0) = CInt(txtM2_20.Text)
arMatrix2(2, 1) = CInt(txtM2_21.Text)
arMatrix2(2, 2) = CInt(txtM2_22.Text)
'Setting the Matrix1 variables and values
Dim intM1_00 As Integer = arMatrix1(0, 0)
Dim intM1_01 As Integer = arMatrix1(0, 1)
Dim intM1_02 As Integer = arMatrix1(0, 2)
Dim intM1_10 As Integer = arMatrix1(1, 0)
Dim intM1_11 As Integer = arMatrix1(1, 1)
Dim intM1_12 As Integer = arMatrix1(1, 2)
Dim intM1_20 As Integer = arMatrix1(2, 0)
Dim intM1_21 As Integer = arMatrix1(2, 1)
Dim intM1_22 As Integer = arMatrix1(2, 2)
'Setting the Matrix2 variables and values
Dim intM2_00 As Integer = arMatrix2(0, 0)
Dim intM2_01 As Integer = arMatrix2(0, 1)
Dim intM2_02 As Integer = arMatrix2(0, 2)
Dim intM2_10 As Integer = arMatrix2(1, 0)
Dim intM2_11 As Integer = arMatrix2(1, 1)
Dim intM2_12 As Integer = arMatrix2(1, 2)
Dim intM2_20 As Integer = arMatrix2(2, 0)
Dim intM2_21 As Integer = arMatrix2(2, 1)
Dim intM2_22 As Integer = arMatrix2(2, 2)
'Set the variables for the results matrix and do the math.
Dim intMR_00 As Integer = intM1_00 + intM2_00
Dim intMR_01 As Integer = intM1_01 + intM2_01
Dim intMR_02 As Integer = intM1_02 + intM2_02
Dim intMR_10 As Integer = intM1_10 + intM2_10
Dim intMR_11 As Integer = intM1_11 + intM2_11
Dim intMR_12 As Integer = intM1_12 + intM2_12
Dim intMR_20 As Integer = intM1_20 + intM2_20
Dim intMR_21 As Integer = intM1_21 + intM2_21
Dim intMR_22 As Integer = intM1_22 + intM2_22
'Populate the results matrix
txtMR_00.Text = CStr(intMR_00)
txtMR_01.Text = CStr(intMR_01)
txtMR_02.Text = CStr(intMR_02)
txtMR_10.Text = CStr(intMR_10)
txtMR_11.Text = CStr(intMR_11)
txtMR_12.Text = CStr(intMR_12)
txtMR_20.Text = CStr(intMR_20)
txtMR_21.Text = CStr(intMR_21)
txtMR_22.Text = CStr(intMR_22)
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'End the program operation, clear all variables, close the form.
Close()
End Sub
--------
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Setting variables for the types of objects to clear.
Dim ctrl As Control
Dim txt As TextBox
'Running a For loop to clear all the textboxes and prepare for new entry.
For Each ctrl In Me.Controls
If (ctrl.GetType() Is GetType(TextBox)) Then
txt = CType(ctrl, TextBox)
txt.Text = ""
'I added this line to return focus to the first box of Matrix1.
txtM1_00.Focus()
End If
Next
End Sub
End Class
|

10-31-2007, 11:47 AM
|
 |
Registered Member
|
|
Join Date: Oct 2007
Posts: 169
|
|
Quote:
Originally Posted by BUPC-HCB
Does anybody here have experience with Visual Studio 2005? Would you mind helping out a beginner? I'm not necessarily looking for THE answer, but I just think there's bound to be a better way than I've done it here. Any pointers or advice would be appreciated.
I am studying arrays and loops and I have to create a matrix calculator. I have two 3x3 matrices to be added together to create a third results matrix. Nine 2 character input text boxes are provided for input per matrix.
I already have created a program, but I'm looking for alternatives. Specifically, I'm trying figure out if I could have handled the input matrix populations and the calculations by using an array loop. The following is my code:
============
Option Explicit On
Option Strict On
Public Class frmMatrixCalculator
Dim arMatrix1(0 To 2, 0 To 2) As Integer
Dim arMatrix2(0 To 2, 0 To 2) As Integer
-----
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
'Declaring Matrix1 and it's corresponding text input boxes.
arMatrix1(0, 0) = CInt(txtM1_00.Text)
arMatrix1(0, 1) = CInt(txtM1_01.Text)
arMatrix1(0, 2) = CInt(txtM1_02.Text)
arMatrix1(1, 0) = CInt(txtM1_10.Text)
arMatrix1(1, 1) = CInt(txtM1_11.Text)
arMatrix1(1, 2) = CInt(txtM1_12.Text)
arMatrix1(2, 0) = CInt(txtM1_20.Text)
arMatrix1(2, 1) = CInt(txtM1_21.Text)
arMatrix1(2, 2) = CInt(txtM1_22.Text)
'Declaring Matrix2 and it's corresponding text input boxes.
arMatrix2(0, 0) = CInt(txtM2_00.Text)
arMatrix2(0, 1) = CInt(txtM2_01.Text)
arMatrix2(0, 2) = CInt(txtM2_02.Text)
arMatrix2(1, 0) = CInt(txtM2_10.Text)
arMatrix2(1, 1) = CInt(txtM2_11.Text)
arMatrix2(1, 2) = CInt(txtM2_12.Text)
arMatrix2(2, 0) = CInt(txtM2_20.Text)
arMatrix2(2, 1) = CInt(txtM2_21.Text)
arMatrix2(2, 2) = CInt(txtM2_22.Text)
'Setting the Matrix1 variables and values
Dim intM1_00 As Integer = arMatrix1(0, 0)
Dim intM1_01 As Integer = arMatrix1(0, 1)
Dim intM1_02 As Integer = arMatrix1(0, 2)
Dim intM1_10 As Integer = arMatrix1(1, 0)
Dim intM1_11 As Integer = arMatrix1(1, 1)
Dim intM1_12 As Integer = arMatrix1(1, 2)
Dim intM1_20 As Integer = arMatrix1(2, 0)
Dim intM1_21 As Integer = arMatrix1(2, 1)
Dim intM1_22 As Integer = arMatrix1(2, 2)
'Setting the Matrix2 variables and values
Dim intM2_00 As Integer = arMatrix2(0, 0)
Dim intM2_01 As Integer = arMatrix2(0, 1)
Dim intM2_02 As Integer = arMatrix2(0, 2)
Dim intM2_10 As Integer = arMatrix2(1, 0)
Dim intM2_11 As Integer = arMatrix2(1, 1)
Dim intM2_12 As Integer = arMatrix2(1, 2)
Dim intM2_20 As Integer = arMatrix2(2, 0)
Dim intM2_21 As Integer = arMatrix2(2, 1)
Dim intM2_22 As Integer = arMatrix2(2, 2)
'Set the variables for the results matrix and do the math.
Dim intMR_00 As Integer = intM1_00 + intM2_00
Dim intMR_01 As Integer = intM1_01 + intM2_01
Dim intMR_02 As Integer = intM1_02 + intM2_02
Dim intMR_10 As Integer = intM1_10 + intM2_10
Dim intMR_11 As Integer = intM1_11 + intM2_11
Dim intMR_12 As Integer = intM1_12 + intM2_12
Dim intMR_20 As Integer = intM1_20 + intM2_20
Dim intMR_21 As Integer = intM1_21 + intM2_21
Dim intMR_22 As Integer = intM1_22 + intM2_22
'Populate the results matrix
txtMR_00.Text = CStr(intMR_00)
txtMR_01.Text = CStr(intMR_01)
txtMR_02.Text = CStr(intMR_02)
txtMR_10.Text = CStr(intMR_10)
txtMR_11.Text = CStr(intMR_11)
txtMR_12.Text = CStr(intMR_12)
txtMR_20.Text = CStr(intMR_20)
txtMR_21.Text = CStr(intMR_21)
txtMR_22.Text = CStr(intMR_22)
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'End the program operation, clear all variables, close the form.
Close()
End Sub
--------
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Setting variables for the types of objects to clear.
Dim ctrl As Control
Dim txt As TextBox
'Running a For loop to clear all the textboxes and prepare for new entry.
For Each ctrl In Me.Controls
If (ctrl.GetType() Is GetType(TextBox)) Then
txt = CType(ctrl, TextBox)
txt.Text = ""
'I added this line to return focus to the first box of Matrix1.
txtM1_00.Focus()
End If
Next
End Sub
End Class
|
Set up a method that loops through the matrix rather than setting each value of the matrix. Also set your integers up as an array of ints. Then create a List<TextBox> and add your textboxes to it. Then link the list with the values in the matrix. It should modularize the code and make it simple to update and read.
|

10-31-2007, 12:28 PM
|
 |
Registered Member
|
|
Join Date: Oct 2007
Posts: 169
|
|
This may be irrelevant by now but ...
Here is what is looks like in vs 2005 c++. Unfortunately I am not a VB guy but I am going to try to make it in vb.
const int DimensionSize = 2;
const int ArraySize = 3;
array <int, DimensionSize> ^ Matrix1 =
gcnew array<int,DimensionSize>(ArraySize,ArraySize);
array <int, DimensionSize> ^ Matrix2 =
gcnew array<int,DimensionSize>(ArraySize,ArraySize);
for(int i = 0 ; i < ArraySize; i++)
{
for(int j = 0 ; j < ArraySize ; j ++)
{
TextBox ^ txt =
(TextBox^)Controls->Find(
String::Format( "txtM1_{0}{1}",i,j),true)[0];
Matrix1[i,j] = int::Parse(txt->Text->Trim());
TextBox ^ txt2 =
(TextBox^)Controls->Find(
String::Format( "txtM2_{0}{1}",i,j),true)[0];
Matrix2[i,j] = int::Parse(txt2->Text->Trim());
}
}
|

10-31-2007, 12:47 PM
|
 |
Registered Member
|
|
Join Date: Oct 2007
Posts: 169
|
|
I was able to get it to work in vb. Here is what I have
Public Class Form1
Const DimensionSize As Integer = 2
Const ArraySize As Integer = 3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Matrix1(2, 2) As Integer
Dim Matrix2(2, 2) As Integer
Dim txt1 As TextBox
Dim txt2 As TextBox
For i As Integer = 0 To ArraySize
For j As Integer = 0 To ArraySize
txt1 = CType(Controls.Find(String.Format("txtM1_{0}{1}", i, j), True)(0), TextBox)
Matrix1(i, j) = Integer.Parse(txt1.Text.Trim())
txt2 = CType(Controls.Find(String.Format("txtM2_{0}{1}", i, j), True)(0), TextBox)
Matrix2(i, j) = Integer.Parse(txt2.Text.Trim())
Next
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtM1_10.TextChanged
End Sub
End Class
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
Are You Visual?
|
Digging4Truth |
Fellowship Hall |
14 |
03-22-2007 08:09 AM |
| |
|