Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Lab4
- {
- partial class Form1
- {
- private TextBox textBox;
- int space = 0;
- bool lat = false;
- private System.ComponentModel.IContainer components = null;
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- this.textBox = new System.Windows.Forms.TextBox();
- this.textBox.AcceptsReturn = true;
- this.textBox.Font = new System.Drawing.Font("Times New Roman", 16F);
- this.textBox.Location = new System.Drawing.Point(10, 10);
- this.textBox.Multiline = true;
- this.textBox.Name = "textBox";
- this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
- this.textBox.Size = new System.Drawing.Size(280, 80);
- this.textBox.KeyUp += textBox_KeyUp;
- this.textBox.KeyPress += textBox_KeyPress;
- this.ClientSize = new System.Drawing.Size(300, 100);
- this.Controls.Add(this.textBox);
- this.Text = "Редактирование на лету";
- }
- private void textBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (Char.IsLetter(e.KeyChar) || (e.KeyChar == ' '))
- if (textBox.SelectionStart == 0) //Если в начале строки
- if (e.KeyChar == ' ') e.Handled = true; //Если в начале строки пробел
- else
- {
- e.KeyChar = Char.ToUpper(e.KeyChar); //Если вначале строки началась фамилия
- if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') ||(e.KeyChar >= 'A' && e.KeyChar <= 'Z')) lat = true;
- else lat = false;
- }
- else if (e.KeyChar == ' ') //Если не в начале строки и ставим пробел
- if (space == 0) space = 1; //Если был ввод фамилии, то он завершён
- else e.Handled = true;
- else if (space > 0) //Если Вводится один из инициалов
- {
- if ((lat && (e.KeyChar >= 'a' && e.KeyChar <= 'z')) || (!lat && (e.KeyChar >= 'а' && e.KeyChar <= 'я')))
- {
- e.KeyChar = Char.ToUpper(e.KeyChar); //Сделать его заглавным
- space++; //Увеличить позицию
- }
- else e.Handled = true;
- }
- else
- {
- if (!((lat && (e.KeyChar >= 'a' && e.KeyChar <= 'z')) || (!lat && (e.KeyChar >= 'а' && e.KeyChar <= 'я'))))
- e.Handled = true;
- }
- else e.Handled = true;
- }
- private void textBox_KeyUp(object sender, KeyEventArgs e)
- {
- if (space == 2) //Если вводится первый инициал
- {
- textBox.AppendText(". "); //Добавить точку и пробел
- textBox.SelectionStart = textBox.Text.Length;
- textBox.SelectionLength = 0; //Снять выделение
- }
- if (space == 3)
- {
- textBox.AppendText("."); //Если вводится второй инициал
- textBox.SelectionStart = 0; //Выделить введённую строку
- textBox.SelectionLength = textBox.Text.Length;
- space = 0; //Перейти к вводу новой строки
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement