TextBox Find and Replace !

Joined
Jun 25, 2005
Messages
73
Reaction score
0
Hi,

What do you suggest to do if I want to do a search in TextBox ? I know that
using RichTextBox, we could have Find method to search for a specific word
or information but I was wonder if somebody knows some methods for Find and
Replace in TextBox.

Thanks
 
>Hi,

What do you suggest to do if I want to do a search in TextBox ? I know that
using RichTextBox, we could have Find method to search for a specific word
or information but I was wonder if somebody knows some methods for Find and
Replace in TextBox.

Thanks

textBox.Text = textBox.Text.Replace("foo", "bar");
 
Last edited by a moderator:
Thanks for your help but what I should to with search procedure ? should I
consider it as an array or link list ?
thanks again

"C# Learner" <[email protected]> wrote in message
news:[email protected]...

>
> >Hi,

> >
> >What do you suggest to do if I want to do a search in TextBox ? I know
that
> >using RichTextBox, we could have Find method to search for a specific
word
> >or information but I was wonder if somebody knows some methods for Find
and
> >Replace in TextBox.
> >
> >Thanks
>
> textBox.Text = textBox.Text.Replace("foo", "bar");
 
Last edited by a moderator:
>Thanks for your help but what I should to with search procedure ? should I
consider it as an array or link list ?
thanks again

Are you asking how to search for text within a text box?

If so you can just say:

int pos = textBox.Text.IndexOf("foo");

If not, could you please give details of exactly what you're trying to
accomplish?

:)
 
Last edited by a moderator:
I appreciate it. Really thanks

"C# Learner" <[email protected]> wrote in message
news:[email protected]...

> >Thanks for your help but what I should to with search procedure ? should
I
> >consider it as an array or link list ?
> >thanks again
>
> Are you asking how to search for text within a text box?
>
> If so you can just say:
>
> int pos = textBox.Text.IndexOf("foo");
>
> If not, could you please give details of exactly what you're trying to
> accomplish?
>
> :)
 
Last edited by a moderator:
Hi again,
how can I move the cursor to a specific line in textbox ?




"C# Learner" <[email protected]> wrote in message
news:[email protected]...
>
> >Thanks for your help but what I should to with search procedure ? should

I
> >consider it as an array or link list ?
> >thanks again
>
> Are you asking how to search for text within a text box?
>
> If so you can just say:
>
> int pos = textBox.Text.IndexOf("foo");
>
> If not, could you please give details of exactly what you're trying to
> accomplish?
>
> :)
 
Last edited by a moderator:
>Hi again,
how can I move the cursor to a specific line in textbox ?

Off the top of my head:

private void button_Click(object sender, System.EventArgs e)
{
SetCursorToStartOfLine(2);
textBox.Select();
}

private void SetCursorToStartOfLine(int lineIndex)
{
int charIndex = 0;

for (int i = 0;
i < textBox1.Lines.Length && i < lineIndex - 1;
++i) {
charIndex += textBox.Lines.Length;
}

textBox.SelectionStart = charIndex +
Environment.NewLine.Length;
}
 
Last edited by a moderator:
Hi, there are some question I have for you since I"m new to C# and still not
familiar with some issues. However, please let me know why you used:
textBox.SelectionStart = charIndex +Environment.NewLine.Length;

What Environement.NewLine is ?

also why you put : charIndex += textBox.Lines.Length; ?

my textbox has 10 lines but this makes it 14 and doesn't return a proper
line number after exiting the end of loop the value is 43.

any suggestions ?
thanks in advance





"C# Learner" <[email protected]> wrote in message
news:[email protected]...
> C# Learner <[email protected]> wrote:
>
>
> > i < textBox1.Lines.Length && i < lineIndex - 1;

>
> that should be:
> textBox.Lines.Length
 
Hi, there are some question I have for you since I"m new to C# and still not
familiar with some issues. However, please let me know why you used:
textBox.SelectionStart = charIndex +Environment.NewLine.Length;

What Environement.NewLine is ?

also why you put : charIndex += textBox.Lines.Length; ?

my textbox has 10 lines but this makes it 14 and doesn't return a proper
line number after exiting the end of loop the value is 43.

any suggestions ?
thanks in advance
"C# Learner" <[email protected]> wrote in message
news:[email protected]...
> C# Learner <[email protected]> wrote:
>
>
> > i < textBox1.Lines.Length && i < lineIndex - 1;

>
> that should be:
> textBox.Lines.Length
 
>
Hi, there are some question I have for you since I"m new to C# and still not
familiar with some issues. However, please let me know why you used:
textBox.SelectionStart = charIndex +Environment.NewLine.Length;

What Environement.NewLine is ?

Here:
http://msdn.microsoft.com/library/d...l/frlrfsystemenvironmentclassnewlinetopic.asp

It's a string - "\r\n" - which is what Windows uses to specify a "new line".
also why you put : charIndex += textBox.Lines.Length; ?

Here, charIndex is being incremented by the length of the current line.
my textbox has 10 lines but this makes it 14 and doesn't return a proper
line number after exiting the end of loop the value is 43.

Sorry! I made an error -- I must've been half asleep when I wrote that.

Here's how it should look:

private void SetCursorToStartOfLine(int lineIndex)
{
int charIndex = 0;

for (int i = 0; i < textBox.Lines.Length && i < lineIndex - 1; ++i) {
charIndex += textBox.Lines.Length + Environment.NewLine.Length;
}

textBox.SelectionStart = charIndex;
}
any suggestions ?
thanks in advance
 
