Solved

Add date to textbox on Enter-key c#

Posted on 2008-11-02
2
1,424 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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.

631 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