Click here to Skip to main content
15,891,184 members
Home / Discussions / C#
   

C#

 
GeneralRe: Question regarding C# Pin
OriginalGriff17-Mar-15 7:40
mveOriginalGriff17-Mar-15 7:40 
GeneralRe: Question regarding C# Pin
Rob Philpott17-Mar-15 23:09
Rob Philpott17-Mar-15 23:09 
AnswerRe: Question regarding C# Pin
Pete O'Hanlon17-Mar-15 2:39
mvePete O'Hanlon17-Mar-15 2:39 
AnswerRe: Question regarding C# Pin
Abdulnazark17-Mar-15 7:46
Abdulnazark17-Mar-15 7:46 
GeneralRe: Question regarding C# Pin
Pete O'Hanlon17-Mar-15 8:31
mvePete O'Hanlon17-Mar-15 8:31 
GeneralRe: Question regarding C# Pin
Abdulnazark17-Mar-15 20:30
Abdulnazark17-Mar-15 20:30 
GeneralRe: Question regarding C# Pin
Pete O'Hanlon17-Mar-15 21:34
mvePete O'Hanlon17-Mar-15 21:34 
GeneralMessage Closed Pin
17-Mar-15 22:24
Abdulnazark17-Mar-15 22:24 
GeneralRe: Question regarding C# Pin
Pete O'Hanlon18-Mar-15 0:03
mvePete O'Hanlon18-Mar-15 0:03 
GeneralRe: Question regarding C# Pin
Abdulnazark18-Mar-15 2:16
Abdulnazark18-Mar-15 2:16 
GeneralRe: Question regarding C# Pin
Richard Deeming18-Mar-15 3:00
mveRichard Deeming18-Mar-15 3:00 
GeneralRe: Question regarding C# Pin
Abdulnazark18-Mar-15 3:25
Abdulnazark18-Mar-15 3:25 
GeneralRe: Question regarding C# Pin
Abdulnazark17-Mar-15 23:04
Abdulnazark17-Mar-15 23:04 
QuestionRLDC Report error : An error occurred during report processing. Pin
jasonalien17-Mar-15 0:13
jasonalien17-Mar-15 0:13 
QuestionC# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
Member 1153148716-Mar-15 23:04
Member 1153148716-Mar-15 23:04 
AnswerRe: C# Notepad Clone using a Textbox (not RichTextBox) Find / Find Next Function help? Pin
OriginalGriff17-Mar-15 0:01
mveOriginalGriff17-Mar-15 0:01 
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 

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.