Windows Forms додаток для аналізу тексту з повним кодом C#
Програма "Аналізатор речень" - це Windows Forms додаток для аналізу тексту, який дозволяє:
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 TabSentenceApp { public partial class Form1 : Form { private readonly char[] _wordSeparators = { ' ', ',', '.', '!', '?', ';', ':', '\t', '\n', '\r', '(', ')', '[', ']', '{', '}', '"', '\'' }; public Form1() { InitializeComponent(); // Підключаємо обробник події tabControl1.SelectedIndexChanged += tabControl1_SelectedIndexChanged; // Додаємо обробники для клавіші Enter txtSentence.KeyDown += TextBox_KeyDown; txtLetter.KeyDown += TextBox_KeyDown; } private void TextBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { // Автоматично переходимо до першої вкладки результатів tabControl1.SelectedTab = tabPageCalc1; e.SuppressKeyPress = true; } } private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { var selectedTab = tabControl1.SelectedTab; if (selectedTab == tabPageCalc1) ShowWordsWithLetter(); else if (selectedTab == tabPageCalc2) ShowWordsWithoutLetter(); else if (selectedTab == tabPageReset) ResetAll(); } private void ShowWordsWithLetter() { // Беремо текст прямо з полів вводу string sentence = txtSentence.Text; string letter = txtLetter.Text; if (string.IsNullOrEmpty(sentence) || string.IsNullOrEmpty(letter)) { txtResult1.Text = "Будь ласка, введіть речення та літеру на вкладці 'Введення'!"; return; } if (letter.Length != 1) { txtResult1.Text = "Будь ласка, введіть рівно одну літеру!"; return; } try { string[] words = sentence.Split(_wordSeparators, StringSplitOptions.RemoveEmptyEntries); var result = new List<string>(); foreach (string word in words) { // Пошук без урахування регістру if (word.IndexOf(letter, StringComparison.OrdinalIgnoreCase) >= 0) { result.Add(word); } } txtResult1.Text = result.Count > 0 ? $"Слова з літерою '{letter}':\n{string.Join("\n", result)}\n\nВсього: {result.Count} слово(в)" : $"Слів з літерою '{letter}' не знайдено"; } catch (Exception ex) { txtResult1.Text = $"Помилка: {ex.Message}"; } } private void ShowWordsWithoutLetter() { // Беремо текст прямо з полів вводу string sentence = txtSentence.Text; string letter = txtLetter.Text; if (string.IsNullOrEmpty(sentence) || string.IsNullOrEmpty(letter)) { txtResult2.Text = "Будь ласка, введіть речення та літеру на вкладці 'Введення'!"; return; } if (letter.Length != 1) { txtResult2.Text = "Будь ласка, введіть рівно одну літеру!"; return; } try { string[] words = sentence.Split(_wordSeparators, StringSplitOptions.RemoveEmptyEntries); var result = new List<string>(); foreach (string word in words) { // Пошук без урахування регістру if (word.IndexOf(letter, StringComparison.OrdinalIgnoreCase) < 0) { result.Add(word); } } txtResult2.Text = result.Count > 0 ? $"Слова без літери '{letter}':\n{string.Join("\n", result)}\n\nВсього: {result.Count} слово(в)" : $"Усі слова містять літеру '{letter}'"; } catch (Exception ex) { txtResult2.Text = $"Помилка: {ex.Message}"; } } private void ResetAll() { txtSentence.Clear(); txtLetter.Clear(); txtResult1.Clear(); txtResult2.Clear(); lblReset.Text = "Очищення виконано! " + DateTime.Now.ToString("HH:mm:ss"); // Повертаємо фокус на поле введення речення tabControl1.SelectedTab = tabPageInput; txtSentence.Focus(); } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } private void BtnTestData_Click(object sender, EventArgs e) { // Тестові дані txtSentence.Text = "Мама мила раму вранці"; txtLetter.Text = "м"; // Оновлюємо результати ShowWordsWithLetter(); ShowWordsWithoutLetter(); // Показуємо результат tabControl1.SelectedTab = tabPageCalc1; } } }
namespace TabSentenceApp { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPageInput = new System.Windows.Forms.TabPage(); this.btnTestData = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.txtLetter = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.txtSentence = new System.Windows.Forms.TextBox(); this.tabPageCalc1 = new System.Windows.Forms.TabPage(); this.txtResult1 = new System.Windows.Forms.TextBox(); this.tabPageCalc2 = new System.Windows.Forms.TabPage(); this.txtResult2 = new System.Windows.Forms.TextBox(); this.tabPageReset = new System.Windows.Forms.TabPage(); this.lblReset = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.tabControl1.SuspendLayout(); this.tabPageInput.SuspendLayout(); this.tabPageCalc1.SuspendLayout(); this.tabPageCalc2.SuspendLayout(); this.tabPageReset.SuspendLayout(); this.SuspendLayout(); // tabControl1 this.tabControl1.Controls.Add(this.tabPageInput); this.tabControl1.Controls.Add(this.tabPageCalc1); this.tabControl1.Controls.Add(this.tabPageCalc2); this.tabControl1.Controls.Add(this.tabPageReset); this.tabControl1.Dock = System.Windows.Forms.DockStyle.Top; this.tabControl1.Location = new System.Drawing.Point(0, 0); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(584, 300); this.tabControl1.TabIndex = 0; // tabPageInput this.tabPageInput.Controls.Add(this.btnTestData); this.tabPageInput.Controls.Add(this.label2); this.tabPageInput.Controls.Add(this.txtLetter); this.tabPageInput.Controls.Add(this.label1); this.tabPageInput.Controls.Add(this.txtSentence); this.tabPageInput.Location = new System.Drawing.Point(4, 24); this.tabPageInput.Name = "tabPageInput"; this.tabPageInput.Padding = new System.Windows.Forms.Padding(3); this.tabPageInput.Size = new System.Drawing.Size(576, 272); this.tabPageInput.TabIndex = 0; this.tabPageInput.Text = "Введення"; this.tabPageInput.UseVisualStyleBackColor = true; // btnTestData this.btnTestData.BackColor = System.Drawing.Color.LightBlue; this.btnTestData.Font = new System.Drawing.Font("Segoe UI", 9F); this.btnTestData.Location = new System.Drawing.Point(140, 142); this.btnTestData.Name = "btnTestData"; this.btnTestData.Size = new System.Drawing.Size(120, 29); this.btnTestData.TabIndex = 4; this.btnTestData.Text = "Тестові дані"; this.btnTestData.UseVisualStyleBackColor = false; this.btnTestData.Click += new System.EventHandler(this.BtnTestData_Click); // label2 this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("Segoe UI", 10F); this.label2.Location = new System.Drawing.Point(20, 120); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(104, 19); this.label2.TabIndex = 3; this.label2.Text = "Літера для пошуку:"; // txtLetter this.txtLetter.Font = new System.Drawing.Font("Segoe UI", 12F); this.txtLetter.Location = new System.Drawing.Point(20, 142); this.txtLetter.MaxLength = 1; this.txtLetter.Name = "txtLetter"; this.txtLetter.Size = new System.Drawing.Size(100, 29); this.txtLetter.TabIndex = 2; this.txtLetter.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // label1 this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("Segoe UI", 10F); this.label1.Location = new System.Drawing.Point(20, 20); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(69, 19); this.label1.TabIndex = 1; this.label1.Text = "Речення:"; // txtSentence this.txtSentence.Font = new System.Drawing.Font("Segoe UI", 10F); this.txtSentence.Location = new System.Drawing.Point(20, 42); this.txtSentence.Multiline = true; this.txtSentence.Name = "txtSentence"; this.txtSentence.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtSentence.Size = new System.Drawing.Size(536, 70); this.txtSentence.TabIndex = 0; // tabPageCalc1 this.tabPageCalc1.Controls.Add(this.txtResult1); this.tabPageCalc1.Location = new System.Drawing.Point(4, 24); this.tabPageCalc1.Name = "tabPageCalc1"; this.tabPageCalc1.Padding = new System.Windows.Forms.Padding(3); this.tabPageCalc1.Size = new System.Drawing.Size(576, 272); this.tabPageCalc1.TabIndex = 1; this.tabPageCalc1.Text = "Слова з літерою"; this.tabPageCalc1.UseVisualStyleBackColor = true; // txtResult1 this.txtResult1.Dock = System.Windows.Forms.DockStyle.Fill; this.txtResult1.Font = new System.Drawing.Font("Segoe UI", 11F); this.txtResult1.Location = new System.Drawing.Point(3, 3); this.txtResult1.Multiline = true; this.txtResult1.Name = "txtResult1"; this.txtResult1.ReadOnly = true; this.txtResult1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtResult1.Size = new System.Drawing.Size(570, 266); this.txtResult1.TabIndex = 0; this.txtResult1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // tabPageCalc2 this.tabPageCalc2.Controls.Add(this.txtResult2); this.tabPageCalc2.Location = new System.Drawing.Point(4, 24); this.tabPageCalc2.Name = "tabPageCalc2"; this.tabPageCalc2.Padding = new System.Windows.Forms.Padding(3); this.tabPageCalc2.Size = new System.Drawing.Size(576, 272); this.tabPageCalc2.TabIndex = 2; this.tabPageCalc2.Text = "Слова без літери"; this.tabPageCalc2.UseVisualStyleBackColor = true; // txtResult2 this.txtResult2.Dock = System.Windows.Forms.DockStyle.Fill; this.txtResult2.Font = new System.Drawing.Font("Segoe UI", 11F); this.txtResult2.Location = new System.Drawing.Point(3, 3); this.txtResult2.Multiline = true; this.txtResult2.Name = "txtResult2"; this.txtResult2.ReadOnly = true; this.txtResult2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtResult2.Size = new System.Drawing.Size(570, 266); this.txtResult2.TabIndex = 0; this.txtResult2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // tabPageReset this.tabPageReset.Controls.Add(this.lblReset); this.tabPageReset.Location = new System.Drawing.Point(4, 24); this.tabPageReset.Name = "tabPageReset"; this.tabPageReset.Padding = new System.Windows.Forms.Padding(3); this.tabPageReset.Size = new System.Drawing.Size(576, 272); this.tabPageReset.TabIndex = 3; this.tabPageReset.Text = "Скидання"; this.tabPageReset.UseVisualStyleBackColor = true; // lblReset this.lblReset.Dock = System.Windows.Forms.DockStyle.Fill; this.lblReset.Font = new System.Drawing.Font("Segoe UI", 14F); this.lblReset.Location = new System.Drawing.Point(3, 3); this.lblReset.Name = "lblReset"; this.lblReset.Size = new System.Drawing.Size(570, 266); this.lblReset.TabIndex = 0; this.lblReset.Text = "Натисніть для очищення всіх даних"; this.lblReset.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // button1 this.button1.BackColor = System.Drawing.Color.LightCoral; this.button1.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Bold); this.button1.Location = new System.Drawing.Point(452, 306); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 40); this.button1.TabIndex = 1; this.button1.Text = "Вихід"; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); // Form1 this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(584, 361); this.Controls.Add(this.button1); this.Controls.Add(this.tabControl1); this.MinimumSize = new System.Drawing.Size(600, 400); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Аналізатор речень"; this.tabControl1.ResumeLayout(false); this.tabPageInput.ResumeLayout(false); this.tabPageInput.PerformLayout(); this.tabPageCalc1.ResumeLayout(false); this.tabPageCalc1.PerformLayout(); this.tabPageCalc2.ResumeLayout(false); this.tabPageCalc2.PerformLayout(); this.tabPageReset.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabPage tabPageInput; private System.Windows.Forms.Button btnTestData; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtLetter; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtSentence; private System.Windows.Forms.TabPage tabPageCalc1; private System.Windows.Forms.TextBox txtResult1; private System.Windows.Forms.TabPage tabPageCalc2; private System.Windows.Forms.TextBox txtResult2; private System.Windows.Forms.TabPage tabPageReset; private System.Windows.Forms.Label lblReset; private System.Windows.Forms.Button button1; } }