473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 38132
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ddddd | last post by:
How do I place a Command Button on one of my forms that when pressed will open up a find/replace window that I can specify two criterias for search and then have the items I searched for show up on...
4
by: JackRazz | last post by:
I'm trying to use Visual Studio's Find/Replace to match VB declarations. This RegEx works fine in Regulator: ...
2
by: :\\\\derian | last post by:
anyone know where i could find the equivalent of the find / replace windows control for my project? :\\derian
2
by: scorpion53061 | last post by:
This excel find/replace code works great under Excel 2003 but bombs with an error (pasted below the code) in Excel 2000. Can anyone suggest an alternative to make both happy? I am using vb.net...
0
by: D Brown | last post by:
Does anyone know why my .NET development would hang / freeze when performing a Global Find / Replace on all files? I have a large ASP.NET VB Application. I have left the machine for 24 hours but...
0
by: vipal.keshwala | last post by:
I just migrated the access data base onto a new server but when I open the application up and click on find/replace in the find what field I try to right click a paste some information, but the...
0
by: Scott | last post by:
Ok I am using the code below to set the default find option but it does not seem to stick. I query the option with getoptions and it is set to 1 but when the find dialog opens it still is set to...
8
by: John Pye | last post by:
Hi all I have a file with a bunch of perl regular expressions like so: /(^|)\*(.*?)\*(|$)/$1'''$2'''$3/ # bold /(^|)\_\_(.*?)\_\_(|$)/$1''<b>$2<\/ b>''$3/ # italic bold...
4
by: Elliot Fraval | last post by:
Hi All, Environment is VBA code in Access 2007. I am having some problems performing a find and replace on text that originates from a memo field in a table. The code is supposed to take...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.