Question: I am working on a program using C# and i am trying to figure out how to code 2 parts of this programs. There is

I am working on a program using C# and i am trying to figure out how to code 2 parts of this programs. There is a button Make B Copy of A, and a Radio Button called A == B. I cant seem to figure out how to code that. I will copy and paste my code here.

using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace MatrixOpsProblem { public partial class frmMatrixOps2 : Form { double[,] MatrixA; double[,] MatrixB; public frmMatrixOps2() { InitializeComponent(); }

private void btnExit_Click(object sender, EventArgs e) { this.Close(); }

private void btnClearAll_Click(object sender, EventArgs e) { txtColsA.Text = ""; txtRowA.Text = ""; txtColsB.Text = ""; txtRowB.Text = ""; txtMatrixA.Text = ""; txtMatrixB.Text = ""; txtMatrixC.Text = ""; }

private void btnCalculate_Click(object sender, EventArgs e) { if (rbnAdd.Checked == true) { if (!(txtRowA.Text.Equals(txtRowB.Text))) MessageBox.Show("Dimensions must be equal for rows A and B.", "Entry Error"); if (!(txtColsA.Text.Equals(txtColsB.Text))) MessageBox.Show("Dimensions must be equal for Columns A and B.", "Entry Error"); else { if (txtMatrixA.Text.Equals("")) { btnMakeA.PerformClick(); RadioAdd(); } if (txtMatrixB.Text.Equals("")) { btnMakeB.PerformClick(); RadioAdd(); } if (!(txtMatrixA.Text.Equals("") && txtMatrixB.Text.Equals(""))) { RadioAdd(); } } } else if (rbnSub.Checked == true) { if (!(txtRowA.Text.Equals(txtRowB.Text))) MessageBox.Show("Dimensions must be equal for rows A and B.", "Entry Error"); if (!(txtColsA.Text.Equals(txtColsB.Text))) MessageBox.Show("Dimensions must be equal for Columns A and B.", "Entry Error"); else { if (txtMatrixA.Text.Equals("")) { btnMakeA.PerformClick(); RadioSubtract(); } if (txtMatrixB.Text.Equals("")) { btnMakeB.PerformClick(); RadioSubtract(); } if (!(txtMatrixA.Text.Equals("") && txtMatrixB.Text.Equals(""))) { RadioSubtract(); } } } else if (rbnMult.Checked == true) { if (!(txtColsA.Text.Equals(txtRowB.Text))) MessageBox.Show("Dimensions for Matris A column should be equal to Dimensions of Matrix B row.", "Entry Error"); else { if (txtMatrixA.Text.Equals("")) { btnMakeA.PerformClick(); RadioMultiply(); } if (txtMatrixB.Text.Equals("")) { btnMakeB.PerformClick(); RadioMultiply(); } if (!(txtMatrixA.Text.Equals("") && txtMatrixB.Text.Equals(""))) { RadioMultiply(); } } } } private void btnMakeA_Click(object sender, EventArgs e) { try { txtRowA.Focus(); int RowA = Convert.ToInt32(txtRowA.Text); if (RowA < 1 || RowA > 10) { MessageBox.Show("Row A must be atleast 1 and less than 10"); txtRowA.Focus(); } int ColA = Convert.ToInt32(txtColsA.Text); if (ColA < 1 || ColA > 10) { MessageBox.Show("Column A must be atleast 1 and less than 10"); txtColsA.Focus(); } MatrixA = CreateMatrix(RowA, ColA); txtMatrixA.Text = ConvertMatrixString(RowA, ColA, MatrixA); } catch (Exception) { if (txtRowA.Text == "") { MessageBox.Show("Row A is required.", "Entry Error"); txtRowA.Focus(); } if (txtColsA.Text == "") { MessageBox.Show("Column A is required.", "Entry Error"); txtColsA.Focus(); } } }

private void btnMakeB_Click(object sender, EventArgs e) { try { int RowB = Convert.ToInt32(txtRowB.Text); if (RowB < 1 || RowB > 10) { MessageBox.Show("Row B must be atleast 1 and less than 10"); txtRowA.Focus(); } int ColB = Convert.ToInt32(txtColsB.Text); if (ColB < 1 || ColB > 10) { MessageBox.Show("Column B must be atleast 1 and less than 10"); txtColsA.Focus(); } MatrixB = CreateMatrix(RowB, ColB); txtMatrixB.Text = ConvertMatrixString(RowB, ColB, MatrixB); } catch (Exception) { if (txtRowB.Text == "") { MessageBox.Show("Row B is required.", "Entry Error"); txtRowA.Focus(); } if (txtColsB.Text == "") { MessageBox.Show("Column B is required.", "Entry Error"); txtColsA.Focus(); } } } private double[,] CreateMatrix(int row, int col) {

double[,] matrix = new double[row, col];

Random autorand = new Random();

for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrix[i, j] = Math.Round(autorand.NextDouble() * 10.0, 1); } } return matrix; }

