Form8.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Praktika409
  11. {
  12. public partial class Form8 : Form
  13. {
  14. public Form8()
  15. {
  16. InitializeComponent();
  17. button1.Click += button1_Click;
  18. button2.Click += button2_Click;
  19. openFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
  20. saveFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
  21. button1.Click += button1_Click;
  22. // добавляем возможность выбора цвета шрифта
  23. fontDialog1.ShowColor = true;
  24. nameBox.Validating += nameBox_Validating;
  25. ageBox.Validating += ageBox_Validating;
  26. }
  27. private void button1_Click(object sender, EventArgs e)
  28. {
  29. DialogResult result = MessageBox.Show(
  30. "Окрасить кнопку в красный цвет?",
  31. "Сообщение",
  32. MessageBoxButtons.YesNo,
  33. MessageBoxIcon.Information,
  34. MessageBoxDefaultButton.Button1,
  35. MessageBoxOptions.DefaultDesktopOnly);
  36. if (result == DialogResult.Yes)
  37. button1.BackColor = Color.Red;
  38. this.TopMost = true;
  39. }
  40. private void button2_Click(object sender, EventArgs e)
  41. {
  42. if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
  43. return;
  44. // получаем выбранный файл
  45. string filename = openFileDialog1.FileName;
  46. // читаем файл в строку
  47. string fileText = System.IO.File.ReadAllText(filename);
  48. textBox1.Text = fileText;
  49. MessageBox.Show("Файл открыт");
  50. }
  51. private void button3_Click(object sender, EventArgs e)
  52. {
  53. if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
  54. return;
  55. // получаем выбранный файл
  56. string filename = saveFileDialog1.FileName;
  57. // сохраняем текст в файл
  58. System.IO.File.WriteAllText(filename, textBox1.Text);
  59. MessageBox.Show("Файл сохранен");
  60. }
  61. private void button4_Click(object sender, EventArgs e)
  62. {
  63. if (fontDialog1.ShowDialog() == DialogResult.Cancel)
  64. return;
  65. // установка шрифта
  66. button4.Font = fontDialog1.Font;
  67. // установка цвета шрифта
  68. button4.ForeColor = fontDialog1.Color;
  69. }
  70. private void nameBox_TextChanged(object sender, EventArgs e)
  71. {
  72. }
  73. private void nameBox_Validating(object sender, CancelEventArgs e)
  74. {
  75. if (String.IsNullOrEmpty(nameBox.Text))
  76. {
  77. errorProvider1.SetError(nameBox, "Не указано имя!");
  78. }
  79. else if (nameBox.Text.Length < 4)
  80. {
  81. errorProvider1.SetError(nameBox, "Слишком короткое имя!");
  82. }
  83. else
  84. {
  85. errorProvider1.Clear();
  86. }
  87. }
  88. private void ageBox_Validating(object sender, CancelEventArgs e)
  89. {
  90. int age = 0;
  91. if (String.IsNullOrEmpty(ageBox.Text))
  92. {
  93. errorProvider1.SetError(ageBox, "Не указан возраст!");
  94. }
  95. else if (!Int32.TryParse(ageBox.Text, out age))
  96. {
  97. errorProvider1.SetError(ageBox, "Некорретный возраст!");
  98. }
  99. else
  100. {
  101. errorProvider1.Clear();
  102. }
  103. }
  104. }
  105. }