Connecting Tech Pros Worldwide Forums | Help | Site Map

TextBox Find and Replace !

Ramsin Savra
Guest
 
Posts: n/a
#1: Nov 15 '05
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


C# Learner
Guest
 
Posts: n/a
#2: Nov 15 '05

re: TextBox Find and Replace !


"Ramsin Savra" <rsavra@otxresearch.com> wrote:
[color=blue]
>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[/color]

textBox.Text = textBox.Text.Replace("foo", "bar");
C# Learner
Guest
 
Posts: n/a
#3: Nov 15 '05

re: TextBox Find and Replace !


"Ramsin Savra" <rsavra@otxresearch.com> wrote:
[color=blue]
>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[/color]

textBox.Text = textBox.Text.Replace("foo", "bar");
Ramsin Savra
Guest
 
Posts: n/a
#4: Nov 15 '05

re: TextBox Find and Replace !


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" <csharp@learner.here> wrote in message
news:skql109ll6a9ou48b7dosvtubbn9v62squ@4ax.com...[color=blue]
> "Ramsin Savra" <rsavra@otxresearch.com> wrote:
>[color=green]
> >Hi,
> >
> >What do you suggest to do if I want to do a search in TextBox ? I know[/color][/color]
that[color=blue][color=green]
> >using RichTextBox, we could have Find method to search for a specific[/color][/color]
word[color=blue][color=green]
> >or information but I was wonder if somebody knows some methods for Find[/color][/color]
and[color=blue][color=green]
> >Replace in TextBox.
> >
> >Thanks[/color]
>
> textBox.Text = textBox.Text.Replace("foo", "bar");[/color]


C# Learner
Guest
 
Posts: n/a
#5: Nov 15 '05

re: TextBox Find and Replace !


"Ramsin Savra" <rsavra@otxresearch.com> wrote:
[color=blue]
>Thanks for your help but what I should to with search procedure ? should I
>consider it as an array or link list ?
>thanks again[/color]

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?

:-)
Ramsin Savra
Guest
 
Posts: n/a
#6: Nov 15 '05

re: TextBox Find and Replace !


I appreciate it. Really thanks

"C# Learner" <csharp@learner.here> wrote in message
news:1g5m101g3bisa7bi8lt7ggih0rqfei2fqo@4ax.com...[color=blue]
> "Ramsin Savra" <rsavra@otxresearch.com> wrote:
>[color=green]
> >Thanks for your help but what I should to with search procedure ? should[/color][/color]
I[color=blue][color=green]
> >consider it as an array or link list ?
> >thanks again[/color]
>
> 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?
>
> :-)[/color]


Ramsin Savra
Guest
 
Posts: n/a
#7: Nov 15 '05

re: TextBox Find and Replace !


Hi again,
how can I move the cursor to a specific line in textbox ?




"C# Learner" <csharp@learner.here> wrote in message
news:1g5m101g3bisa7bi8lt7ggih0rqfei2fqo@4ax.com...[color=blue]
> "Ramsin Savra" <rsavra@otxresearch.com> wrote:
>[color=green]
> >Thanks for your help but what I should to with search procedure ? should[/color][/color]
I[color=blue][color=green]
> >consider it as an array or link list ?
> >thanks again[/color]
>
> 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?
>
> :-)[/color]


C# Learner
Guest
 
Posts: n/a
#8: Nov 15 '05

re: TextBox Find and Replace !


"Ramsin Savra" <rsavra@otxresearch.com> wrote:
[color=blue]
>Hi again,
>how can I move the cursor to a specific line in textbox ?[/color]

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;
}
C# Learner
Guest
 
Posts: n/a
#9: Nov 15 '05

re: TextBox Find and Replace !


C# Learner <csharp@learner.here> wrote:

[color=blue]
> i < textBox1.Lines.Length && i < lineIndex - 1;[/color]

that should be:
textBox.Lines.Length
Ramsin Savra
Guest
 
Posts: n/a
#10: Nov 15 '05

