Solved

Insert variable at the cursor in an textbox

Posted on 2004-09-25
3
179 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

23 Experts available now in Live!

Get 1:1 Help Now