Solved

Insert variable at the cursor in an textbox

Posted on 2004-09-25
3
182 Views
Last Modified: 2010-04-23
I've a function which inserts todays date in a textbox like this: textbox.Text = textbox.Text + date
As you can see the date gets written in the end of the textbox.

How it looks like now, if I insert the date:
"I was in Sweden. It was fun." + date

I wanna have it like this:
"I was in Sweden" + date + ". It was fun."

I've tested some with SelectStart and so on, but with no success.
Hopes you understand and are willing to help =)!
0
Comment
Question by:stromberg10
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 12151779
The following will change whatever the currently selected text in the box is to the current date.  If nothing is currently selected, then the date will be inserted wherever the cursor is.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.SelectedText = DateTime.Now().ToShortDateString
    End Sub

Regards,

Idle_Mind
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12151790
If you need to programmatically position the cursor before you insert the date then set the focus to the box and use the SelectionStart and SelectionLength properties:

        TextBox1.Focus()
        TextBox1.SelectionStart = 5
        TextBox1.SelectionLength = 0

~IM
0
 

Author Comment

by:stromberg10
ID: 12151965
I made it myself :D, but thanks for the help!

I did like this:
textBox.Text = textBox.Text.Insert(textBox.SelectionStart, Date.Now)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now