Solved

Insert variable at the cursor in an textbox

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 2 43
VB.NET 2008 Populate DataModel with DataTable 44 29
VB.Net Report Printing Issue 3 35
Obtain cell value using column ID in DataGrid using VB.NET 2 19
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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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