Click here to Skip to main content
15,907,910 members
Home / Discussions / C#
   

C#

 
QuestionQuestion regarding C# Pin
Member 1153209517-Mar-15 2:09
Member 1153209517-Mar-15 2:09 
AnswerRe: Question regarding C# Pin
OriginalGriff17-Mar-15 2:38
mveOriginalGriff17-Mar-15 2:38 
GeneralRe: Question regarding C# Pin
Rob Philpott17-Mar-15 7:20
Rob Philpott17-Mar-15 7:20 
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 
You've received some good advice on this thread; I'd like to point you to a slightly different type of solution. In looking at communication between object-instances, I try to think in terms of what I call "POEM" which is my half-serious anagram for expressing the idea of "Principle of Expose the Minimum."

In your scenario, I see the only real "need to know" is: the Form with the Text you want to search and highlight (the Main Form, I assume) needs to know what the user entered as search text in your second Form.

I would choose what I see as the simple strategy of having one Property on the SearchForm with a private 'set and public 'get that exposes the text the end-user entered, and I'd show the SearchForm modally using 'ShowDialog, "doing the right thing" when the end-user clicked 'Cancel, or 'Search Buttons:
C#
public string TextToFind { private set; get;  }

private void btnCancel_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.Cancel;
    this.Close();
}

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;
    }

    this.DialogResult = DialogResult.OK;
    this.Close();
}
To use this, I would declare one instance of the SearchForm in the Main Form, and re-use it like this:
C#
private void Search_Click(object sender, EventArgs e)
{
    if (SearchForm1.ShowDialog() == DialogResult.OK)
    {
        string searchText = SearchForm1.TextToFind;

        // note: case sensitive search here
        int ndx = TheTextBox.Text.IndexOf(searchText);

        if (ndx == -1) return; // nothing found

        TheTextBox.SelectionStart = ndx;
        TheTextBox.SelectionLength = searchText.Length;
    }
}
From my point-of-view this strategy keeps the search Form "as dumb a robot as possible," and that is a good thing Smile | :)
«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 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 

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.