Question

I am trying to make a program in C# and was wondering how it could be...

I am trying to make a program in C# and was wondering how it could be done based on the given instructions.

Here is the code that i have so far...

namespace Conversions
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblCalculatedLength = new System.Windows.Forms.Label();
this.txtLength = new System.Windows.Forms.TextBox();
this.lblToLength = new System.Windows.Forms.Label();
this.lblFromLength = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.Button();
this.btnCalculate = new System.Windows.Forms.Button();
this.cboConversions = new System.Windows.Forms.ComboBox();
this.Label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblCalculatedLength
//
this.lblCalculatedLength.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblCalculatedLength.Location = new System.Drawing.Point(94, 76);
this.lblCalculatedLength.Name = "lblCalculatedLength";
this.lblCalculatedLength.Size = new System.Drawing.Size(96, 20);
this.lblCalculatedLength.TabIndex = 30;
this.lblCalculatedLength.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtLength
//
this.txtLength.Location = new System.Drawing.Point(94, 44);
this.txtLength.Name = "txtLength";
this.txtLength.Size = new System.Drawing.Size(96, 20);
this.txtLength.TabIndex = 25;
//
// lblToLength
//
this.lblToLength.Location = new System.Drawing.Point(14, 76);
this.lblToLength.Name = "lblToLength";
this.lblToLength.Size = new System.Drawing.Size(72, 23);
this.lblToLength.TabIndex = 29;
this.lblToLength.Text = "Kilometers:";
this.lblToLength.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// lblFromLength
//
this.lblFromLength.Location = new System.Drawing.Point(14, 44);
this.lblFromLength.Name = "lblFromLength";
this.lblFromLength.Size = new System.Drawing.Size(72, 23);
this.lblFromLength.TabIndex = 28;
this.lblFromLength.Text = "Miles:";
this.lblFromLength.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// btnExit
//
this.btnExit.CausesValidation = false;
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Location = new System.Drawing.Point(158, 116);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(80, 23);
this.btnExit.TabIndex = 27;
this.btnExit.Text = "E&xit";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(30, 116);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.Size = new System.Drawing.Size(80, 23);
this.btnCalculate.TabIndex = 26;
this.btnCalculate.Text = "&Calculate";
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
//
// cboConversions
//
this.cboConversions.DropDownWidth = 160;
this.cboConversions.Location = new System.Drawing.Point(94, 12);
this.cboConversions.Name = "cboConversions";
this.cboConversions.Size = new System.Drawing.Size(144, 21);
this.cboConversions.TabIndex = 24;
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(14, 12);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(72, 23);
this.Label1.TabIndex = 23;
this.Label1.Text = "Conversion:";
this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AcceptButton = this.btnCalculate;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(261, 155);
this.Controls.Add(this.lblCalculatedLength);
this.Controls.Add(this.txtLength);
this.Controls.Add(this.lblToLength);
this.Controls.Add(this.lblFromLength);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.cboConversions);
this.Controls.Add(this.Label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Conversions";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

internal System.Windows.Forms.Label lblCalculatedLength;
internal System.Windows.Forms.TextBox txtLength;
internal System.Windows.Forms.Label lblToLength;
internal System.Windows.Forms.Label lblFromLength;
internal System.Windows.Forms.Button btnExit;
internal System.Windows.Forms.Button btnCalculate;
internal System.Windows.Forms.ComboBox cboConversions;
internal System.Windows.Forms.Label Label1;
}
}

///////////////////////////

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

namespace Conversions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

string[,] conversionTable = {
           {"Miles to kilometers", "Miles", "Kilometers", "1.6093"},
           {"Kilometers to miles", "Kilometers", "Miles", "0.6214"},
           {"Feet to meters", "Feet", "Meters", "0.3048"},
           {"Meters to feet", "Meters", "Feet", "3.2808"},
           {"Inches to centimeters", "Inches", "Centimeters", "2.54"},
           {"Centimeters to inches", "Centimeters", "Inches", "0.3937"}
       };

public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}

public bool IsDecimal(TextBox textBox, string name)
{
try
{
Convert.ToDecimal(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " must be a decimal number.", "Entry Error");
textBox.Focus();
return false;
}
}

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

private void btnCalculate_Click(object sender, EventArgs e)
{

}
}
}

directions:

2. Set the DropDownStyle property of the combo box so the user must select an item from the list.

