I'm trying to make a project which "converts" for example from meters to centimeters. I fill 20 labels with a random generated question containing a "fromUnit" and "toUnit" object.

Here is my code:

public partial class Metrics : Form
      private double[] _answer;
      private TextBox[] _textBox;
      private Label[] _labels;

      public Metrics()
         this._answer = new double[20];
         this._textBox = new TextBox[20];
         this._labels = new Label[20];
         this.label1.Text = this.getQuestion(1, 10);

      private string[][] lengths = new string[][]
         new string[] {"kilometer", "1000"},
         new string[] {"hectometer", "100"},
         new string[] {"decameter", "10"},
         new string[] {"meter", "1"},
         new string[] {"decimeter", ".1"},
         new string[] {"centimeter", ".01"},
         new string[] {"millimeter", ".001"}

      public string[] getUnit(int index)
         return lengths[index];

      private string getQuestion(int minLength, int maxLength)
         // Create Random object
         Random rand = new Random((int)DateTime.Now.Ticks);

         int length = rand.Next(minLength, maxLength); // Gets the length to convert
         int randNumber1 = rand.Next(0, 6); // gets the first unit random number
         int randNumber2 = rand.Next(0, 6); // gets the second unit random number

         while (randNumber1 == randNumber2) // loop ensure that units are not the same
            randNumber2 = rand.Next(0, 6);

         string[] fromUnit = getUnit(randNumber1);  // gets the unit details from class
         string[] toUnit = getUnit(randNumber2);  // gets the unit details from class

         // gets the answer (i.e. (23 * .(01 / .001)) = 230)
         this._answer = length * (Convert.ToDouble(fromUnit[1]) / (Convert.ToDouble(toUnit[1])));

         //return the question string
         return "How much is " + length.ToString() + " " + fromUnit[0] + "(s) in " + toUnit[0] + "(s)?";

      private void btnCalculate_Click(object sender, EventArgs e)


      private void FillLabelsWithQuestions()
         foreach (Control label in this.Controls)
            if (label is Label)
               label.Text = getQuestion(1, 100);

The problem is, i don't know how to check if the answer is correct which is entered in a textbox and checks the answer when the button "calculate" is pressed, because there are strings involved too.

My labels are named like this:


And so are my textboxes:


This is a screenshot of how my Form looks like:


Anyone any suggestion?

Thanks in advance!

Member Avatar

Have you heard of string compare?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.