Hi i am new to this so please forgive my naivety.
i have been given a project to create a notepad using windows form application, which i have so far managed, its got all the basic functionalities like cut,copy,paste, load file, save file etc.
However one of the requirements is for the notepad to have a search facility, so you can find any line of text within the document show that line of text and the line number which it is situated.
so my question is, is there a tool in the toolbox that i should be using to be able to do this, or it just a matter of having a search button and writing the code directly to the form, so when the search button is clicked a new form is displayed with a text box search bar, and then test the user input to see if it exists within the text file?
any tips or even a point in the right direction would be very much appreciated.
Thank you
Guest UserPosted Jul 22, 2014, 1:19 PM
Got it!
Change line of code like this in FindMyText function
twitter @sumitjolly
Guest UserPosted Jul 24, 2014, 11:18 PM
thnx for accepting my answer!
to remove selection you can use following code:
indexToText=0;
text="";
thnx
@sumitjolly
lee croydonPosted Jul 23, 2014, 2:59 PM
Thank you for your help, i did not use your code in the end due to the replace text box but it did work and it helped me in finding a solution.....
i ended up using the code from my last post, the problem with it was this line
if (searchStart > searchEnd || searchEnd == -1)
it should have been
if (SearchEnd > searchStart || searchStart == -1)
now the search box works fine, i just need to figure out how to remove the highlighted word or sentence when the user clicks in the Rich Text Box and display the line number of the highlighted text.
i did use a back color grey instead of changing the text color in the end as well.
once again thank you for your help :)
Guest UserPosted Jul 23, 2014, 2:20 PM
Guest UserPosted Jul 23, 2014, 4:01 AM
Guest UserPosted Jul 23, 2014, 12:21 AM
I see code deviation in previous working code for startIndex and in this new implementation
New one:
Old One in post:
if you compare you're passing extra param to Find function & also you're passing different value as RichTextBoxFinds.None
Look into these and I'm sure it'll solve
twitter @sumitjolly
lee croydonPosted Jul 22, 2014, 3:41 PM
Thank you for your reply, the does work however i don't really want the second textBox (replaceText).
i have come up with this from bits i have found online, however it is returning the startIndex as -1 so it is not invoking the if statement. i am a little puzzled as to why.
i have attached the entire code have you any theories on why it is coming back -1 instead of a plus number?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NotePadProject
{
public partial class Form1 : Form
{
int start = 0;
int indexOfSearchText = 0;
public Form1()
{
InitializeComponent();
}
private void mnuExit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Really Exit", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Close();
}
}
private void mnuCut_Click(object sender, EventArgs e)
{
txtBox1.Cut();
}
private void mnuUndo_Click(object sender, EventArgs e)
{
if (txtBox1.CanUndo == true)
{
txtBox1.Undo();
txtBox1.ClearUndo();
}
}
private void mnuCopy_Click(object sender, EventArgs e)
{
if (txtBox1.SelectionLength > 0)
{
txtBox1.Copy();
}
}
private void mnuPaste_Click(object sender, EventArgs e)
{
txtBox1.Paste();
Clipboard.Clear();
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
txtBox1.SelectAll();
}
private void mnuOpen_Click(object sender, EventArgs e)
{
string chosen_File = "";
openFD.InitialDirectory = "C:";
openFD.Title = "Open a Text File";
openFD.FileName = "";
openFD.Filter = "Text Files (.txt)|*.txt|Word Documents (.doc)|*.doc|ODF Text Document (.odt)|*.odt|All Files (*.*)|*.*";
if (openFD.ShowDialog() != DialogResult.Cancel)
{
chosen_File = openFD.FileName;
txtBox1.LoadFile(chosen_File, RichTextBoxStreamType.PlainText);
}
}
private void mnuSave_Click(object sender, EventArgs e)
{
string saved_File = "";
saveFD.InitialDirectory = "C:";
saveFD.Title = "Save a Text File";
saveFD.FileName = "";
saveFD.Filter = "Text Files (.txt)|*.txt|Word Documents (.doc)|*.doc|ODF Text Document (.odt)|*.odt|All Files (*.*)|*.*";
if (saveFD.ShowDialog() != DialogResult.Cancel)
{
saved_File = saveFD.FileName;
txtBox1.SaveFile(saved_File, RichTextBoxStreamType.PlainText);
}
}
private void mnuNew_Click(object sender, EventArgs e)
{
txtBox1.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
int startIndex = 0;
if (searchBox.Text.Length > 0)
{
startIndex = FindMyText(searchBox.Text.Trim(), start, txtBox1.Text.Length);
}
if (startIndex >= 0)
{
txtBox1.SelectionColor = Color.Red;
int endIndex = searchBox.Text.Length;
txtBox1.Select(startIndex, endIndex);
start = startIndex + endIndex;
}
}
public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
{
if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
{
txtBox1.Undo();
}
int returnValue = -1;
if (searchStart >= 0 && indexOfSearchText >= 0)
{
if (searchStart > searchEnd || searchEnd == -1)
{
indexOfSearchText = txtBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
if (indexOfSearchText != -1)
{
returnValue = indexOfSearchText;
}
}
}
return returnValue;
}
private void searchbox_TextChanged(object sender, EventArgs e)
{
start = 0;
indexOfSearchText = 0;
}
}
}
again any input will be much appreciated, and thank you for your posts so far they have been more than helpful.
Regards Lee
lee croydonPosted Jul 22, 2014, 10:21 AM
the code you added does work but it does not do what i had in mind, i am trying to highlight the text i am looking for in the RichTextBox......
this is the code i have now which works in a sense that it returns true but it doesn't highlight the text in the RichTextBox (txtBox1)...
public bool FindMyText(string text)
{
bool returnValue = false;
if (text.Length > 0)
{
int indexToText = txtBox1.Find(text, RichTextBoxFinds.MatchCase);
if (indexToText >= 0)
{
returnValue = true;
}
else
{
MessageBox.Show("No results for current search");
}
}
return returnValue;
}
private void searchbox_TextChanged(object sender, EventArgs e)
{
string search = searchbox.Text;
FindMyText(search);
}
}
}
as you can see this is not far off what i had to start with other than i no longer have a button for the search just a search box.
However like i said above it does not highlight the text found in the RichTextBox, anyone have any ideas why?
Guest UserPosted Jul 22, 2014, 8:16 AM
take one more text box and accept string that you want to replace. Now, pass it as param and i'm using it in FindMyText to replace. However you can use separate button to replace it.
lee croydonPosted Jul 22, 2014, 3:52 AM
as for using a RichTextBox, i am currently using one in the application.
public bool FindMyText(string text)
{
bool returnValue = false;
if (text.Length > 0)
{
int indexToText = txtBox1.Find(text, RichTextBoxFinds.MatchCase);
if (indexToText >= 0)
{
returnValue = true;
}
else
{
MessageBox.Show("No results for current search");
}
}
return returnValue;
}
private void button1_Click(object sender, EventArgs e)
{
string foundText = searchbox.Text;
if (searchbox.TextLength > 0)
{
FindMyText(foundText);
if (true)
{
txtBox1.Find(Text, RichTextBoxFinds.MatchCase);
}
}
}
any advise would be appreciated
Regards Lee
Guest UserPosted Jul 21, 2014, 1:30 PM
OR
http://www.textcontrol.com/en_US/products/dotnet/features/search-replace-client/
twitter @sumitjolly