469,586 Members | 2,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,586 developers. It's quick & easy.

TextBox Find and Replace !

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
Nov 15 '05 #1
16 37555
"Ramsin Savra" <rs****@otxresearch.com> wrote:
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");
Nov 15 '05 #2
"Ramsin Savra" <rs****@otxresearch.com> wrote:
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");
Nov 15 '05 #3
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" <cs****@learner.here> wrote in message
news:sk********************************@4ax.com...
"Ramsin Savra" <rs****@otxresearch.com> wrote:
Hi,

What do you suggest to do if I want to do a search in TextBox ? I know thatusing RichTextBox, we could have Find method to search for a specific wordor information but I was wonder if somebody knows some methods for Find andReplace in TextBox.

Thanks


textBox.Text = textBox.Text.Replace("foo", "bar");

Nov 15 '05 #4
"Ramsin Savra" <rs****@otxresearch.com> wrote:
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?

:-)
Nov 15 '05 #5
I appreciate it. Really thanks

"C# Learner" <cs****@learner.here> wrote in message
news:1g********************************@4ax.com...
"Ramsin Savra" <rs****@otxresearch.com> wrote:
Thanks for your help but what I should to with search procedure ? should Iconsider 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?

:-)

Nov 15 '05 #6
Hi again,
how can I move the cursor to a specific line in textbox ?


"C# Learner" <cs****@learner.here> wrote in message
news:1g********************************@4ax.com...
"Ramsin Savra" <rs****@otxresearch.com> wrote:
Thanks for your help but what I should to with search procedure ? should Iconsider 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?

:-)

Nov 15 '05 #7
"Ramsin Savra" <rs****@otxresearch.com> wrote:
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[i].Length;
}

textBox.SelectionStart = charIndex +
Environment.NewLine.Length;
}
Nov 15 '05 #8
C# Learner <cs****@learner.here> wrote:

i < textBox1.Lines.Length && i < lineIndex - 1;


that should be:
textBox.Lines.Length
Nov 15 '05 #9

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[i].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" <cs****@learner.here> wrote in message
news:9l********************************@4ax.com...
C# Learner <cs****@learner.here> wrote:

i < textBox1.Lines.Length && i < lineIndex - 1;


that should be:
textBox.Lines.Length

Nov 15 '05 #10

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[i].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" <cs****@learner.here> wrote in message
news:9l********************************@4ax.com...
C# Learner <cs****@learner.here> wrote:

i < textBox1.Lines.Length && i < lineIndex - 1;


that should be:
textBox.Lines.Length

Nov 15 '05 #11
"Ramsin Savra" <rs****@otxresearch.com> wrote:

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/de...wlinetopic.asp

It's a string - "\r\n" - which is what Windows uses to specify a "new line".
also why you put : charIndex += textBox.Lines[i].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[i].Length + Environment.NewLine.Length;
}

textBox.SelectionStart = charIndex;
}
any suggestions ?
thanks in advance


Nov 15 '05 #12
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
"Ramsin Savra" <rs****@otxresearch.com> wrote in message
news:e8**************@TK2MSFTNGP11.phx.gbl...

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[i].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" <cs****@learner.here> wrote in message
news:9l********************************@4ax.com...
C# Learner <cs****@learner.here> wrote:

i < textBox1.Lines.Length && i < lineIndex - 1;


that should be:
textBox.Lines.Length


Nov 15 '05 #13
"Ramsin Savra" <rs****@otxresearch.com> wrote:
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.
Nov 15 '05 #14

Thanks for keeping in touch. The thing is when we call :
charIndex += textBox.Lines[i].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" <cs****@learner.here> wrote in message
news:a4********************************@4ax.com...
"Ramsin Savra" <rs****@otxresearch.com> wrote:
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 numberof 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.



Nov 15 '05 #15
"Ramsin Savra" <rs****@otxresearch.com> wrote:

Thanks for keeping in touch. The thing is when we call :
charIndex += textBox.Lines[i].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...
Nov 15 '05 #16
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" <cs****@learner.here> wrote in message
news:gj********************************@4ax.com...
"Ramsin Savra" <rs****@otxresearch.com> wrote:

Thanks for keeping in touch. The thing is when we call :
charIndex += textBox.Lines[i].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; afterrunning 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 theloop)?
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...

Nov 15 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by ddddd | last post: by
2 posts views Thread by :\\\\derian | last post: by
2 posts views Thread by scorpion53061 | last post: by
reply views Thread by vipal.keshwala | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.