Solved

Add date to textbox on Enter-key c#

Posted on 2008-11-02
2
1,423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

Independent Software Vendors: 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
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 use and create keystrokes in Netbeans IDE 8.0 for Windows.

752 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