473,395 Members | 1,458 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.

Current Text

I have a richtextbox, anybody have any ideas for me to be able to get the string from the last spacebar to the current position?
Nov 16 '05 #1
7 1247
Hi Bill

Will this give you what you want?

private string GetWord()
{
if(rtb.TextLength == 0)
return "";
string word = "";

for(int i = rtb.SelectionStart; i > 0; i--)
{
char c = rtb.Text[i-1];
if(c == ' ')
break;
word = c + word;
}
return word;
}
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Morten Wennevik <Mo************@hotmail.com> wrote:
Will this give you what you want?

private string GetWord()
{
if(rtb.TextLength == 0)
return "";
string word = "";

for(int i = rtb.SelectionStart; i > 0; i--)
{
char c = rtb.Text[i-1];
if(c == ' ')
break;
word = c + word;
}
return word;
}


A somewhat more efficient alternative:

string GetWord()
{
if (rtb.TextLength==0)
{
return "";
}

string text = rtb.Text;

int end = rtb.SelectionStart;
int start = text.LastIndexOf (' ', end);
if (start==-1)
{
start = 0;
}
return text.Substring (start, end-start);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Yes, I thought of that, but lastindexof reports the last ' ' of the text where the search begins at selectionstart. This is all fine if selectionstart actually is at the end of the text, but otherwise you might find spaces after current position and run the risk of substringing a negative number.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
Morten Wennevik <Mo************@hotmail.com> wrote:
Yes, I thought of that, but lastindexof reports the last ' ' of the
text where the search begins at selectionstart. This is all fine if
selectionstart actually is at the end of the text, but otherwise you
might find spaces after current position and run the risk of
substringing a negative number.


No, note that I used the version of LastIndexOf which takes two
parameters - the second parameter is the index at which to start the
search. So, for instance, "hello there Morton".LastIndexOf(' ', 8)
returns 5.

The OP may wish to tailor things considering what he wishes to happen
if the current selection *is* a space, however.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Ah, sorry, the phrasing in the help file confused me.

IndexOf(char value, int startIndex)
LastIndexOf(char value, int startIndex)

They should have changed it to endIndex. Also the helpfile describes startIndex as
"The starting position of a substring within this instance." which I read to be

string text = "Hello Morten Wennevik";
text.LastIndexOf(' ', 8) == (text.SubString(8)).IndexOf(' ');

It may be that my English skills are lacking, but I find the helpfile very confusing in this case.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Morten Wennevik <Mo************@hotmail.com> wrote:
Ah, sorry, the phrasing in the help file confused me.

IndexOf(char value, int startIndex)
LastIndexOf(char value, int startIndex)

They should have changed it to endIndex. Also the helpfile describes startIndex as
"The starting position of a substring within this instance." which I read to be

string text = "Hello Morten Wennevik";
text.LastIndexOf(' ', 8) == (text.SubString(8)).IndexOf(' ');

It may be that my English skills are lacking, but I find the helpfile
very confusing in this case.


Yes, the general description isn't great. The more detailed description
makes more sense.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Change that to

text.LastIndexOf(' ', 8) == ((text.SubString(8)).LastIndexOf(' ') + 8)

The first returns 5, and the other returns 12 (4+8).

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephen Yip via .NET 247 | last post by:
Dear All, I would like to perform a simple task by program (VB.Net or VC#.Net). When a user logins the windows and try to run my program, how can I use his login information (username and...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
3
by: Christopher Weaver | last post by:
I want to set a value in a specific field in the current row of a DataSet. This seems like the most basic thing to do but I can't find the syntax for identifying the current row. IOW, I can do...
3
by: Carolyn Vo | last post by:
I have a datagrid in my web control class that I am trying to get the current rows displayed for. I have enabled paging on the datagrid so if the user is currently on page 3 of 8, and if I have...
2
by: Jesper Stocholm | last post by:
I have implemented role-based security within my ASP.Net application. However, it seems the role is not passed to the authentication ticket I create. I want to use it to display/hide some...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
0
by: Ben | last post by:
module main ... application.run(new splashform) .. end module after a few screen, I try to load a new codes I got from MSDN on datagrid that works on its own. I took out submain and ran...
1
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
9
by: Alex | last post by:
Get the Name and Phone Number of the Current Windows User in a .NET Application I am writing a simple .NET (C#) application. It needs to "automatic" get the Name (last, first) and phone number...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.