3. Add code to load the combo box with the first element in each row of the rectangular array, and display the first item in the combo box when the form is loaded.

4. Add code to change the labels for the text boxes, clear the calculated length, and move the focus to the entry text box when the user selects a different item from the combo box.

5. Test the application to be sure the conversions are displayed in the combo box, the first conversion is selected by default, and the labels change appropriately when a different conversion is selected.

6. Add code to calculate and display the converted length when the user clicks the Calculate button. To calculate the length, you can get the index for the selected conversion and then use that index to get the multiplier from the array. Test the application to be sure this works correctly.

7. Add code to check that the user enters a valid decimal value for the length. Then, test the application one more time to be sure the validation works correctly.

Homework Answers

Answer #1

CORRECTED FORM LOGIC CODE.

//*******************************************************************************************//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
lblFromLength.Text = "Miles";
lblToLength.Text = "Kilometers";
}
string[,] conversionTable = {
{"Miles to kilometers", "Miles", "Kilometers", "1.6093"},
{"Kilometers to miles", "Kilometers", "Miles", "0.6214"},
{"Feet to meters", "Feet", "Meters", "0.3048"},
{"Meters to feet", "Meters", "Feet", "3.2808"},
{"Inches to centimeters", "Inches", "Centimeters", "2.54"},
{"Centimeters to inches", "Centimeters", "Inches", "0.3937"}
};
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
public bool IsDecimal(TextBox textBox, string name)
{
try
{
Convert.ToDecimal(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " must be a decimal number.", "Entry Error");
textBox.Focus();
return false;
}
}

private void btnCalculate_Click_Click(object sender, EventArgs e)
{

if (txtLength.Text.Equals(""))
MessageBox.Show("Please provide input.", "Input Error");

else
{
double n1 = 0;
try
{
n1 = Convert.ToDouble(txtLength.Text);

}
catch (Exception)
{
MessageBox.Show("Please provide a decimal/integer as input.", "Input Error");
}
int index = cboConversions.SelectedIndex;
if (index == 0)
lblCalculatedLength.Text = (n1 * double.Parse(conversionTable[0, 3])).ToString();
else if (index == 1)
lblCalculatedLength.Text = (n1 * double.Parse(conversionTable[1, 3])).ToString();

else if (index == 2)
lblCalculatedLength.Text = (n1 * double.Parse(conversionTable[2, 3])).ToString();
else if (index == 3)
lblCalculatedLength.Text = (n1 * double.Parse(conversionTable[3, 3])).ToString();
else if (index == 4)
lblCalculatedLength.Text = (n1 * double.Parse(conversionTable[4, 3])).ToString();
else if (index == 5)
lblCalculatedLength.Text = (n1 * double.Parse(conversionTable[5, 3])).ToString();
}
}

private void btnExit_Click_Click(object sender, EventArgs e)
{
Dispose();
}

private void cboConversions_SelectedIndexChanged(object sender, EventArgs e)
{
txtLength.Text="";
lblCalculatedLength.Text = "";
int index=cboConversions.SelectedIndex;
if (index == 0)
{
lblFromLength.Text = conversionTable[0, 1];
lblToLength.Text = conversionTable[0, 2];

}
else if (index == 1)
{
lblFromLength.Text = conversionTable[1, 1];
lblToLength.Text = conversionTable[1, 2];
}
else if (index == 2)
{
lblFromLength.Text = conversionTable[2, 1];
lblToLength.Text = conversionTable[2, 2];
}

else if (index == 3)
{
lblFromLength.Text = conversionTable[3, 1];
lblToLength.Text = conversionTable[3, 2];
}
else if (index == 4)
{
lblFromLength.Text = conversionTable[4, 1];
lblToLength.Text =conversionTable[4, 2];
}
else
{
lblFromLength.Text = conversionTable[5, 1];
lblToLength.Text = conversionTable[5, 2];
}

}

private void lblFromLength_Click(object sender, EventArgs e)
{

}

}
}

//**************************************************************************************//

CORRECTED FORM DESIGNER CODE

namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

  

