Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add date to textbox on Enter-key c#

Posted on 2008-11-02
2
Medium Priority
?
1,429 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 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

782 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