-1

This code should monitor the user's keystrokes and stops him if s\he types a the wrong character .Yet when it comes to the if statement where it should compare to charecter everything just goes wrong

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;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.IO;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        void Rest()
        {
            counter = -1;

            txt1.Enabled = true;
            txt2.Enabled = true;
            txt3.Enabled = true;
            txt4.Enabled = false;
            txt5.Enabled = true;
            btn2.Enabled = false;
            btn1.Enabled = true;
            pass = "";
            txt4.Clear();
            Dic.Clear();
            turns = 0;
        }

        string path;
        int counter = -1;
        string pass;
        Dictionary<char, letterInfo> Dic;
        int turns = 0;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                if (counter < pass.Length) { MessageBox.Show("WrongInput(Shorter) !!!  "); Rest(); }
                else
                {
                    turns++;
                    if (turns == Convert.ToInt32(txt1.Text))
                    {
                        MessageBox.Show("Test is Done ");
                        /*Writting files */
                        Rest();
                    }
                }
            }
            counter++;
            if (counter >= pass.Length) { MessageBox.Show("WrongInput !!!exceded length  "); Rest(); }
            if ((char)e.KeyValue != pass[counter]) { MessageBox.Show("WrongInput !!!Wrong charecter  " + ((char)e.KeyValue).ToString() + " " + pass[counter].ToString()); Rest(); }
            if (Dic.ContainsKey((char)e.KeyValue))
            {
                Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
            }
            else
            {
                Dic.Add((char)e.KeyValue, new letterInfo());
                Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
            }


        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            Dic[(char)e.KeyValue].end.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string start = "";
            string end = "";
            string letter = "";
            string all;
            foreach (KeyValuePair<char, letterInfo> pair in Dic)
            {
                start = start + " " + pair.Value.start[0];
                end = end + " " + pair.Value.end[0];
                letter = letter + " " + pair.Key;
            }
            all = letter + "\n" + start + "\n" + end;
            MessageBox.Show(all);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                txt1.Enabled = false;
                txt2.Enabled = false;
                txt3.Enabled = false;
                txt4.Enabled = true;
                txt5.Enabled = false;
                btn2.Enabled = true;
                btn1.Enabled = false;
                pass = txt2.Text;
                path = txt3.Text;
                counter = Convert.ToInt32(txt1.Text);
                Dic = new Dictionary<char, letterInfo>();
                /*   if (!File.Exists(path))
                {
                    MessageBox.Show("Error..File not found");
                    Rest();
                }Code to handle the xls files */

            }
            catch (Exception s)
            { MessageBox.Show(s.Message); }
        }
    }
}

This is the function where the problem occurs

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (counter < pass.Length) 
        { 
            MessageBox.Show("WrongInput(Shorter) !!!  "); Rest(); 
        }
        else 
        {
            turns++;
            if (turns == Convert.ToInt32(txt1.Text))
            {
                MessageBox.Show("Test is Done ");
                /*Writting files */
                Rest();
            } 
        }
    }
    counter++;
    if (counter >= pass.Length) 
    { 
        MessageBox.Show("WrongInput !!!exceded length  "); Rest(); 
    }
    if ((char)e.KeyValue != pass[counter]) 
    { 
        MessageBox.Show("WrongInput !!!Wrong charecter  "+((char)e.KeyValue).ToString()+" "+pass[counter].ToString()); Rest(); 
    }
    if (Dic.ContainsKey((char)e.KeyValue))
    {
        Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
    }
    else
    {
        Dic.Add((char)e.KeyValue, new letterInfo());
        Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
    }
}

And those are the problematic if statements

  counter++;
   if (counter >= pass.Length) 
   { 
       MessageBox.Show("WrongInput !!!exceded length  "); Rest(); 
   }
   if ((char)e.KeyValue != pass[counter]) 
   { 
       MessageBox.Show("WrongInput !!!Wrong charecter  "+((char)e.KeyValue).ToString()+" "+pass[counter].ToString()); Rest(); 
   }
crthompson
  • 15,653
  • 6
  • 58
  • 80
HSN
  • 783
  • 3
  • 11
  • 20

1 Answers1

1

You enter into if (counter >= pass.Length) because:

counter = -1; //starts at -1
pass = ""; //starts at empty string (length of zero)

Neither of these variables change in your code until:

counter++; //counter is now 0

counter is now equal to pass.Length. So the if statement is true and "WrongInput !!!exceded length " is printed.

You dont set the value of pass until button 2 is clicked. So on each keypress event that is fired, the pass value is empty.

In if ((char)e.KeyValue != pass[counter]) you are trying to compare the key entered, with the character number in pass. pass is empty and counter increments until you hit button 2. So e.Keyvalue will not equal pass[counter] every time.

crthompson
  • 15,653
  • 6
  • 58
  • 80
  • Thank you for editing the code & answering . "pass" wont be used(to get text strings ) unless button_2 has already been clicked i outputted the value of "pass" inside the "if" statement in a message box and it's working ..yet the entered key -for some reason - is always taken capitalized .i.e when i press "e" the output would be "E" . And idea why is this happening ? – HSN Dec 24 '13 at 19:55
  • @HSN Yes, If you handle the `KeyPress` event instead, you can examine the `KeyChar` property of `KeyPressEventArgs` to get the correct case. – crthompson Dec 24 '13 at 20:02
  • The key enumeration is always capitalized. http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx – crthompson Dec 24 '13 at 20:03
  • 1
    @HSN check this answer. http://stackoverflow.com/questions/3673279/always-in-upper-case-c-winforms – crthompson Dec 24 '13 at 20:03