this.lblCalculatedLength = new System.Windows.Forms.Label();
this.txtLength = new System.Windows.Forms.TextBox();
this.lblToLength = new System.Windows.Forms.Label();
this.lblFromLength = new System.Windows.Forms.Label();
this.btnExit_Click = new System.Windows.Forms.Button();
this.btnCalculate_Click = new System.Windows.Forms.Button();
this.cboConversions = new System.Windows.Forms.ComboBox();
this.Label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblCalculatedLength
//
this.lblCalculatedLength.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblCalculatedLength.Location = new System.Drawing.Point(94, 76);
this.lblCalculatedLength.Name = "lblCalculatedLength";
this.lblCalculatedLength.Size = new System.Drawing.Size(96, 20);
this.lblCalculatedLength.TabIndex = 30;
this.lblCalculatedLength.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtLength
//
this.txtLength.Location = new System.Drawing.Point(94, 44);
this.txtLength.Name = "txtLength";
this.txtLength.Size = new System.Drawing.Size(96, 20);
this.txtLength.TabIndex = 25;
//
// lblToLength
//
//string getselectedItem = cboConversions.SelectedItem.ToString();
this.lblToLength.Location = new System.Drawing.Point(14, 76);
this.lblToLength.Name = "lblToLength";
this.lblToLength.Size = new System.Drawing.Size(72, 23);
this.lblToLength.TabIndex = 29;
this.lblToLength.Text = "Kilometers:";
this.lblToLength.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// lblFromLength
//
this.lblFromLength.Location = new System.Drawing.Point(14, 44);
this.lblFromLength.Name = "lblFromLength";
this.lblFromLength.Size = new System.Drawing.Size(72, 23);
this.lblFromLength.TabIndex = 28;
//if(getselectedItem.Equals("Miles to Kilometers"))
  
this.lblFromLength.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lblFromLength.Click += new System.EventHandler(this.lblFromLength_Click);
//
// btnExit_Click
//
this.btnExit_Click.CausesValidation = false;
this.btnExit_Click.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit_Click.Location = new System.Drawing.Point(158, 116);
this.btnExit_Click.Name = "btnExit_Click";
this.btnExit_Click.Size = new System.Drawing.Size(80, 23);
this.btnExit_Click.TabIndex = 27;
this.btnExit_Click.Text = "E&xit";
this.btnExit_Click.Click += new System.EventHandler(this.btnExit_Click_Click);
//
// btnCalculate_Click
//
this.btnCalculate_Click.Location = new System.Drawing.Point(30, 116);
this.btnCalculate_Click.Name = "btnCalculate_Click";
this.btnCalculate_Click.Size = new System.Drawing.Size(80, 23);
this.btnCalculate_Click.TabIndex = 26;
this.btnCalculate_Click.Text = "&Calculate";
this.btnCalculate_Click.Click += new System.EventHandler(this.btnCalculate_Click_Click);
//
// cboConversions
//
this.cboConversions.DropDownWidth = 160;
this.cboConversions.Items.AddRange(new object[] {
conversionTable[0, 0],
conversionTable[1, 0],
conversionTable[2, 0],
conversionTable[3, 0],
conversionTable[4, 0],
conversionTable[5, 0]});
cboConversions.SelectedIndex = 0;


this.cboConversions.Location = new System.Drawing.Point(94, 12);
this.cboConversions.Name = "cboConversions";
this.cboConversions.Size = new System.Drawing.Size(144, 21);
this.cboConversions.TabIndex = 24;
this.cboConversions.SelectedIndexChanged += new System.EventHandler(this.cboConversions_SelectedIndexChanged);


//
// Label1
//
this.Label1.Location = new System.Drawing.Point(14, 12);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(72, 23);
this.Label1.TabIndex = 23;
this.Label1.Text = "Conversion:";
this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AcceptButton = this.btnCalculate_Click;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnExit_Click;
this.ClientSize = new System.Drawing.Size(261, 155);
this.Controls.Add(this.lblCalculatedLength);
this.Controls.Add(this.txtLength);
this.Controls.Add(this.lblToLength);
this.Controls.Add(this.lblFromLength);
this.Controls.Add(this.btnExit_Click);
this.Controls.Add(this.btnCalculate_Click);
this.Controls.Add(this.cboConversions);
this.Controls.Add(this.Label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Conversions";
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion
internal System.Windows.Forms.Label lblCalculatedLength;
internal System.Windows.Forms.TextBox txtLength;
internal System.Windows.Forms.Label lblToLength;
internal System.Windows.Forms.Label lblFromLength;
internal System.Windows.Forms.Button btnExit_Click;
internal System.Windows.Forms.Button btnCalculate_Click;
internal System.Windows.Forms.ComboBox cboConversions;
internal System.Windows.Forms.Label Label1;

OUTPUT

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT