Advertisement
DarkDevourer

лр4

Dec 25th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  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.  
  11.  
  12. namespace ЛР4
  13. {
  14. partial class Form1
  15. {
  16. private int lat;
  17. private TextBox textBox;
  18. int space = 0;
  19.  
  20. private System.ComponentModel.IContainer components = null;
  21. protected override void Dispose(bool disposing)
  22. {
  23. if (disposing && (components != null))
  24. {
  25. components.Dispose();
  26. }
  27. base.Dispose(disposing);
  28. }
  29. private void InitializeComponent()
  30. {
  31. this.textBox = new System.Windows.Forms.TextBox();
  32.  
  33. this.textBox.AcceptsReturn = true;
  34. this.textBox.Font = new System.Drawing.Font("Times New Roman", 16F);
  35. this.textBox.Location = new System.Drawing.Point(10, 10);
  36. this.textBox.Multiline = true;
  37. this.textBox.Name = "textBox";
  38. this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  39. this.textBox.Size = new System.Drawing.Size(280, 40);
  40. this.textBox.KeyUp += textBox_KeyUp;
  41. this.textBox.KeyPress += textBox_KeyPress;
  42.  
  43. this.ClientSize = new System.Drawing.Size(300, 100);
  44. this.Controls.Add(this.textBox);
  45. this.Text = "Редактирование на лету";
  46. }
  47.  
  48. private void textBox_KeyPress(object sender, KeyPressEventArgs e)
  49. {
  50. if (Char.IsLetter(e.KeyChar) || (e.KeyChar == ' '))
  51. if (textBox.SelectionStart == 0) //Если в начале строки
  52. if (e.KeyChar == ' ') e.Handled = true; //Если в начале строки пробел
  53. else
  54. {
  55. e.KeyChar = Char.ToUpper(e.KeyChar); //Если вначале строки началась фамилия
  56. if ((e.KeyChar >= 'a') && (e.KeyChar <= 'z'))
  57. lat = 1;
  58. else lat = 0;
  59. }
  60. else if (e.KeyChar == ' ') //Если не в начале строки и ставим пробел
  61. if (space == 0) space = 1; //Если был ввод фамилии, то он завершён
  62. else e.Handled = true;
  63. else if (space > 0) //Если Вводится один из инициалов
  64. {
  65. if (((lat == 1) && ((e.KeyChar < 'a') || (e.KeyChar > 'z'))) || ((lat == 0) && ((e.KeyChar < 'а') || (e.KeyChar > 'я'))))
  66. { e.Handled = true; }
  67. else
  68. {
  69. e.KeyChar = Char.ToUpper(e.KeyChar); //Сделать его заглавным
  70. space++; //Увеличить позицию
  71. }
  72. }
  73. else
  74. {
  75. if (((lat == 1) && ((e.KeyChar < 'a') || (e.KeyChar > 'z'))) || ((lat == 0) && ((e.KeyChar < 'а') || (e.KeyChar > 'я'))))
  76. { e.Handled = true; }
  77. }
  78. else e.Handled = true;
  79. }
  80.  
  81. private void textBox_KeyUp(object sender, KeyEventArgs e)
  82. {
  83. if (space == 2) //Если вводится первый инициал
  84. {
  85. textBox.AppendText(". "); //Добавить точку
  86. textBox.SelectionStart = textBox.Text.Length;
  87. textBox.SelectionLength = 0; //Снять выделение
  88. }
  89. if (space == 3)
  90. {
  91. textBox.AppendText("."); //Если вводится второй инициал
  92. textBox.SelectionStart = 0; //Выделить введённую строку
  93. textBox.SelectionLength = textBox.Text.Length;
  94. space = 0; //Перейти к вводу новой строки
  95. };
  96. }
  97. }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement