0

Ввожу имя и ввожу фразу и нажимаю запомнить

введите сюда описание изображения

и раз исключение выскакивает

введите сюда описание изображения

Вопрос решил.Спасибо вам.

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

        namespace LabWork6
        {
            public partial class Form1 : Form
            {

                System.Diagnostics.Stopwatch sw;
                string username;
                string phrase;
                double time;
                bool startLogIn;
                private bool start;

                public Form1()
                {
                    InitializeComponent();
                    sw = new System.Diagnostics.Stopwatch();
                    start = false;
                    phrase = "";                      
                    textBox3.Focus();         
                    startLogIn = false;
                    time = 0;
                }

                private void button1_Click(object sender, EventArgs e)
                {
                    bool checkInput = true;
                    StreamWriter writefiler = new StreamWriter("resultingfile.txt");
                    if (textBox1.Text == "")
                    {
                        MessageBox.Show("Окно пользователя не должно быть пустым!");
                        checkInput = false;
                    }
                    else if (textBox2.Text == "")
                    {
                        MessageBox.Show("Фраза не может быть пустой!");
                        checkInput = false;
                    }
 username=textBox1.Text;
                    if (checkInput && !File.Exists(username))
                    {
                        writefiler = File.CreateText(username);
                        writefiler.WriteLine(phrase + ";" + time / 5 + ";");
                        writefiler.Close();
                        phrase = "";
                        time = 0;
                        start = true;
                        this.Close();
                    }
                }

                private void textBoxPassword_KeyPress(object sender, KeyPressEventArgs e)
                {
                    if (!start)
                    {
                        start = true;
                        sw.Start();
                    }
                    else
                    {
                        if (e.KeyChar == (char)Keys.Enter)
                        {
                            start = false;
                            TimeSpan ts;
                            sw.Stop();
                            ts = sw.Elapsed;
                            sw.Reset();
                            phrase = textBox2.Text;
                            time += ts.TotalSeconds;
                        }
                    }
                }

                private void textBoxLogIn_KeyPress(object sender, KeyPressEventArgs e)
                {
                    if (e.KeyChar == (char)Keys.Enter)
                    {
                        username = textBox1.Text;
                        textBox2.Focus();
                    }
                    else
                        username = textBox1.Text;
                }

                private void button2_Click(object sender, EventArgs e)
                {
                    if (textBox3.Text.Length > 0)
                    {
                        if (textBox4.Text.Length > 0)
                        {
                            if (File.Exists(username))
                            {
                                using (TextReader fileReader = new StreamReader(username))
                                {
                                    string s = "";
                                    while ((s = fileReader.ReadLine()) != null)
                                    {
                                        string pass;
                                        double timeSource;
                                        pass = s.Substring(0, s.IndexOf(";"));
                                        s = s.Remove(0, s.IndexOf(";") + 1);
                                        timeSource = Convert.ToDouble(s.Substring(0, s.IndexOf(";")));
                                        if (pass == phrase)
                                        {
                                            if (timeSource >= time - time * 0.3 && timeSource <= time + time * 0.3)
                                            {
                                                MessageBox.Show("Вход выполнен");
                                                this.Close();
                                            }
                                            else
                                            {
                                                MessageBox.Show("Время ввода не совпадает");
                                                textBox4.Clear();
                                            }
                                        }
                                        else
                                        {
                                            MessageBox.Show("Неверный пароль");
                                            textBox4.Clear();
                                        }
                                    }
                                    phrase = "";
                                    time = 0;
                                    start = true;
                                    fileReader.Close();
                                    this.Close();
                                }
                            }
                            else
                                MessageBox.Show("Нет пользователя");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Введите пароль");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Введите логин");
                    }
                }

                private void textBoxPasswordInput_KeyPress(object sender, KeyPressEventArgs e)
                {
                    if (!start)
                    {
                        start = true;
                        sw.Start();
                    }
                    else
                    {
                        if (e.KeyChar == (char)Keys.Enter)
                        {
                            start = false;
                            TimeSpan ts;
                            sw.Stop();
                            ts = sw.Elapsed;
                            sw.Reset();
                            phrase = textBox4.Text;
                            time = ts.TotalSeconds;
                            button2.Focus();
                        }
                    }
                }

                private void textBoxLogIn_KeyPress1(object sender, KeyPressEventArgs e)
                {
                    if (!startLogIn)
                    {
                        startLogIn = true;
                        username = "";
                    }
                    else
                    {
                        if (e.KeyChar == (char)Keys.Enter)
                        {
                            username = textBox3.Text;
                            textBox4.Focus();
                        }
                        else
                            username = textBox3.Text;
                    }
                }
            }
        }
ixSci
  • 23,825
beginner
  • 380
  • 1
    собственно проверьте значение username - судя по всему оно null – Grundy Dec 01 '15 at 11:54
  • Добавил в код username=textBox1.Text;

    Заработало.Спасибо

    – beginner Dec 01 '15 at 12:43
  • что-то вообще не понял комментарий – Grundy Dec 01 '15 at 12:43
  • слишком много месте где этой переменной что-то присваивается и непонятно откуда – Grundy Dec 01 '15 at 12:45
  • @Grundy, вероятно, ТС нашел место, где нужно было присвоить значение username :) – Vlad Dec 01 '15 at 15:07

0 Answers0