Exemple #1
0
        private void buttonFIndAuthor_Click(object sender, EventArgs e)
        {
            textBoxShow.Clear();
            string author = textBoxFindAuthor.Text + "\r";

            if (author.Length > 0)
            {
                string        path      = @"C:\Books_Library";
                string        path_file = @"C:\Books_Library\lib.txt";
                DirectoryInfo dirInfo   = new DirectoryInfo(path);
                if (!dirInfo.Exists || dirInfo.GetFiles().Length == 0)
                {
                    MessageBox.Show("Нет такого файла или каталога.");
                }
                else if (dirInfo.Exists && dirInfo.GetFiles().Length == 1)
                {
                    StreamReader file = new StreamReader(path_file);
                    string       text_info;
                    text_info = file.ReadToEnd();
                    file.Close();
                    string[] words = text_info.Split('\n');
                    for (int i = 0; i < words.Length - 1; i += 4)
                    {
                        if (author == words[i])
                        {
                            Book book = new Book(words[i], words[i + 1], words[i + 2], Convert.ToInt32(words[i + 3]));
                            //textBoxShow.Text += words[i] + words[i + 1] + words[i + 2] + Convert.ToInt32(words[i + 3]);
                            //textBoxShow.Text += words[i];
                            textBoxShow.Text += book.ToString() + "\r\n";
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Введите значение для поиска.");
            }
        }
        /// <summary>
        /// Provides formatting
        /// </summary>
        /// <param name="format">The format given</param>
        /// <param name="arg">The object to provide format onto</param>
        /// <param name="formatProvider">IFormatProvider instance</param>
        /// <returns>Formatted string</returns>
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (!(arg is Book))
            {
                try
                {
                    return(this.HandleOtherFormats(format, arg));
                }
                catch (FormatException ex)
                {
                    throw new FormatException(nameof(ex));
                }
            }

            Book book = arg as Book;

            if (string.IsNullOrEmpty(format))
            {
                return(book.ToString());
            }

            bool wasFormatted = false;

            foreach (char c in format)
            {
                switch (c)
                {
                case 'I':
                    if (wasFormatted)
                    {
                        result += ", ";
                    }
                    else
                    {
                        wasFormatted = true;
                    }
                    result += "ISBN 13: ";
                    result += book.ISBN;
                    break;

                case 'A':
                    if (wasFormatted)
                    {
                        result += ", ";
                    }
                    else
                    {
                        wasFormatted = true;
                    }
                    result += book.Author;
                    break;

                case 'T':
                    if (wasFormatted)
                    {
                        result += ", ";
                    }
                    else
                    {
                        wasFormatted = true;
                    }
                    result += "\"";
                    result += book.Title;
                    result += "\"";
                    break;

                case 'Y':
                    if (wasFormatted)
                    {
                        result += ", ";
                    }
                    else
                    {
                        wasFormatted = true;
                    }
                    result += book.Year;
                    break;

                case 'P':
                    if (wasFormatted)
                    {
                        result += ", ";
                    }
                    else
                    {
                        wasFormatted = true;
                    }
                    result += book.Price;
                    result += "$";
                    break;

                case 'R':
                    if (wasFormatted)
                    {
                        result += ", ";
                    }
                    else
                    {
                        wasFormatted = true;
                    }
                    result += book.Publisher;
                    break;

                case 'N':
                    if (wasFormatted)
                    {
                        result += ", ";
                    }
                    else
                    {
                        wasFormatted = true;
                    }
                    result += "P. ";
                    result += book.NumOfPages;
                    break;
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                try
                {
                    this.HandleOtherFormats(format, arg);
                }
                catch (FormatException ex)
                {
                    throw new FormatException(nameof(ex));
                }
            }

            return(result);
        }