473,549 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting cursor position for TextBox

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
Jun 19 '07 #1
4 64856
>Does anyone know of an easy way to set the position of the cursor in a
>TextBox?
Use the SelectionStart and SelectionLength properties.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 19 '07 #2
Mattias Sjögren wrote:
>Does anyone know of an easy way to set the position of the cursor in a
TextBox?

Use the SelectionStart and SelectionLength properties.
Mattias
I already tried SelectionStart property but I tried SelectionLength as
well by using the following:

ldapHostField.S electionStart = 0;
ldapHostField.S electionLength = 0;

This did not make any difference. The text in the field is still all
selected. I'm not sure why it is even w/o setting values for those 2
properties unless it is because ldapHostField is the first field in the
Form. The cursor is still at the end of the text instead of at the
beginning.

thanks
Jun 20 '07 #3
Hi Brandon,

Based on my understanding, you'd like to set the position of the cursor in
a TextBox to just before the first character of the text. If I'm off base,
please feel free to let me know.

The simplest and recommended way to set the position of the cursor in a
TextBox is to use the SelectionStart property.

The following is a sample code. It requires that you add a TextBox on a
form.

private void Form1_Load(obje ct sender, EventArgs e)
{
this.textBox1.T ext = "this is a very very long text...";

this.textBox1.S electionStart = 0;
}

When you run the application, you should see the TextBox on the form is
focused and the cursor is before the first character in the TextBox.

If your problem is still not resolved, you may send me a sample project
that could just reproduce the problem. To get my actual email address,
remove 'online' from my displayed email address.

Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 20 '07 #4
Linda Liu [MSFT] wrote:
Hi Brandon,

Based on my understanding, you'd like to set the position of the cursor in
a TextBox to just before the first character of the text. If I'm off base,
please feel free to let me know.

The simplest and recommended way to set the position of the cursor in a
TextBox is to use the SelectionStart property.

The following is a sample code. It requires that you add a TextBox on a
form.

private void Form1_Load(obje ct sender, EventArgs e)
{
this.textBox1.T ext = "this is a very very long text...";

this.textBox1.S electionStart = 0;
}

When you run the application, you should see the TextBox on the form is
focused and the cursor is before the first character in the TextBox.

If your problem is still not resolved, you may send me a sample project
that could just reproduce the problem. To get my actual email address,
remove 'online' from my displayed email address.

thanks I got it working after seeing your code.
Jun 21 '07 #5

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

Similar topics

2
28868
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
9042
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
2517
by: gal20 | last post by:
In my login page I have 2 textbox and one submit button. I also have a keypad for users to enter text to the textbox. I need the coding for backspace button that can be used for both textbox. The one I found can only be used for one textbox. Is there any way to find the cursor position and how do I do that? Thanx in advance
1
2155
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!
3
2340
by: Doug Bell | last post by:
Hi, How do I get the position of the cursor (as character position) when it is in a text box with no text selected? Thanks Doug
0
1320
by: LProgrammer | last post by:
Hello! We are currently working on a PocketPC application in vb.net 2005. While working with the textbox control it is required that we get the exact cursor position. Cursor.Position The above option is not available in .Net Compact Framework. Any inputs here on this one?
6
1369
by: yasirkhan | last post by:
Hi, i am new in .net, and i am confused how to set cursor position in textbox. I have made an application of keyboard and other application of User Info. when i run my applications then the form named User info opens. in this form i have a text box, when this textbox is clicked then keyboard opens. But when i type a word from my keyboard...
2
2400
dj12345
by: dj12345 | last post by:
i have function to make character in lowercase if a text is in uppercase function upperCase(x) { try { var y=document.getElementById(x).value; document.getElementById(x).value=y.toLowerCase(); }
1
5406
by: sypher04 | last post by:
Hi, new here as a member, but have visited frequently as a guest. I'm a C# developer from Canada. I primarily work on ASP.net and Pocket PC. I am currently in the process of writing a new mobile app with the .NET Compact framework, but have hit a snag I'm hoping someone here will know something more about than myself. So, I have a textbox,...
0
7520
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
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...
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5368
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...
0
5088
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...
0
3500
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...
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
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.