Solved

Insert variable at the cursor in an textbox

Posted on 2004-09-25
3
192 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
[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
3 Comments
 
LVL 86

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 86

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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