473,396 Members | 2,029 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,396 software developers and data experts.

wrong values from TextBox.SelectionStart property

I am getting clearly wrong values from TextBox.SelectionStart property
in C# code. For example, if I move caret one character to right by
pressing Keys.Right, sometimes declared SelectionStart property
actually decreases, sometimes it does not change. Difference seems
always at most off by 1 but it is very irritiating.

My setup is as follows:

Textbox txtText, txtCursor;
txtText.ReadOnly = true;

//since there is no SelectionChanged event (as far as i know) I detect
likely changes in an //adhoc manner. If this contributes to the problem
and if there is a better way, please tell me.
//Note that txtText is readonly, so pressing most keys does not change
caret position and is //irrelevant

private void txtText_KeyDown(...)
{
if(e.KeyCode in [Keys.Left, Right, Down, Up])
CursorMove();
}
private void txtText_MouseDown(...)
{
CursorMove();
}

//CursorMove output cursor position
void CursorMove()
{
int pos = txtText.SelectionStart;
txtCursor.Text = "pos=" + pos;
}

Any help is greatly appreciated. Thanks ahead of time.

Sep 3 '06 #1
1 4480
I figured it out. The problem is with listening to the TextBox.KeyDown
event. This way caret position value I get is the one BEFORE what
happens after completion of the key press, hence wrong. If I listen to
the TextBox.KeyUp event, I get accurate values. Don't make the same
mistake :)

Sep 9 '06 #2

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

Similar topics

0
by: Alan | last post by:
Hi all, I want to code the logic of delete some text by myself. The snippet of code like: int nStart = this.TextBox.SelectionStart; string sLast = this.TextBox.Text.Substring(0,...
1
by: Jed Ozone | last post by:
In a TextBox, I'm using a MouseMove event to track the cursor position. I want to get able to tell what text the current cursor position is at so I can set the SelectionStart to that position. ...
4
by: melanieab | last post by:
Hi, I need to write something so that when a textbox is entered (this is for a touchscreen), all the text in that textbox is selected (highlighted) and new keystrokes replace the old text. Help!...
2
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...
22
by: DraguVaso | last post by:
Hi, For my application I need the following behavior: When I press F4 the cursor has to move to the next line in my multiline textbox which begins with "0". Finding lines starting with 0 isn't...
2
by: Peter | last post by:
I hate using the mask edit conrol so I created these two function to format my phone numbers. Is there a way to consolidate these two functions into one? In this example say the textbox you...
1
by: fiaolle | last post by:
Hi The first set of source code is the class for a combobox in a grid, hopefully. In the second set of code we try to use the combobox class, but the grid is empty. I don't understand how this...
2
by: linuxpld | last post by:
Hi How can I force my cursor to go to the end of the text after pushing a button. Using System.Windows.Forms I have something like this: TextBox textBox; Button button;
4
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...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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
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,...

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.