Solved

Add date to textbox on Enter-key c#

Posted on 2008-11-02
2
1,421 Views
Last Modified: 2013-12-17
I was wondering if anyone could help me to get the code to do the following:
1. I have a textbox on the form that contains text
2. I want the program to automatically insert shortDateString to the top of the textbox when a user hits the "Enter"-key on the keyboard.
3. The date is only going to be inserted when the position of cursor is at beginning of textbox text when the enter key is pressed.
4. The user should then be able to start typing text right under the date.

I do not know a better way to explain this. Will try to explain better if it is not understanable.
0
Comment
Question by:orjanmj
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 22861867
For C# WinForms:
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                if (textBox1.SelectionStart == 0 && textBox1.SelectionLength == 0)
                {
                    textBox1.SelectedText = DateTime.Now.ToString("d");
                }
            }
        }

Open in new window

0
 

Author Comment

by:orjanmj
ID: 22862092
Thanks a bunch. I actually had already tried this before, but I had done a mixup with the selection start... This was exactly what I wanted!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question