Last edited by a moderator:
Thanks for help but doesn't work it generates number much bigger than
textbox.lines. I have defined 10 lines for it but the code calculates number
of characters in every line so it's not the logic I'm looking for.

regards

>
> Hi, there are some question I have for you since I"m new to C# and still

not
> familiar with some issues. However, please let me know why you used:
> textBox.SelectionStart = charIndex +Environment.NewLine.Length;
>
> What Environement.NewLine is ?
>
> also why you put : charIndex += textBox.Lines.Length; ?
>
> my textbox has 10 lines but this makes it 14 and doesn't return a proper
> line number after exiting the end of loop the value is 43.
>
> any suggestions ?
> thanks in advance
> "C# Learner" <[email protected]> wrote in message
> news:[email protected]...
> > C# Learner <[email protected]> wrote:

> >
> >
> > > i < textBox1.Lines.Length && i < lineIndex - 1;
> >
> > that should be:
> > textBox.Lines.Length
>
>
 
Last edited by a moderator:
>Thanks for help but doesn't work it generates number much bigger than
textbox.lines. I have defined 10 lines for it but the code calculates number
of characters in every line so it's not the logic I'm looking for.

regards

That's the whole point. It gets the *character index* of the start of
a line.

This character index is then used in "textBox.SelectionStart =
charIndex".

You can't select a line with a *line index*, as far as I know.
 
Last edited by a moderator:
Thanks for keeping in touch. The thing is when we call :
charIndex += textBox.Lines.Length + Environment.NewLine.Length;

It does a sum operatioin on textBox.lines + 2.
let's say my textBox has 10 lines and every line has some characters or
nothing ... now each iteration the value
of charIndex is being increased. Here I have text box with 10 lines; after
running the loop charIndex isn't even equal to 10 (max line number in
textbox)
only after first iteration the value of charIndex is 15 which is greater
than 10.
then how can I send the cursor to 39 (value of charIdnex after exiting the
loop)?
Or there is something that I can't get but since I've used the code you
pointed.
Again I apprecaite your concern.






"C# Learner" <[email protected]> wrote in message
news:[email protected]...


>
> >Thanks for help but doesn't work it generates number much bigger than

> >textbox.lines. I have defined 10 lines for it but the code calculates
number
> >of characters in every line so it's not the logic I'm looking for.
> >
> >regards
>
> That's the whole point. It gets the *character index* of the start of
> a line.
>
> This character index is then used in "textBox.SelectionStart =
> charIndex".
>
> You can't select a line with a *line index*, as far as I know.
 
Last edited by a moderator:
>
Thanks for keeping in touch. The thing is when we call :
charIndex += textBox.Lines.Length + Environment.NewLine.Length;
>
>It does a sum operatioin on textBox.lines + 2.
>let's say my textBox has 10 lines and every line has some characters or
>nothing ... now each iteration the value
>of charIndex is being increased. Here I have text box with 10 lines; after
>running the loop charIndex isn't even equal to 10 (max line number in
>textbox)
>only after first iteration the value of charIndex is 15 which is greater
>than 10.
>then how can I send the cursor to 39 (value of charIdnex after exiting the
>loop)?
>Or there is something that I can't get but since I've used the code you
>pointed.
>Again I apprecaite your concern.

Imagine you have a text box that looks like this:

Line 1.
Line 2.
Line 3.

Now, the text box is really holding this: "Line 1.\r\nLine 2.\r\n.Line
3\r\n."

To get to the start of line three, we must set the cursor position to:
Length(Line 1) + Length("\r\n") + Length(Line2) + Length("\r\n").

This is what is achieved in the loop.

I'm not quite sure what part you don't understand...
 
Last edited by a moderator:
Yes, you're right. The thing is I hadn't used txtBody.Focus(); method before
textBox.SelectionStart = digit
so wasn't working. Not works just fine.
Thanks for your help and your patience

regards


"C# Learner" <[email protected]> wrote in message
news:[email protected]...

>
> >

> >Thanks for keeping in touch. The thing is when we call :
> > charIndex += textBox.Lines.Length + Environment.NewLine.Length;
> >
> >It does a sum operatioin on textBox.lines + 2.
> >let's say my textBox has 10 lines and every line has some characters or
> >nothing ... now each iteration the value
> >of charIndex is being increased. Here I have text box with 10 lines;
after
> >running the loop charIndex isn't even equal to 10 (max line number in
> >textbox)
> >only after first iteration the value of charIndex is 15 which is greater
> >than 10.
> >then how can I send the cursor to 39 (value of charIdnex after exiting
the
> >loop)?
> >Or there is something that I can't get but since I've used the code you
> >pointed.
> >Again I apprecaite your concern.
>
> Imagine you have a text box that looks like this:
>
> Line 1.
> Line 2.
> Line 3.
>
> Now, the text box is really holding this: "Line 1.\r\nLine 2.\r\n.Line
> 3\r\n."
>
> To get to the start of line three, we must set the cursor position to:
> Length(Line 1) + Length("\r\n") + Length(Line2) + Length("\r\n").
>
> This is what is achieved in the loop.
>
> I'm not quite sure what part you don't understand...
 
Last edited by a moderator:
Back
Top