private double[,] CreateIMatrix(int row, int col) { double[,] matrix = new double[row, col]; Random autorand = new Random(); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i == j) matrix[i, j] = 1; else matrix[i, j] = 0; } } return matrix; } private string ConvertMatrixString(int row, int col, double[,] matrix) {

StringBuilder stringBulider = new StringBuilder();

for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { stringBulider.Append(matrix[i, j] + " "); } stringBulider.Append(Environment.NewLine); } return stringBulider.ToString(); }

private void RadioAdd() { double finalMatrixA = MatrixA[0, 1]; double finalMatrixB = MatrixB[0, 1];

int row = Convert.ToInt32(txtRowA.Text); int col = Convert.ToInt32(txtColsA.Text);

double[,] matrixC = new double[row, col];

for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrixC[i, j] = MatrixA[i, j] + MatrixB[i, j]; } } txtMatrixC.Text = ConvertMatrixString(row, col, matrixC); }

private void RadioSubtract() { double finalMatrixA = MatrixA[0, 1]; double finalMatrixB = MatrixB[0, 1];

int row = Convert.ToInt32(txtRowA.Text); int col = Convert.ToInt32(txtColsA.Text);

double[,] matrixC = new double[row, col];

for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrixC[i, j] = MatrixA[i, j] - MatrixB[i, j]; } } txtMatrixC.Text = ConvertMatrixString(row, col, matrixC); }

private void RadioMultiply() { double finalMatrixA = MatrixA[0, 1]; double finalMatrixB = MatrixB[0, 1];

int row = Convert.ToInt32(txtRowA.Text); int col = Convert.ToInt32(txtColsA.Text);

double[,] matrixC = new double[row, col];

for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrixC[i, j] = MatrixA[i, j] * MatrixB[i, j]; } } txtMatrixC.Text = ConvertMatrixString(row, col, matrixC); }

private void btnMakeBId_Click(object sender, EventArgs e) { try { int RowB = Convert.ToInt32(txtRowB.Text); if (RowB < 1 || RowB > 10) { MessageBox.Show("Row B must be atleast 1 and less than 10"); txtRowA.Focus(); } int ColB = Convert.ToInt32(txtColsB.Text); if (ColB < 1 || ColB > 10) { MessageBox.Show("Column B must be atleast 1 and less than 10"); txtColsA.Focus(); } MatrixB = CreateIMatrix(RowB, ColB); txtMatrixB.Text = ConvertMatrixString(RowB, ColB, MatrixB); } catch (Exception) { if (txtRowB.Text == "") { MessageBox.Show("Row B is a required field.", "Entry Error"); txtRowA.Focus(); } if (txtColsB.Text == "") { MessageBox.Show("Column B is a required field.", "Entry Error"); txtColsA.Focus(); } } }

private void frmMatrixOpsProblem_Load(object sender, EventArgs e) { txtColsA.Text = "3"; txtColsB.Text = "3"; txtRowA.Text = "3"; txtRowB.Text = "3"; }

private void btnMakeBCopyA_Click(object sender, EventArgs e) {

} } }

----------------------------------------------------------

