c# books

basically i am trying to convert a little c++ console app to a C# program with gui

how do i declare an array once so i can use with lots of textboxes which capture the different array elements (this make sense?)

here is code I am working on

i can get to work if i declare in each instance of textbox, but i know this is just wrong way to do this , any tips please?


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

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

}

private void textBox4_TextChanged(object sender, EventArgs e)
{
int FormulaTotal = 0;
FormulaTotal = int.Parse(textBox4.Text);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
string RefID = textBox1.Text;
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
string Epcode = textBox2.Text;
}

private void textBox3_TextChanged(object sender, EventArgs e)
{
string Colourist = textBox3.Text;
}

private void Form1_Load(object sender, EventArgs e)
{
string[] basics = new string[] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
int [] Amounts =new int[] {0,0,0,0,0,0,0,0,0};


}

private void textBox5_TextChanged(object sender, EventArgs e)
{
string[] basics = new string[9] ;
basics[0] = textBox5.Text;


}

private void textBox6_TextChanged(object sender, EventArgs e)
{


}


}
}
 
my knowledge of c++ is basics at best, just what I have taught myself .


he is C++ code I'm trying to convert

#include <iostream>

using namespace std;

void label ();

int main ()
{

float total;
int numberOfBasics;
int basicArray [8];
float amountArray [] = {0,0,0,0,0,0,0,0,0};
float endAmount [] = {0,0,0,0,0,0,0,0,0};
cout << "Please enter the number of basics: ";
cin >> numberOfBasics;
cout <<"please enter the formula total: ";
float FormulaTotal=0;
cin >> FormulaTotal;
int x = 0;

while (x < numberOfBasics)
{
int x=0;
cout <<"Enter basic: ";
cin >> basicArray [x]; // get recipe
cout <<"Enter amount: ";
cin >> amountArray [x];
++x;
}

total = amountArray[0]+ amountArray[1] + amountArray[2]
+ amountArray[3] +amountArray[4]+amountArray[5]
+amountArray[6]+amountArray[7]+amountArray[8];
x=0;
while (x < numberOfBasics)
{
x=0;
float formTotal;
endAmount[x] = amountArray[x]/total*formTotal;
++x;
}
cout<< "\n\n\nFormula\n";

x=0;

while (x < numberOfBasics)
{
x=0;
cout<< basicArray[x];
cout<< " : ";
cout<< endAmount[x];
cout<<"\n";
++x;
}

char quit[6];
cin >> quit;
return 1;
}
 
Last edited:
yes it compiles and does what i want it too, probably isn't coded very well mind you :eek:


i am resetting the value of x so that the loop only runs equal to the amount if basics selected at start. I just put this together with what I could remember from when I went to college.
 
Last edited:
Una said:
yeah thats a start.. would rewrite it for you if I knew what you were trying to do in plain english :P Things like
float amountArray [] = {0,0,0,0,0,0,0,0,0};
float endAmount [] = {0,0,0,0,0,0,0,0,0};

Are obviously gonna limit you to 9 inputs.

basically I want to enter up to 9 basics(ingredients) with 9 values(in grams to 1 decimal palace. and have a the output averaged down to the total(normall 100grams) ideally i want to be able to format output and print.
 
Back
Top Bottom