added sort and replace
This commit is contained in:
71
Metoder/Form1.Designer.cs
generated
71
Metoder/Form1.Designer.cs
generated
@@ -33,16 +33,18 @@
|
||||
btn_clear = new Button();
|
||||
lbl_found = new Label();
|
||||
txtbx_text = new RichTextBox();
|
||||
txbx_replace = new TextBox();
|
||||
txtbx_replace = new TextBox();
|
||||
btn_replace = new Button();
|
||||
btn_sort = new Button();
|
||||
SuspendLayout();
|
||||
//
|
||||
// btn_find
|
||||
//
|
||||
btn_find.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
btn_find.Location = new Point(676, 6);
|
||||
btn_find.Location = new Point(647, 4);
|
||||
btn_find.Margin = new Padding(2);
|
||||
btn_find.Name = "btn_find";
|
||||
btn_find.Size = new Size(112, 34);
|
||||
btn_find.Size = new Size(78, 24);
|
||||
btn_find.TabIndex = 1;
|
||||
btn_find.Text = "Find";
|
||||
btn_find.UseVisualStyleBackColor = true;
|
||||
@@ -51,17 +53,19 @@
|
||||
// txtbx_find
|
||||
//
|
||||
txtbx_find.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
txtbx_find.Location = new Point(520, 6);
|
||||
txtbx_find.Location = new Point(538, 4);
|
||||
txtbx_find.Margin = new Padding(2);
|
||||
txtbx_find.Name = "txtbx_find";
|
||||
txtbx_find.PlaceholderText = "Search text";
|
||||
txtbx_find.Size = new Size(150, 31);
|
||||
txtbx_find.Size = new Size(106, 23);
|
||||
txtbx_find.TabIndex = 2;
|
||||
//
|
||||
// btn_clear
|
||||
//
|
||||
btn_clear.Location = new Point(12, 9);
|
||||
btn_clear.Location = new Point(8, 5);
|
||||
btn_clear.Margin = new Padding(2);
|
||||
btn_clear.Name = "btn_clear";
|
||||
btn_clear.Size = new Size(112, 34);
|
||||
btn_clear.Size = new Size(78, 23);
|
||||
btn_clear.TabIndex = 3;
|
||||
btn_clear.Text = "Clear";
|
||||
btn_clear.UseVisualStyleBackColor = true;
|
||||
@@ -71,9 +75,10 @@
|
||||
//
|
||||
lbl_found.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
lbl_found.AutoSize = true;
|
||||
lbl_found.Location = new Point(676, 44);
|
||||
lbl_found.Location = new Point(647, 30);
|
||||
lbl_found.Margin = new Padding(2, 0, 2, 0);
|
||||
lbl_found.Name = "lbl_found";
|
||||
lbl_found.Size = new Size(108, 25);
|
||||
lbl_found.Size = new Size(71, 15);
|
||||
lbl_found.TabIndex = 4;
|
||||
lbl_found.Text = "items found";
|
||||
lbl_found.Visible = false;
|
||||
@@ -81,44 +86,61 @@
|
||||
// txtbx_text
|
||||
//
|
||||
txtbx_text.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
|
||||
txtbx_text.Location = new Point(12, 72);
|
||||
txtbx_text.Location = new Point(8, 47);
|
||||
txtbx_text.Margin = new Padding(2);
|
||||
txtbx_text.Name = "txtbx_text";
|
||||
txtbx_text.Size = new Size(776, 352);
|
||||
txtbx_text.Size = new Size(718, 355);
|
||||
txtbx_text.TabIndex = 5;
|
||||
txtbx_text.Text = "";
|
||||
//
|
||||
// txbx_replace
|
||||
// txtbx_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;
|
||||
txtbx_replace.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
txtbx_replace.Location = new Point(312, 5);
|
||||
txtbx_replace.Margin = new Padding(2);
|
||||
txtbx_replace.Name = "txtbx_replace";
|
||||
txtbx_replace.PlaceholderText = "Replace found text with";
|
||||
txtbx_replace.Size = new Size(139, 23);
|
||||
txtbx_replace.TabIndex = 6;
|
||||
//
|
||||
// btn_replace
|
||||
//
|
||||
btn_replace.Location = new Point(374, 6);
|
||||
btn_replace.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
btn_replace.Location = new Point(454, 4);
|
||||
btn_replace.Margin = new Padding(2);
|
||||
btn_replace.Name = "btn_replace";
|
||||
btn_replace.Size = new Size(112, 34);
|
||||
btn_replace.Size = new Size(78, 24);
|
||||
btn_replace.TabIndex = 7;
|
||||
btn_replace.Text = "Replace found";
|
||||
btn_replace.UseVisualStyleBackColor = true;
|
||||
btn_replace.Click += btn_replace_Click;
|
||||
//
|
||||
// btn_sort
|
||||
//
|
||||
btn_sort.Location = new Point(91, 5);
|
||||
btn_sort.Name = "btn_sort";
|
||||
btn_sort.Size = new Size(96, 23);
|
||||
btn_sort.TabIndex = 8;
|
||||
btn_sort.Text = "Sort Words";
|
||||
btn_sort.UseVisualStyleBackColor = true;
|
||||
btn_sort.Click += btn_sort_Click;
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(10F, 25F);
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
ClientSize = new Size(734, 416);
|
||||
Controls.Add(btn_sort);
|
||||
Controls.Add(btn_replace);
|
||||
Controls.Add(txbx_replace);
|
||||
Controls.Add(txtbx_replace);
|
||||
Controls.Add(txtbx_text);
|
||||
Controls.Add(lbl_found);
|
||||
Controls.Add(btn_clear);
|
||||
Controls.Add(txtbx_find);
|
||||
Controls.Add(btn_find);
|
||||
Margin = new Padding(2);
|
||||
Name = "Form1";
|
||||
Text = "TextChecker";
|
||||
Text = "Text Tool";
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
@@ -129,7 +151,8 @@
|
||||
private Button btn_clear;
|
||||
private Label lbl_found;
|
||||
private RichTextBox txtbx_text;
|
||||
private TextBox txbx_replace;
|
||||
private TextBox txtbx_replace;
|
||||
private Button btn_replace;
|
||||
private Button btn_sort;
|
||||
}
|
||||
}
|
||||
|
||||
124
Metoder/Form1.cs
124
Metoder/Form1.cs
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Net.Security;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace Metoder
|
||||
@@ -12,10 +15,19 @@ namespace Metoder
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
// button binds
|
||||
private void btn_find_Click(object sender, EventArgs e)
|
||||
{
|
||||
string searchWord = txtbx_find.Text;
|
||||
Highlight(searchWord);
|
||||
string pattern = txtbx_find.Text;
|
||||
Highlight(pattern);
|
||||
}
|
||||
|
||||
private void btn_replace_Click(object sender, EventArgs e)
|
||||
{
|
||||
string pattern = txtbx_find.Text;
|
||||
string replacement = txtbx_replace.Text;
|
||||
|
||||
ReplaceFound(pattern, replacement);
|
||||
}
|
||||
|
||||
private void btn_clear_Click(object sender, EventArgs e)
|
||||
@@ -23,6 +35,13 @@ namespace Metoder
|
||||
lbl_found.Visible = false;
|
||||
}
|
||||
|
||||
private void btn_sort_Click(object sender, EventArgs e)
|
||||
{
|
||||
SortWords();
|
||||
}
|
||||
|
||||
|
||||
// methods
|
||||
void UpdateFindResult(int foundCount)
|
||||
{
|
||||
lbl_found.Visible = true;
|
||||
@@ -37,18 +56,26 @@ namespace Metoder
|
||||
lbl_found.Text = foundCount.ToString() + suffix;
|
||||
}
|
||||
|
||||
(int start, int length) FindTextIndex(string text, int startIndex)
|
||||
Dictionary<int, int> GetMatchesIndices(string pattern, string text)
|
||||
{
|
||||
int start = (text.IndexOf(text, startIndex, StringComparison.OrdinalIgnoreCase));
|
||||
if (start == -1) return (-1, 0);
|
||||
Dictionary<int, int> dict = new Dictionary<int, int>();
|
||||
|
||||
return (start, text.Length);
|
||||
if (pattern == "") return dict; // avoid errors, infinite loop
|
||||
|
||||
int index = 0;
|
||||
|
||||
// find and save start index and length of each occurance
|
||||
while ((index = text.IndexOf(pattern, index, StringComparison.OrdinalIgnoreCase)) != -1)
|
||||
{
|
||||
dict[index] = pattern.Length;
|
||||
index += pattern.Length;
|
||||
}
|
||||
|
||||
void Highlight(string word)
|
||||
{
|
||||
if (word == "") return; // avoid errors, infinite loop
|
||||
return dict;
|
||||
}
|
||||
|
||||
void Highlight(string pattern)
|
||||
{
|
||||
RichTextBox richTextBox = txtbx_text;
|
||||
string text = richTextBox.Text;
|
||||
|
||||
@@ -57,17 +84,13 @@ namespace Metoder
|
||||
richTextBox.SelectionBackColor = richTextBox.BackColor;
|
||||
|
||||
int found = 0;
|
||||
int caret = richTextBox.SelectionStart; // set cursor
|
||||
int caret = richTextBox.SelectionStart; // set cursor to start
|
||||
|
||||
var words = GetWords();
|
||||
int index = 0;
|
||||
|
||||
while ((index = text.IndexOf(word, index, StringComparison.OrdinalIgnoreCase)) != -1)
|
||||
foreach (KeyValuePair<int, int> occurance in GetMatchesIndices(pattern, text))
|
||||
{
|
||||
richTextBox.Select(index, word.Length); // select word
|
||||
richTextBox.SelectionBackColor = Color.LightBlue;
|
||||
found++; // count found
|
||||
index = index + word.Length; // offset (ignore word next loop)
|
||||
richTextBox.Select(occurance.Key, occurance.Value); // select pattern occurance
|
||||
richTextBox.SelectionBackColor = Color.LightBlue; // highlight
|
||||
found++;
|
||||
}
|
||||
|
||||
// reset cursor
|
||||
@@ -77,17 +100,70 @@ namespace Metoder
|
||||
UpdateFindResult(found);
|
||||
}
|
||||
|
||||
void ReplaceFound(string pattern, string replacement)
|
||||
{
|
||||
string text = txtbx_text.Text;
|
||||
|
||||
Dictionary<int, int> matches = GetMatchesIndices(pattern, text);
|
||||
|
||||
if (matches.Count == 0) return;
|
||||
|
||||
string finalString = ""; // or string.Empty
|
||||
int currentIndex = 0;
|
||||
|
||||
foreach (KeyValuePair<int, int> occurrence in matches)
|
||||
{
|
||||
// add text before occurrence
|
||||
if (occurrence.Key > currentIndex)
|
||||
{
|
||||
finalString += text.Substring(currentIndex, occurrence.Key - currentIndex);
|
||||
}
|
||||
|
||||
// add replacement to result
|
||||
finalString += replacement;
|
||||
|
||||
// move past occurrence
|
||||
currentIndex = occurrence.Key + occurrence.Value;
|
||||
}
|
||||
|
||||
// add remaining text
|
||||
if (currentIndex < text.Length)
|
||||
{
|
||||
finalString += text.Substring(currentIndex);
|
||||
}
|
||||
|
||||
txtbx_text.Text = finalString;
|
||||
}
|
||||
|
||||
void SortWords()
|
||||
{
|
||||
List<string> words = GetSortedWords(GetWords());
|
||||
|
||||
string finalString = "";
|
||||
|
||||
// add each sorted word separated by spaces
|
||||
foreach (string word in words)
|
||||
{
|
||||
finalString += word;
|
||||
finalString += " ";
|
||||
}
|
||||
|
||||
txtbx_text.Text = finalString;
|
||||
}
|
||||
|
||||
List<string> GetSortedWords(List<string> words)
|
||||
{
|
||||
words.Sort();
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
List<string> GetWords() // get list of words separated by spaces
|
||||
{
|
||||
string text = txtbx_text.Text;
|
||||
string[] words = text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] words = text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries); // split by spaces
|
||||
|
||||
return words.ToList();
|
||||
}
|
||||
|
||||
private void btn_replace_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user