re: TextBox Find and Replace !



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" <csharp@learner.here> wrote in message
news:9lcm10hivhjkkfkcj3ogn1ks64shq4nsfn@4ax.com...[color=blue]
> C# Learner <csharp@learner.here> wrote:
>
>[color=green]
> > i < textBox1.Lines.Length && i < lineIndex - 1;[/color]
>
> that should be:
> textBox.Lines.Length[/color]


Ramsin Savra
Guest
 
Posts: n/a
#11: Nov 15 '05

re: TextBox Find and Replace !



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" <csharp@learner.here> wrote in message
news:9lcm10hivhjkkfkcj3ogn1ks64shq4nsfn@4ax.com...[color=blue]
> C# Learner <csharp@learner.here> wrote:
>
>[color=green]
> > i < textBox1.Lines.Length && i < lineIndex - 1;[/color]
>
> that should be:
> textBox.Lines.Length[/color]


C# Learner
Guest
 
Posts: n/a
#12: Nov 15 '05

re: TextBox Find and Replace !


"Ramsin Savra" <rsavra@otxresearch.com> wrote:
[color=blue]
>
>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 ?[/color]

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".
[color=blue]
>also why you put : charIndex += textBox.Lines[i].Length; ?[/color]

Here, charIndex is being incremented by the length of the current line.
[color=blue]
>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.[/color]

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;
}
[color=blue]
>any suggestions ?
>thanks in advance[/color]

Ramsin Savra
Guest
 
Posts: n/a
#13: Nov 15 '05

re: TextBox Find and Replace !


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" <rsavra@otxresearch.com> wrote in message
news:e8kLNAc6DHA.2540@TK2MSFTNGP11.phx.gbl...[color=blue]
>
> Hi, there are some question I have for you since I"m new to C# and still[/color]
not[color=blue]
> 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" <csharp@learner.here> wrote in message
> news:9lcm10hivhjkkfkcj3ogn1ks64shq4nsfn@4ax.com...[color=green]
> > C# Learner <csharp@learner.here> wrote:
> >
> >[color=darkred]
> > > i < textBox1.Lines.Length && i < lineIndex - 1;[/color]
> >
> > that should be:
> > textBox.Lines.Length[/color]
>
>[/color]


C# Learner
Guest
 
Posts: n/a
#14: Nov 15 '05

re: TextBox Find and Replace !


"Ramsin Savra" <rsavra@otxresearch.com> wrote:
[color=blue]
>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[/color]

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.
Ramsin Savra
Guest
 
Posts: n/a
#15: Nov 15 '05

re: TextBox Find and Replace !



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" <csharp@learner.here> wrote in message
news:a4fu10h1gik8mslc5k3golo6h9esqnomrj@4ax.com...[color=blue]
> "Ramsin Savra" <rsavra@otxresearch.com> wrote:
>[color=green]
> >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[/color][/color]
number[color=blue][color=green]
> >of characters in every line so it's not the logic I'm looking for.
> >
> >regards[/color]
>
> 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.[/color]




C# Learner
Guest
 
Posts: n/a
#16: Nov 15 '05

re: TextBox Find and Replace !


"Ramsin Savra" <rsavra@otxresearch.com> wrote:
[color=blue]
>
>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.[/color]

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...
Ramsin Savra
Guest
 
Posts: n/a
#17: Nov 15 '05

re: TextBox Find and Replace !


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" <csharp@learner.here> wrote in message
news:gj8120tifb1dmosiov1q49tptldgu8t5qd@4ax.com...[color=blue]
> "Ramsin Savra" <rsavra@otxresearch.com> wrote:
>[color=green]
> >
> >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;[/color][/color]
after[color=blue][color=green]
> >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[/color][/color]
the[color=blue][color=green]
> >loop)?
> >Or there is something that I can't get but since I've used the code you
> >pointed.
> >Again I apprecaite your concern.[/color]
>
> 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...[/color]


Closed Thread


Similar C# / C Sharp bytes