diff --git a/Metoder.slnx b/Metoder.slnx new file mode 100644 index 0000000..ea46212 --- /dev/null +++ b/Metoder.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Metoder/Form1.Designer.cs b/Metoder/Form1.Designer.cs new file mode 100644 index 0000000..c052d99 --- /dev/null +++ b/Metoder/Form1.Designer.cs @@ -0,0 +1,135 @@ +namespace Metoder +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + btn_find = new Button(); + txtbx_find = new TextBox(); + btn_clear = new Button(); + lbl_found = new Label(); + txtbx_text = new RichTextBox(); + txbx_replace = new TextBox(); + btn_replace = new Button(); + SuspendLayout(); + // + // btn_find + // + btn_find.Anchor = AnchorStyles.Top | AnchorStyles.Right; + btn_find.Location = new Point(676, 6); + btn_find.Name = "btn_find"; + btn_find.Size = new Size(112, 34); + btn_find.TabIndex = 1; + btn_find.Text = "Find"; + btn_find.UseVisualStyleBackColor = true; + btn_find.Click += btn_find_Click; + // + // txtbx_find + // + txtbx_find.Anchor = AnchorStyles.Top | AnchorStyles.Right; + txtbx_find.Location = new Point(520, 6); + txtbx_find.Name = "txtbx_find"; + txtbx_find.PlaceholderText = "Search text"; + txtbx_find.Size = new Size(150, 31); + txtbx_find.TabIndex = 2; + // + // btn_clear + // + btn_clear.Location = new Point(12, 9); + btn_clear.Name = "btn_clear"; + btn_clear.Size = new Size(112, 34); + btn_clear.TabIndex = 3; + btn_clear.Text = "Clear"; + btn_clear.UseVisualStyleBackColor = true; + btn_clear.Click += btn_clear_Click; + // + // lbl_found + // + lbl_found.Anchor = AnchorStyles.Top | AnchorStyles.Right; + lbl_found.AutoSize = true; + lbl_found.Location = new Point(676, 44); + lbl_found.Name = "lbl_found"; + lbl_found.Size = new Size(108, 25); + lbl_found.TabIndex = 4; + lbl_found.Text = "items found"; + lbl_found.Visible = false; + // + // txtbx_text + // + txtbx_text.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + txtbx_text.Location = new Point(12, 72); + txtbx_text.Name = "txtbx_text"; + txtbx_text.Size = new Size(776, 352); + txtbx_text.TabIndex = 5; + txtbx_text.Text = ""; + // + // txbx_replace + // + txbx_replace.Location = new Point(171, 8); + txbx_replace.Name = "txbx_replace"; + txbx_replace.PlaceholderText = "Replace found text with"; + txbx_replace.Size = new Size(197, 31); + txbx_replace.TabIndex = 6; + // + // btn_replace + // + btn_replace.Location = new Point(374, 6); + btn_replace.Name = "btn_replace"; + btn_replace.Size = new Size(112, 34); + btn_replace.TabIndex = 7; + btn_replace.Text = "Replace found"; + btn_replace.UseVisualStyleBackColor = true; + btn_replace.Click += btn_replace_Click; + // + // Form1 + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(btn_replace); + Controls.Add(txbx_replace); + Controls.Add(txtbx_text); + Controls.Add(lbl_found); + Controls.Add(btn_clear); + Controls.Add(txtbx_find); + Controls.Add(btn_find); + Name = "Form1"; + Text = "TextChecker"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + private Button btn_find; + private TextBox txtbx_find; + private Button btn_clear; + private Label lbl_found; + private RichTextBox txtbx_text; + private TextBox txbx_replace; + private Button btn_replace; + } +} diff --git a/Metoder/Form1.cs b/Metoder/Form1.cs new file mode 100644 index 0000000..aa77b23 --- /dev/null +++ b/Metoder/Form1.cs @@ -0,0 +1,93 @@ +using System; +using System.Net.Security; +using System.Reflection; +using static System.Net.Mime.MediaTypeNames; + +namespace Metoder +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + private void btn_find_Click(object sender, EventArgs e) + { + string searchWord = txtbx_find.Text; + Highlight(searchWord); + } + + private void btn_clear_Click(object sender, EventArgs e) + { + lbl_found.Visible = false; + } + + void UpdateFindResult(int foundCount) + { + lbl_found.Visible = true; + string suffix; + + // correct grammar + if (foundCount == 1) + suffix = " item found"; + else + suffix = " items found"; + + lbl_found.Text = foundCount.ToString() + suffix; + } + + (int start, int length) FindTextIndex(string text, int startIndex) + { + int start = (text.IndexOf(text, startIndex, StringComparison.OrdinalIgnoreCase)); + if (start == -1) return (-1, 0); + + return (start, text.Length); + } + + void Highlight(string word) + { + if (word == "") return; // avoid errors, infinite loop + + RichTextBox richTextBox = txtbx_text; + string text = richTextBox.Text; + + // reset old highlight + richTextBox.SelectAll(); + richTextBox.SelectionBackColor = richTextBox.BackColor; + + int found = 0; + int caret = richTextBox.SelectionStart; // set cursor + + var words = GetWords(); + int index = 0; + + while ((index = text.IndexOf(word, index, StringComparison.OrdinalIgnoreCase)) != -1) + { + richTextBox.Select(index, word.Length); // select word + richTextBox.SelectionBackColor = Color.LightBlue; + found++; // count found + index = index + word.Length; // offset (ignore word next loop) + } + + // reset cursor + richTextBox.Select(caret, 0); + + // update find label + UpdateFindResult(found); + } + + List GetWords() // get list of words separated by spaces + { + string text = txtbx_text.Text; + string[] words = text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries); + + return words.ToList(); + } + + private void btn_replace_Click(object sender, EventArgs e) + { + + } + } +} diff --git a/Metoder/Form1.resx b/Metoder/Form1.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/Metoder/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Metoder/Metoder.csproj b/Metoder/Metoder.csproj new file mode 100644 index 0000000..663fdb8 --- /dev/null +++ b/Metoder/Metoder.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net8.0-windows + enable + true + enable + + + \ No newline at end of file diff --git a/Metoder/Program.cs b/Metoder/Program.cs new file mode 100644 index 0000000..f267a85 --- /dev/null +++ b/Metoder/Program.cs @@ -0,0 +1,17 @@ +namespace Metoder +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); + } + } +} \ No newline at end of file