Form11.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 WindowsFormsApp19
  11. {
  12. public partial class Form11 : Form
  13. {
  14. public Form11()
  15. {
  16. InitializeComponent();
  17. dateTimePicker1.Format = DateTimePickerFormat.Time;
  18. dateTimePicker1.ValueChanged += dateTimePicker1_ValueChanged;
  19. monthCalendar1.DateChanged += monthCalendar1_DateChanged;
  20. webBrowser1.Url = new Uri("http://google.com");
  21. button1.Click += button1_Click;
  22. this.ShowInTaskbar = true;
  23. notifyIcon1.Click += notifyIcon1_Click;
  24. button1.Click += button1_Click;
  25. button2.Click += button2_Click;
  26. openFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
  27. saveFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
  28. button6.Click += button1_Click;
  29. // расширенное окно для выбора цвета
  30. colorDialog1.FullOpen = true;
  31. // установка начального цвета для colorDialog
  32. colorDialog1.Color = this.BackColor;
  33. }
  34. private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
  35. {
  36. label1.Text = String.Format("Вы выбрали: {0}", dateTimePicker1.Value.ToLongTimeString());
  37. }
  38. private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
  39. {
  40. label2.Text = String.Format("Вы выбрали: {0}", e.Start.ToLongDateString());
  41. }
  42. private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  43. {
  44. }
  45. private void textBox1_TextChanged(object sender, EventArgs e)
  46. {
  47. }
  48. private void button1_Click(object sender, EventArgs e)
  49. {
  50. webBrowser1.Navigate(textBox1.Text);
  51. }
  52. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  53. {
  54. }
  55. private void notifyIcon1_Click(object sender, EventArgs e)
  56. {
  57. this.WindowState = FormWindowState.Normal;
  58. }
  59. private void button2_Click(object sender, EventArgs e)
  60. {
  61. MessageBox.Show(
  62. "Выберите один из вариантов",
  63. "Сообщение",
  64. MessageBoxButtons.YesNo,
  65. MessageBoxIcon.Information,
  66. MessageBoxDefaultButton.Button1,
  67. MessageBoxOptions.DefaultDesktopOnly);
  68. }
  69. private void button3_Click(object sender, EventArgs e)
  70. {
  71. DialogResult result = MessageBox.Show(
  72. "Окрасить кнопку в красный цвет?",
  73. "Сообщение",
  74. MessageBoxButtons.YesNo,
  75. MessageBoxIcon.Information,
  76. MessageBoxDefaultButton.Button1,
  77. MessageBoxOptions.DefaultDesktopOnly);
  78. if (result == DialogResult.Yes)
  79. button3.BackColor = Color.Red;
  80. this.TopMost = true;
  81. }
  82. private void button4_Click(object sender, EventArgs e)
  83. {
  84. if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
  85. return;
  86. // получаем выбранный файл
  87. string filename = saveFileDialog1.FileName;
  88. // сохраняем текст в файл
  89. System.IO.File.WriteAllText(filename, textBox1.Text);
  90. MessageBox.Show("Файл сохранен");
  91. }
  92. private void button5_Click(object sender, EventArgs e)
  93. {
  94. if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
  95. return;
  96. // получаем выбранный файл
  97. string filename = openFileDialog1.FileName;
  98. // читаем файл в строку
  99. string fileText = System.IO.File.ReadAllText(filename);
  100. textBox1.Text = fileText;
  101. MessageBox.Show("Файл открыт");
  102. }
  103. private void button6_Click(object sender, EventArgs e)
  104. {
  105. if (colorDialog1.ShowDialog() == DialogResult.Cancel)
  106. return;
  107. // установка цвета формы
  108. this.BackColor = colorDialog1.Color;
  109. }
  110. private void Form11_Load(object sender, EventArgs e)
  111. {
  112. nameBox.Validating += nameBox_Validating;
  113. ageBox.Validating += ageBox_Validating;
  114. }
  115. private void namebox_TextChanged(object sender, EventArgs e)
  116. {
  117. }
  118. private void nameBox_Validating(object sender, CancelEventArgs e)
  119. {
  120. if (String.IsNullOrEmpty(nameBox.Text))
  121. {
  122. errorProvider1.SetError(nameBox, "Не указано имя!");
  123. }
  124. else if (nameBox.Text.Length < 4)
  125. {
  126. errorProvider1.SetError(nameBox, "Слишком короткое имя!");
  127. }
  128. else
  129. {
  130. errorProvider1.Clear();
  131. }
  132. }
  133. private void ageBox_Validating(object sender, CancelEventArgs e)
  134. {
  135. int age = 0;
  136. if (String.IsNullOrEmpty(ageBox.Text))
  137. {
  138. errorProvider1.SetError(ageBox, "Не указан возраст!");
  139. }
  140. else if (!Int32.TryParse(ageBox.Text, out age))
  141. {
  142. errorProvider1.SetError(ageBox, "Некорретный возраст!");
  143. }
  144. else
  145. {
  146. errorProvider1.Clear();
  147. }
  148. }
  149. }
  150. }