473,609 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to position cursor after changing selection in TextBox derived

I'm working on a TextBox control (in C# using .NET 2.0) that allows fields
(like MS Office's mail merge feature). I want the editor to treat the field
as though it was a single character. The user shouldn't be able to delete
part of the field or select only part of the field.

For example, a line might look like: «Image» shot on «Date» at «Resolution»
where «Image» «Date» «Resolution» are all fields.

My control inherits from TextBox. I've overloaded OnKeyDown & OnKeyPress so
that if the user backspaces it deletes the whole field. If they move left or
right, it skips past the entire field. I've also overloaded OnMouseUp so if
they select part of the field with the mouse, it increases the selection to
select all of the field. This all seems to work.

My problem is if the user holds shift and left, I can't get the field
selected and put the cursor to the left of the selection. Similarly if they
select by dragging the mouse right to left, the cursor is on the right side.

A snippet of the OnMouseUp code follows:

int SelectionEnd = SelectionLength + SelectionStart;
for (int i = SelectionStart; i >= 0; i--)
{
if (Text[i] == FieldEnd)
// Found an end tag. SelectionStart isn't in a field.
break;
if (Text[i] == FieldStart)
{
// Found the start tag. Modify selection.
Select(i, SelectionEnd - i);

}
}

I assume I can't do what I want with just Select(start,le n) or
SelectionStart and SelectionLength . I've tried setting start to the end and
using a negative length (like you would do with a RichTextBox , but that
doesn't work. I've even tried setting SelectionStart to the position I want
after setting the selection but still no joy. Any help will be much
appreciated!
Mar 27 '07 #1
0 1884

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

Similar topics

3
11238
by: jason | last post by:
I've got this javascript routine (i found on google - thank you) in an asp.net page that on page reload sets the cursor of a textbox to the last line. It works great! Using a similar concept, I have another application that uses a textbox like an editor window and has a save and other buttons. Problem is - when I save/post/reload, the textbox returns to cursor the top again. How can I preserve / save the exact cursor spot and return...
2
28871
by: AP | last post by:
I am trying to find the integer value for the cursor position in a text box, in particular I want to find out if I am at the end of some text that I typed or at the beginning. I am using IE 6. Thanks, Andy
7
9047
by: Steve | last post by:
does any one know a way to determine the position of the cursor in a text box. I would like to position a list next to the current position for context commands. I am using VS.NET/C#/System.Windows.Forms.
1
2162
by: Matt Tapia | last post by:
Is there a way to insert text into a multi-row textbox where the cursor position is at in the multi-row textbox? Just wondering..I was told I could use the SelectedText property however I get this error: 'SelectedText' is not a member of 'System.Web.UI.WebControls.TextBox'. Thanks!
2
6771
by: DaveR | last post by:
Can anybody help me to a)get the user's cursor insertion point (caret) from a multiline textbox, using client-side script I suppose, and then b)return the position to VB code behind? I have tried the following client-side function suggested on this Google Group (thank you) but I'm not sure what it's doing or how to return the caret position to code behind ("txtReading" is the Textbox ID): function getcaretpos(txtReading)
2
2132
by: Gene Hubert | last post by:
For a string that is being edited in a textbox, is there a way to find the position of the edit cursor in the string being edited? For example, it would be 0 or 1 at the beginning of a string and go up by one each time the right arrow key was pressed. Thanks Much, Gene in NC
4
6244
by: Neil Robbins | last post by:
Could anyone suggest a way in which I could find the position of the cursor within a textbox. I want to evaluate every keypress made within the textbox in order to validate it but in order to do this I need to know where the cursor is when the key is pressed. So far I can't see how I could do this. Any help would be very much appreciated, Neil R.
2
3074
by: ThunderMusic | last post by:
Hi, I have a form in which there are many <asp:textbox> tags. When I run the form, we use tab to navigate across the textboxes, but one of our client noticed that when we navigate to some readonly textboxes, the cursor (text cursor, not mouse cursor) disapears and when we tab to the next textbox and come back (shift-tab), not it's ok. Notice that when we navigate to each textbox, the text is automaticaly selected which is not the case for...
4
64865
by: Brandon McCombs | last post by:
Does anyone know of an easy way to set the position of the cursor in a TextBox? If I set text in the box that is longer than the length of the TextBox the position of the cursor is at the very end of the line and a user would have to use the arrow keys to move it back to the beginning of the line of text. I'd prefer to set the cursor position automatically to just before the first character of the text. thanks
0
8035
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8509
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8374
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6034
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5502
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4002
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4059
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.