473,503 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 38143
"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
1433
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
2054
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
1617
by: :\\\\derian | last post by:
anyone know where i could find the equivalent of the find / replace windows control for my project? :\\derian
2
10001
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
1106
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
1222
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
1549
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
2696
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
4937
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
7203
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
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6995
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
7463
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...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.