且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在文本框中插入当前日期/时间

更新时间:2023-01-29 08:33:03

一个文本属性TextBox允许您访问整个文本,获取和设置机器人 - 所以您需要做的就是决定放置它的位置,并插入日期字符串。

例如:

The Text property of a TextBox gives you access to the whole text, bot for get and set - so all you need to do is decide where you want to put it, and "insert" the date string.
For example:
myTextBox.Text = time.ToString("G") + myTextBox.Text;

将它插入前面。

或:

Will insert it at the front.
Or:

myTextBox.Text = myTextBox.Text.Replace("%%", time.ToString("G"));

将替换:

Would replace:

It is %% at the moment



With

It is 20/02/2014 13:02:10 at the moment


您可以使用文本框的SelectionStart属性。



例如:

You can use the SelectionStart property of the textbox.

For example:
txtWritingBox.Text = txtWritingBox.Text.Insert(txtWritingBox.SelectionStart, time.ToString("G"));


string inginsertText = "Blah Blah Blah Text";
int selectionIndex = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText);
textBox1.SelectionStart = selectionIndex + insertText.Length;



-KR


-KR