I am working on a project of converting VB code to C# code and I am lost as to how to read data from a prompt in C#.
Here is the snippet I am concerned with fixing, but below I will post the entire code.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
string Prompt = null;
decimal[] Tests1 = null;
NumTest1 = Convert.ToInt16(txtTest1.Text);
Array.Resize(ref Tests1, NumTest1);
short i = 0;
Prompt = "Please enter grade scores one at a time";
for (i = 0; i <= Tests1.Length; i++)
{
Tests1[i] = Convert.ToInt16(Prompt.ToString);
Total1 = Total1 + Tests1[i];
textBox1.Text = Convert.ToString((Total1) / NumTest1);
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Text = "0";
}
}
}
Below this is the entire code, it has a total of 5 errors, all relating to what I am trying to accomplish of reading data from a InputBox / Prompt. If someone can help me figure out the first one, I will be able to correct the other four errors.
To better understand the code, this program is supposed to read a class name, the number of tests in a class, then when the user clicks on the button, read the number of tests being input so it can calculate the average for that class. Also, there is another button (button 5) which calculates the overall average of all classes entered. I believe I have the code 99% correct, except I cannot test it due to not being able to figure out the InputBox in C#.
One last thing, I am using Visual Studio 2010 Ultimate.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace Griffin_GPA_Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double FirstNum;
double SecondNum;
double ThirdNum;
double FourthNum;
int NumTest1;
int NumTest2;
int NumTest3;
int NumTest4;
decimal Total1;
decimal Total2;
decimal Total3;
decimal Total4;
private void Button1_Click(System.Object sender, System.EventArgs e)
{
string Prompt = null;
decimal[] Tests1 = null;
NumTest1 = Convert.ToInt16(txtTest1.Text);
Array.Resize(ref Tests1, NumTest1);
short i = 0;
Prompt = "Please enter grade scores one at a time";
for (i = 0; i <= Tests1.Length; i++)
{
Tests1[i] = Convert.ToInt16(Prompt.ToString);
Total1 = Total1 + Tests1[i];
textBox1.Text = Convert.ToString((Total1) / NumTest1);
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Text = "0";
}
}
}
private void Button2_Click(System.Object sender, System.EventArgs e)
{
string Prompt = null;
decimal[] Tests2 = null;
NumTest2 = Convert.ToInt16(txtTest2.Text);
Array.Resize(ref Tests2, NumTest2);
short j = 0;
Prompt = "Please enter grade scores one at a time";
for (j = 0; j <= Tests2.Length; j++)
{
Tests2[j] = Interaction.InputBox(Prompt);
Total2 = Total2 + Tests2[j];
textBox2.Text = Convert.ToString((Total2) / NumTest2);
if (string.IsNullOrEmpty(textBox2.Text))
{
textBox2.Text = "0";
}
}
}
private void Button3_Click(System.Object sender, System.EventArgs e)
{
string Prompt = null;
decimal[] Tests3 = null;
NumTest3 = Convert.ToInt16(txtTest3.Text);
Array.Resize(ref Tests3, NumTest3);
short k = 0;
Prompt = "Please enter grade scores one at a time";
for (k = 0; k <= Tests3.Length; k++)
{
Tests3[k] = Interaction.InputBox(Prompt);
Total3 = Total3 + Tests3[k];
textBox3.Text = Convert.ToString((Total3) / NumTest3);
if (string.IsNullOrEmpty(textBox3.Text))
{
textBox3.Text = "0";
}
}
}
private void Button4_Click(System.Object sender, System.EventArgs e)
{
string Prompt = null;
decimal[] Tests4 = null;
NumTest4 = Convert.ToInt16(txtTest4.Text);
Array.Resize(ref Tests4, NumTest4);
short l = 0;
Prompt = "Please enter grade scores one at a time";
for (l = 0; l <= Tests4.Length; l++)
{
Tests4[l] = Interaction.InputBox(Prompt);
Total4 = Total4 + Tests4[l];
textBox4.Text = Convert.ToString((Total4) / NumTest4);
if (string.IsNullOrEmpty(textBox4.Text))
{
textBox4.Text = "0";
}
}
}
private void Button5_Click(System.Object sender, System.EventArgs e)
{
textBox5.Text = Convert.ToString(((Convert.ToInt32(textBox1.Text) * (Convert.ToInt32(txtTest1.Text))) +
(Convert.ToInt32(textBox2.Text) * (Convert.ToInt32(txtTest2.Text))) + (Convert.ToInt32(textBox3.Text) *
(Convert.ToInt32(txtTest3.Text))) + (Convert.ToInt32(textBox4.Text) * (Convert.ToInt32(txtTest4.Text))))
/ ((Convert.ToInt32(txtTest1.Text)) + (Convert.ToInt32(txtTest2.Text)) + (Convert.ToInt32(txtTest3.Text))
+ (Convert.ToInt32(txtTest4.Text))));
}
private void Button6_Click(System.Object sender, System.EventArgs e)
{
FirstNum = 0;
SecondNum = 0;
ThirdNum = 0;
FourthNum = 0;
Total1 = 0;
Total2 = 0;
Total3 = 0;
Total4 = 0;
NumTest1 = 0;
NumTest2 = 0;
NumTest3 = 0;
NumTest4 = 0;
txtClass1.Text = "Class 1";
txtClass2.Text = "Class 2";
txtClass3.Text = "Class 3";
txtClass4.Text = "Class 4";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
txtTest1.Text = "0";
txtTest2.Text = "0";
txtTest3.Text = "0";
txtTest4.Text = "0";
}
private void Button7_Click(System.Object sender, System.EventArgs e)
{
StreamWriter StreamToWrite = null;
StreamToWrite = new StreamWriter("C:\\vb10sbs\\GPA\\grades.txt");
StreamToWrite.Write(txtClass1.Text + "\n" + txtTest1.Text + "\n" + textBox1.Text + "\n" +
txtClass2.Text + "\n" + txtTest2.Text + "\n" + textBox2.Text + "\n" +
txtClass3.Text + "\n" + txtTest3.Text + "\n" + textBox3.Text + "\n" +
txtClass4.Text + "\n" + txtTest4.Text + "\n" + textBox4.Text);
StreamToWrite.Close();
System.Environment.Exit(0);
}
private void ToolStripMenuItem1_Click(System.Object sender, System.EventArgs e)
{
StreamReader StreamToRead = null;
StreamToRead = new StreamReader("C:\\vb10sbs\\GPA\\grades.txt");
txtClass1.Text = StreamToRead.ReadLine();
txtTest1.Text = StreamToRead.ReadLine();
textBox1.Text = StreamToRead.ReadLine();
txtClass2.Text = StreamToRead.ReadLine();
txtTest2.Text = StreamToRead.ReadLine();
textBox2.Text = StreamToRead.ReadLine();
txtClass3.Text = StreamToRead.ReadLine();
txtTest3.Text = StreamToRead.ReadLine();
textBox3.Text = StreamToRead.ReadLine();
txtClass4.Text = StreamToRead.ReadLine();
txtTest4.Text = StreamToRead.ReadLine();
textBox4.Text = StreamToRead.ReadLine();
StreamToRead.Close();
}
private void ToolStripMenuItem2_Click(System.Object sender, System.EventArgs e)
{
StreamWriter StreamToWrite = null;
StreamToWrite = new StreamWriter("C:\\vb10sbs\\GPA\\grades.txt");
StreamToWrite.Write(txtClass1.Text + "\n" + txtTest1.Text + "\n" + textBox1.Text + "\n" +
txtClass2.Text + "\n" + txtTest2.Text + "\n" + textBox2.Text + "\n" +
txtClass3.Text + "\n" + txtTest3.Text + "\n" + textBox3.Text + "\n" +
txtClass4.Text + "\n" + txtTest4.Text + "\n" + textBox4.Text);
StreamToWrite.Close();
}
private void ResetToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
button6.PerformClick();
}
private void ExitToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
button7.PerformClick();
}
private void Form1_FormClosing(System.Object sender, System.Windows.Forms.FormClosingEventArgs e)
{
StreamWriter StreamToWrite = null;
StreamToWrite = new StreamWriter("C:\\vb10sbs\\GPA\\grades.txt");
StreamToWrite.Write(txtClass1.Text + "\n" + txtTest1.Text + "\n" + textBox1.Text + "\n" +
txtClass2.Text + "\n" + txtTest2.Text + "\n" + textBox2.Text + "\n" +
txtClass3.Text + "\n" + txtTest3.Text + "\n" + textBox3.Text + "\n" +
txtClass4.Text + "\n" + txtTest4.Text + "\n" + textBox4.Text);
StreamToWrite.Close();
}
private void Form1_Load(System.Object sender, System.EventArgs e)
{
StreamReader StreamToRead = null;
StreamToRead = new StreamReader("C:\\vb10sbs\\GPA\\grades.txt");
txtClass1.Text = StreamToRead.ReadLine();
txtTest1.Text = StreamToRead.ReadLine();
textBox1.Text = StreamToRead.ReadLine();
txtClass2.Text = StreamToRead.ReadLine();
txtTest2.Text = StreamToRead.ReadLine();
textBox2.Text = StreamToRead.ReadLine();
txtClass3.Text = StreamToRead.ReadLine();
txtTest3.Text = StreamToRead.ReadLine();
textBox3.Text = StreamToRead.ReadLine();
txtClass4.Text = StreamToRead.ReadLine();
txtTest4.Text = StreamToRead.ReadLine();
textBox4.Text = StreamToRead.ReadLine();
StreamToRead.Close();
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Text = "0";
}
if (string.IsNullOrEmpty(textBox2.Text))
{
textBox2.Text = "0";
}
if (string.IsNullOrEmpty(textBox3.Text))
{
textBox3.Text = "0";
}
if (string.IsNullOrEmpty(textBox4.Text))
{
textBox4.Text = "0";
}
if (string.IsNullOrEmpty(txtTest1.Text))
{
txtTest1.Text = "0";
}
if (string.IsNullOrEmpty(txtTest2.Text))
{
txtTest2.Text = "0";
}
if (string.IsNullOrEmpty(txtTest3.Text))
{
txtTest3.Text = "0";
}
if (string.IsNullOrEmpty(txtTest4.Text))
{
txtTest4.Text = "0";
}
}
}
}
For those wondering, yes this is an assignment for my college course, and yes it is due tonight. I am continuing to search the web, but everything says there is not a way to use an InputBox / Prompt object, but they all reference earlier versions of Visual Studio as well.