Advertisement
CherMi

asdsa

Dec 11th, 2020 (edited)
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 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 Lab4
  13. {
  14.     partial class Form1
  15.     {
  16.         private TextBox textBox;
  17.         int space = 0;
  18.         bool lat = false;
  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, 80);
  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') ||(e.KeyChar >= 'A' && e.KeyChar <= 'Z')) lat = true;
  57.                         else lat = false;
  58.                     }
  59.                 else if (e.KeyChar == ' ')  //Если не в начале строки и ставим пробел
  60.                     if (space == 0) space = 1; //Если был ввод фамилии, то он завершён
  61.                     else e.Handled = true;
  62.                 else if (space > 0) //Если Вводится один из инициалов
  63.                 {
  64.                     if ((lat && (e.KeyChar >= 'a' && e.KeyChar <= 'z')) || (!lat && (e.KeyChar >= 'а' && e.KeyChar <= 'я')))
  65.                     {
  66.                         e.KeyChar = Char.ToUpper(e.KeyChar); //Сделать его заглавным
  67.                         space++; //Увеличить позицию
  68.                     }
  69.                     else e.Handled = true;
  70.                 }
  71.                 else
  72.                 {
  73.                     if (!((lat && (e.KeyChar >= 'a' && e.KeyChar <= 'z')) || (!lat && (e.KeyChar >= 'а' && e.KeyChar <= 'я'))))
  74.                         e.Handled = true;
  75.                 }
  76.             else e.Handled = true;
  77.         }
  78.  
  79.         private void textBox_KeyUp(object sender, KeyEventArgs e)
  80.         {
  81.             if (space == 2) //Если вводится первый инициал
  82.             {
  83.                 textBox.AppendText(". "); //Добавить точку и пробел
  84.                 textBox.SelectionStart = textBox.Text.Length;
  85.                 textBox.SelectionLength = 0; //Снять выделение
  86.             }
  87.             if (space == 3)
  88.             {
  89.                 textBox.AppendText("."); //Если вводится второй инициал
  90.                 textBox.SelectionStart = 0; //Выделить введённую строку
  91.                 textBox.SelectionLength = textBox.Text.Length;
  92.                 space = 0; //Перейти к вводу новой строки
  93.             };
  94.         }
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement