BlackStone Posted November 17, 2004 Posted November 17, 2004 In a TextBox, is there an event that is thrown when the caret is moved, whether by keyboard or mouse? Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
jdemackio Posted November 17, 2004 Posted November 17, 2004 I don't believe there is, however you can use the TextBox's KeyDown and MouseDown events. You can keep track of the cursor's position by using the event arguments (e.X gives the current X position) you know the cursor was moved if this does not match the old position that you are keeping track of. Also for KeyDown the cursor has to move because something just got typed into it or erased from it. Maybe you can compare before and after lengths of the text in the TextBox. Quote - The only true knowledge is knowing that you know nothing.
BlackStone Posted November 17, 2004 Author Posted November 17, 2004 (edited) I need to be able to trim the spaces off the end of a line when the caret is moved away from it. That part is fined(I have to track every way possible of moving the caret, Up\Down\PgUp\PgDown\Ctrl+Home\Ctrl+End). The problem is the caret moves down to the new position of the line. An example('-'s are spaces): LINE OF TEXT------ ANOTHER LINE OF TEXT I want the caret to land between 'E' & 'X'. However, because I moved the caret automatically with the trim, it goes between 'E' & ' '. I tried to fix the problem by completely intercepting UP and DOWN, but it gets even more complicated with the other keys. Not to make this post any more befuddled than it already is, but using a RichTextBox, there is a desired position on the line(It tries to move the caret to the same position on the line your moving to or else it moves to the last character on the line). Is there anyway to set this desired position?? Any ideas, anyone? Edited November 17, 2004 by BlackStone Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
*Experts* Nerseus Posted November 17, 2004 *Experts* Posted November 17, 2004 If you want to just trim spaces, why not use the Leave or LostFocus events? Or, if you're binding, use the DataSet's ColumnChanged event or something similar? Maybe when you do EndEdit on the DataRow? Or, if you have a common class to handle Database calls that takes a DataSet, it could generically loop through rows/columns and trim everything that's a string - it's more extreme, but easy to write and use. Another alternative would be trimming in a stored procedure. I wouldn't recommend it as there would be no UI validation and it splits the business rules even more than usual. It has the advantage of knowing that regardless of client side code, the field will always be trimmed before going in the Database. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
jdemackio Posted November 18, 2004 Posted November 18, 2004 If your concerned about removing spaces, why not wait until the end and then go through each line and perform the necessary trimming? I really don't think there is a way to do what you want because the RichTextBox does not have any functionality of moving the caret around or getting the current position of the caret. Quote - The only true knowledge is knowing that you know nothing.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.