Windows Develop Bookmark and Share   
 index > Windows Forms General > RichTextBox - finding word at a position
 

RichTextBox - finding word at a position

Hi,

In RichTextBox, I want to find the word which comes immediately(or 1 or 2 words) after a word say "Name".

Name John Age 23

I will use Find("Name"), now I want "John" or "Age" (1 or 2 words) from reference "Name".

How can we do it?

Thank you.
vijayaraghava  Thursday, March 02, 2006 7:28 PM
Hi,

I thought there was a built - in method for that purpose. I implemented what I said above as follows:

StringBuilder[] typesPresent = {new StringBuilder("Name"),new
StringBuilder("Age")};
char[] delim = {'\r','\n',' '};
int initialPos1=0,initialPos2=0,finalPos=0;
foreach(StringBuilder t in typesPresent)
{
t.Append(" ");
initialPos1 = 0;
while(initialPos1 != -1)
{
initialPos1 =
rtbEditor.Find(t.ToString(),initialPos1+1,RichTextBoxFinds.NoHighlight);
if(initialPos1 != -1)
{
initialPos2 = initialPos1 + t.Length;
finalPos = rtbEditor.Find(delim,initialPos2);
while(initialPos2 < finalPos)
{
type.Append(rtbEditor.Text[initialPos2]);
initialPos2++;
}
typeList.Add(type.ToString());
type.Remove(0,type.Length);
}
}
}

Here rtbEditor is RichTextBox control, typeList is an ArrayList, type is StringBuilder.


Bye.

vijayaraghava  Friday, March 03, 2006 10:05 AM
the find-method/function is overloaded.
ernieracer  Thursday, March 02, 2006 7:34 PM
Hi,

I don't know what follows "Name". Let the RichTextBox control contents be as follows.


Name John Age 23
Name Mike Age 30
Name Mary Age 25


I only know that there will be "Name" and "Age" in the text. Now using these as the reference, I need to find the other two columns like "John" and "23". This is not possible through Find() method or its overloads. I have taken a look at them.

There will be a space between each field in the above text.

Thank you.
vijayaraghava  Thursday, March 02, 2006 11:06 PM

you still have to code a little bit, even the .net-framework helps you with a lot of functions.

i think you didn't look at the Find() method + overloads. it has also a starting parameter/end parameter and so on...

where to start:

when you know, that after "Name" follows the NAME, then you use the function Find() to find the first and/or next "Name"+space and then the next space. and between the spaces is the NAME you need. the same with age. you should look into the MSDN and/or other helpfiles. what you need is how to parse strings or how to work with strings in general.

you can also experiment with the "Split()"-function: could be lesser code.

hope that helps
Daniel

ernieracer  Friday, March 03, 2006 12:56 AM
Hi,

I thought there was a built - in method for that purpose. I implemented what I said above as follows:

StringBuilder[] typesPresent = {new StringBuilder("Name"),new
StringBuilder("Age")};
char[] delim = {'\r','\n',' '};
int initialPos1=0,initialPos2=0,finalPos=0;
foreach(StringBuilder t in typesPresent)
{
t.Append(" ");
initialPos1 = 0;
while(initialPos1 != -1)
{
initialPos1 =
rtbEditor.Find(t.ToString(),initialPos1+1,RichTextBoxFinds.NoHighlight);
if(initialPos1 != -1)
{
initialPos2 = initialPos1 + t.Length;
finalPos = rtbEditor.Find(delim,initialPos2);
while(initialPos2 < finalPos)
{
type.Append(rtbEditor.Text[initialPos2]);
initialPos2++;
}
typeList.Add(type.ToString());
type.Remove(0,type.Length);
}
}
}

Here rtbEditor is RichTextBox control, typeList is an ArrayList, type is StringBuilder.


Bye.

vijayaraghava  Friday, March 03, 2006 10:05 AM

You can use google to search for other answers

Custom Search

More Threads

• How to get that the main form is going to be minimized
• How to thread a splash and some other work.
• Tree list grid view control needed
• Disposing a form which has user control?
• Sandbar Menus
• Moving between VS2003 & VS2005
• Help with TCP Client
• DirectX.AudioVideoPlayBack.Video problem
• datagridview current cell
• using ToolStrip in visual basics .NET 2003??