Advertisement
CherMi

Lab4

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