private void InitializeComponent() { this.gbxSelectOp = new System.Windows.Forms.GroupBox(); this.rbnAequalB = new System.Windows.Forms.RadioButton(); this.rbnSub = new System.Windows.Forms.RadioButton(); this.rbnAdd = new System.Windows.Forms.RadioButton(); this.rbnMult = new System.Windows.Forms.RadioButton(); this.txtColsB = new System.Windows.Forms.TextBox(); this.txtColsA = new System.Windows.Forms.TextBox(); this.txtRowB = new System.Windows.Forms.TextBox(); this.txtRowA = new System.Windows.Forms.TextBox(); this.lblCols = new System.Windows.Forms.Label(); this.lblRows = new System.Windows.Forms.Label(); this.btnExit = new System.Windows.Forms.Button(); this.btnClearAll = new System.Windows.Forms.Button(); this.btnCalculate = new System.Windows.Forms.Button(); this.btnMakeBId = new System.Windows.Forms.Button(); this.btnMakeB = new System.Windows.Forms.Button(); this.btnMakeA = new System.Windows.Forms.Button(); this.lblMatrixC = new System.Windows.Forms.Label(); this.lblMatrixB = new System.Windows.Forms.Label(); this.lblMatrixA = new System.Windows.Forms.Label(); this.txtMatrixC = new System.Windows.Forms.TextBox(); this.txtMatrixB = new System.Windows.Forms.TextBox(); this.txtMatrixA = new System.Windows.Forms.TextBox(); this.btnMakeBCopyA = new System.Windows.Forms.Button(); this.gbxSelectOp.SuspendLayout(); this.SuspendLayout(); // // gbxSelectOp // this.gbxSelectOp.Controls.Add(this.rbnAequalB); this.gbxSelectOp.Controls.Add(this.rbnSub); this.gbxSelectOp.Controls.Add(this.rbnAdd); this.gbxSelectOp.Controls.Add(this.rbnMult); this.gbxSelectOp.Location = new System.Drawing.Point(14, 283); this.gbxSelectOp.Name = "gbxSelectOp"; this.gbxSelectOp.Size = new System.Drawing.Size(280, 50); this.gbxSelectOp.TabIndex = 39; this.gbxSelectOp.TabStop = false; this.gbxSelectOp.Text = "Select Operation"; // // rbnAequalB // this.rbnAequalB.AutoSize = true; this.rbnAequalB.Location = new System.Drawing.Point(217, 27); this.rbnAequalB.Name = "rbnAequalB"; this.rbnAequalB.Size = new System.Drawing.Size(57, 17); this.rbnAequalB.TabIndex = 3; this.rbnAequalB.TabStop = true; this.rbnAequalB.Text = "A == B"; this.rbnAequalB.UseVisualStyleBackColor = true; // // rbnSub // this.rbnSub.AutoSize = true; this.rbnSub.Location = new System.Drawing.Point(140, 27); this.rbnSub.Name = "rbnSub"; this.rbnSub.Size = new System.Drawing.Size(65, 17); this.rbnSub.TabIndex = 2; this.rbnSub.TabStop = true; this.rbnSub.Text = "Subtract"; this.rbnSub.UseVisualStyleBackColor = true; // // rbnAdd // this.rbnAdd.AutoSize = true; this.rbnAdd.Location = new System.Drawing.Point(81, 27); this.rbnAdd.Name = "rbnAdd"; this.rbnAdd.Size = new System.Drawing.Size(44, 17); this.rbnAdd.TabIndex = 1; this.rbnAdd.TabStop = true; this.rbnAdd.Text = "Add"; this.rbnAdd.UseVisualStyleBackColor = true; // // rbnMult // this.rbnMult.AutoSize = true; this.rbnMult.Location = new System.Drawing.Point(15, 27); this.rbnMult.Name = "rbnMult"; this.rbnMult.Size = new System.Drawing.Size(60, 17); this.rbnMult.TabIndex = 0; this.rbnMult.TabStop = true; this.rbnMult.Text = "Multiply"; this.rbnMult.UseVisualStyleBackColor = true; // // txtColsB // this.txtColsB.Location = new System.Drawing.Point(237, 246); this.txtColsB.Name = "txtColsB"; this.txtColsB.Size = new System.Drawing.Size(100, 20); this.txtColsB.TabIndex = 38; // // txtColsA // this.txtColsA.Location = new System.Drawing.Point(237, 216); this.txtColsA.Name = "txtColsA"; this.txtColsA.Size = new System.Drawing.Size(100, 20); this.txtColsA.TabIndex = 37; // // txtRowB // this.txtRowB.Location = new System.Drawing.Point(131, 246); this.txtRowB.Name = "txtRowB"; this.txtRowB.Size = new System.Drawing.Size(100, 20); this.txtRowB.TabIndex = 36; // // txtRowA // this.txtRowA.Location = new System.Drawing.Point(131, 216); this.txtRowA.Name = "txtRowA"; this.txtRowA.Size = new System.Drawing.Size(100, 20); this.txtRowA.TabIndex = 35; // // lblCols // this.lblCols.AutoSize = true; this.lblCols.Location = new System.Drawing.Point(247, 200); this.lblCols.Name = "lblCols"; this.lblCols.Size = new System.Drawing.Size(27, 13); this.lblCols.TabIndex = 34; this.lblCols.Text = "Cols"; // // lblRows // this.lblRows.AutoSize = true; this.lblRows.Location = new System.Drawing.Point(131, 200); this.lblRows.Name = "lblRows"; this.lblRows.Size = new System.Drawing.Size(34, 13); this.lblRows.TabIndex = 33; this.lblRows.Text = "Rows"; // // btnExit // this.btnExit.Location = new System.Drawing.Point(384, 351); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(75, 23); this.btnExit.TabIndex = 32; this.btnExit.Text = "&Exit"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // btnClearAll // this.btnClearAll.Location = new System.Drawing.Point(265, 351); this.btnClearAll.Name = "btnClearAll"; this.btnClearAll.Size = new System.Drawing.Size(113, 23); this.btnClearAll.TabIndex = 31; this.btnClearAll.Text = "Clear &Matrices"; this.btnClearAll.UseVisualStyleBackColor = true; this.btnClearAll.Click += new System.EventHandler(this.btnClearAll_Click); // // btnCalculate // this.btnCalculate.Location = new System.Drawing.Point(14, 351); this.btnCalculate.Name = "btnCalculate"; this.btnCalculate.Size = new System.Drawing.Size(75, 23); this.btnCalculate.TabIndex = 30; this.btnCalculate.Text = "&Calculate"; this.btnCalculate.UseVisualStyleBackColor = true; this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click); // // btnMakeBId // this.btnMakeBId.Location = new System.Drawing.Point(343, 244); this.btnMakeBId.Name = "btnMakeBId"; this.btnMakeBId.Size = new System.Drawing.Size(128, 23); this.btnMakeBId.TabIndex = 29; this.btnMakeBId.Text = "Make B identity"; this.btnMakeBId.UseVisualStyleBackColor = true; this.btnMakeBId.Click += new System.EventHandler(this.btnMakeBId_Click); // // btnMakeB // this.btnMakeB.Location = new System.Drawing.Point(14, 244); this.btnMakeB.Name = "btnMakeB"; this.btnMakeB.Size = new System.Drawing.Size(111, 23); this.btnMakeB.TabIndex = 27; this.btnMakeB.Text = "Make Matrix B"; this.btnMakeB.UseVisualStyleBackColor = true; this.btnMakeB.Click += new System.EventHandler(this.btnMakeB_Click); // // btnMakeA // this.btnMakeA.Location = new System.Drawing.Point(14, 214); this.btnMakeA.Name = "btnMakeA"; this.btnMakeA.Size = new System.Drawing.Size(111, 23); this.btnMakeA.TabIndex = 26; this.btnMakeA.Text = "Make Matrix A"; this.btnMakeA.UseVisualStyleBackColor = true; this.btnMakeA.Click += new System.EventHandler(this.btnMakeA_Click); // // lblMatrixC // this.lblMatrixC.AutoSize = true; this.lblMatrixC.Location = new System.Drawing.Point(333, 9); this.lblMatrixC.Name = "lblMatrixC"; this.lblMatrixC.Size = new System.Drawing.Size(45, 13); this.lblMatrixC.TabIndex = 22; this.lblMatrixC.Text = "Matrix C"; // // lblMatrixB // this.lblMatrixB.AutoSize = true; this.lblMatrixB.Location = new System.Drawing.Point(174, 9); this.lblMatrixB.Name = "lblMatrixB"; this.lblMatrixB.Size = new System.Drawing.Size(45, 13); this.lblMatrixB.TabIndex = 21; this.lblMatrixB.Text = "Matrix B"; // // lblMatrixA // this.lblMatrixA.AutoSize = true; this.lblMatrixA.Location = new System.Drawing.Point(26, 9); this.lblMatrixA.Name = "lblMatrixA"; this.lblMatrixA.Size = new System.Drawing.Size(45, 13); this.lblMatrixA.TabIndex = 20; this.lblMatrixA.Text = "Matrix A"; // // txtMatrixC // this.txtMatrixC.Location = new System.Drawing.Point(321, 34); this.txtMatrixC.Multiline = true; this.txtMatrixC.Name = "txtMatrixC"; this.txtMatrixC.ReadOnly = true; this.txtMatrixC.Size = new System.Drawing.Size(150, 150); this.txtMatrixC.TabIndex = 25; // // txtMatrixB // this.txtMatrixB.Location = new System.Drawing.Point(168, 34); this.txtMatrixB.Multiline = true; this.txtMatrixB.Name = "txtMatrixB"; this.txtMatrixB.ReadOnly = true; this.txtMatrixB.Size = new System.Drawing.Size(150, 150); this.txtMatrixB.TabIndex = 24; // // txtMatrixA // this.txtMatrixA.Location = new System.Drawing.Point(14, 34); this.txtMatrixA.Multiline = true; this.txtMatrixA.Name = "txtMatrixA"; this.txtMatrixA.ReadOnly = true; this.txtMatrixA.Size = new System.Drawing.Size(150, 150); this.txtMatrixA.TabIndex = 23; // // btnMakeBCopyA // this.btnMakeBCopyA.Location = new System.Drawing.Point(343, 214); this.btnMakeBCopyA.Name = "btnMakeBCopyA"; this.btnMakeBCopyA.Size = new System.Drawing.Size(128, 23); this.btnMakeBCopyA.TabIndex = 40; this.btnMakeBCopyA.Text = "Make B Copy of A"; this.btnMakeBCopyA.UseVisualStyleBackColor = true; this.btnMakeBCopyA.Click += new System.EventHandler(this.btnMakeBCopyA_Click); // // frmMatrixOps2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(489, 391); this.Controls.Add(this.btnMakeBCopyA); this.Controls.Add(this.gbxSelectOp); this.Controls.Add(this.txtColsB); this.Controls.Add(this.txtColsA); this.Controls.Add(this.txtRowB); this.Controls.Add(this.txtRowA); this.Controls.Add(this.lblCols); this.Controls.Add(this.lblRows); this.Controls.Add(this.btnExit); this.Controls.Add(this.btnClearAll); this.Controls.Add(this.btnCalculate); this.Controls.Add(this.btnMakeBId); this.Controls.Add(this.btnMakeB); this.Controls.Add(this.btnMakeA); this.Controls.Add(this.txtMatrixC); this.Controls.Add(this.txtMatrixB); this.Controls.Add(this.txtMatrixA); this.Controls.Add(this.lblMatrixC); this.Controls.Add(this.lblMatrixB); this.Controls.Add(this.lblMatrixA); this.Name = "frmMatrixOps2"; this.Text = "Matrix Ops"; this.Load += new System.EventHandler(this.frmMatrixOpsProblem_Load); this.gbxSelectOp.ResumeLayout(false); this.gbxSelectOp.PerformLayout(); this.ResumeLayout(false); this.PerformLayout();

}

#endregion

private System.Windows.Forms.GroupBox gbxSelectOp; private System.Windows.Forms.RadioButton rbnSub; private System.Windows.Forms.RadioButton rbnAdd; private System.Windows.Forms.RadioButton rbnMult; private System.Windows.Forms.TextBox txtColsB; private System.Windows.Forms.TextBox txtColsA; private System.Windows.Forms.TextBox txtRowB; private System.Windows.Forms.TextBox txtRowA; private System.Windows.Forms.Label lblCols; private System.Windows.Forms.Label lblRows; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.Button btnClearAll; private System.Windows.Forms.Button btnCalculate; private System.Windows.Forms.Button btnMakeBId; private System.Windows.Forms.Button btnMakeB; private System.Windows.Forms.Button btnMakeA; private System.Windows.Forms.Label lblMatrixC; private System.Windows.Forms.Label lblMatrixB; private System.Windows.Forms.Label lblMatrixA; private System.Windows.Forms.TextBox txtMatrixC; private System.Windows.Forms.TextBox txtMatrixB; private System.Windows.Forms.TextBox txtMatrixA; private System.Windows.Forms.RadioButton rbnAequalB; private System.Windows.Forms.Button btnMakeBCopyA; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!