Click here to Skip to main content
15,884,099 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
Member 1153148717-Mar-15 0:24
Member 1153148717-Mar-15 0:24 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
Freak3017-Mar-15 2:03
Freak3017-Mar-15 2:03 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
OriginalGriff17-Mar-15 2:36
mveOriginalGriff17-Mar-15 2:36 
AnswerRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
BillWoodruff17-Mar-15 4:56
professionalBillWoodruff17-Mar-15 4:56 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
Member 1153148717-Mar-15 5:24
Member 1153148717-Mar-15 5:24 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
Member 1153148717-Mar-15 5:38
Member 1153148717-Mar-15 5:38 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
BillWoodruff17-Mar-15 7:46
professionalBillWoodruff17-Mar-15 7:46 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
BillWoodruff17-Mar-15 22:11
professionalBillWoodruff17-Mar-15 22:11 
I think there's a simple way to keep the search form open, and re-use it to find the next instance of a string. I would implement this by injecting a simple Action Delegate instance into the instance of the SearchForm at run-time (this is just a shortcut for creating a custom event; see the note at the end, here, for why I prefer this syntax):
C#
public partial class SearchForm : Form
{
    public SearchForm()
    {
        InitializeComponent();
    }

    public Action<string, bool> FindAction;

    private string LastSearchText;

    private string TextToFind;

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Hide();
    }

    private void btnFind_Click(object sender, EventArgs e)
    {
        TextToFind = textBox1.Text;

        if (String.IsNullOrWhiteSpace(TextToFind))
        {
            MessageBox.Show("Searching on white space makes not sense.");
            return;

        }

        // invoke the "callback" here
        FindAction(TextToFind, LastSearchText == TextToFind);

        LastSearchText = TextToFind;
    }
}
When 'FindAction is invoked, it passes both the text the user wishes to search for, and a boolean flag that indicates whether the search is a new search, or a repeat of the previous search.

In the body of the Main Form:
C#
SearchForm searchForm = new SearchForm();

private void Form1_Load(object sender, EventArgs e)
{
    searchForm.Visible = false;
    searchForm.Owner = this;

    // inject the code for the Action delegate
    searchForm.FindAction = FindAction;
}

private void FindAction(string textToFind, bool isSameSearch)
{
    // start search from end of last search, or start from beginning of text ?
    int searchStartIndex = (isSameSearch)
        ? textBox1.SelectionStart + textBox1.SelectionLength
        : 0;

    int ndx = textBox1.Text.IndexOf(textToFind, searchStartIndex);

    if (ndx == -1) return;
    {
        textBox1.SelectionStart = ndx;
        textBox1.SelectionLength = textToFind.Length;
    }
}

private void ShowSearchForm_Click(object sender, EventArgs e)
{
    searchForm.Show();
}
Notes:

1. You may wish to change the logic used here so that the search always begins at the current insertion point in the text ... where the cursor is ... rather than starting from the beginning ?

2. Why I prefer using an Action (or Func) delegate instance for "callbacks:"

a. while both Action and Func Types are "full-fledged" Delegate Types which can have multiple subscribers, with subscribers added and removed using the += and -= syntax:

b. when my intent is to use a delegate only as a callback in one specific "role" ... i.e., not to make the delegate available in a wider scope for use/subscription ... I think it more clearly expresses that intent to use Action/Func, and directly assign the callback function using = rather than +=.

Note that other folks may have other (strong) opinions about such a usage pattern with Action and Func.
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"

GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
Member 1153148717-Mar-15 23:49
Member 1153148717-Mar-15 23:49 
GeneralRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
BillWoodruff18-Mar-15 17:01
professionalBillWoodruff18-Mar-15 17:01 
Questionhow to send any file from server to client in c# Pin
Member 1149066016-Mar-15 21:32
Member 1149066016-Mar-15 21:32 
AnswerRe: how to send any file from server to client in c# Pin
Eddy Vluggen16-Mar-15 23:27
professionalEddy Vluggen16-Mar-15 23:27 
AnswerRe: how to send any file from server to client in c# Pin
F-ES Sitecore16-Mar-15 23:40
professionalF-ES Sitecore16-Mar-15 23:40 
QuestionProblem parsing rss feed Pin
Member 1022623016-Mar-15 19:25
Member 1022623016-Mar-15 19:25 
AnswerRe: Problem parsing rss feed Pin
Pete O'Hanlon16-Mar-15 22:19
mvePete O'Hanlon16-Mar-15 22:19 
QuestionHow to call protected override void OnPaint(PaintEventArgs e) in another method in C# Pin
Member 1068390216-Mar-15 8:47
Member 1068390216-Mar-15 8:47 
AnswerRe: How to call protected override void OnPaint(PaintEventArgs e) in another method in C# Pin
Eddy Vluggen16-Mar-15 9:08
professionalEddy Vluggen16-Mar-15 9:08 
GeneralRe: How to call protected override void OnPaint(PaintEventArgs e) in another method in C# Pin
Member 1068390216-Mar-15 9:48
Member 1068390216-Mar-15 9:48 
Questionask about windorms and containers Pin
fsdsc216-Mar-15 4:56
fsdsc216-Mar-15 4:56 
AnswerRe: ask about windorms and containers Pin
OriginalGriff16-Mar-15 5:04
mveOriginalGriff16-Mar-15 5:04 
GeneralRe: ask about windorms and containers Pin
fsdsc216-Mar-15 11:21
fsdsc216-Mar-15 11:21 
GeneralRe: ask about windorms and containers Pin
fsdsc217-Mar-15 9:12
fsdsc217-Mar-15 9:12 
Questioncreate PPPoe connection in c# Pin
KARFER16-Mar-15 4:38
KARFER16-Mar-15 4:38 
AnswerRe: create PPPoe connection in c# Pin
OriginalGriff16-Mar-15 5:05
mveOriginalGriff16-Mar-15 5:05 
QuestionDoubt about netinventory Pin
Raghuraman.tm1916-Mar-15 1:32
professionalRaghuraman.tm1916-Mar-15 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.