如何在文本框中插入当前日期/时间 [英] How to insert the current date/time in text box

查看:401
本文介绍了如何在文本框中插入当前日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本框中添加当前时间/日期。



使用下面的代码,我们可以在文本框的末尾添加时间/日期。< br $>


I want to add the current time/date in textbox.

Using the code below we can add the time/date at the end of the textbox.

txtWritingBox.AppendText(time.ToString("G"));





但是我想在文本框的任何地方插入time.date





你能解决这个问题吗?



But I want to insert time.date anywhere in the textbox


Can you solve this

推荐答案

一个文本属性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


这篇关于如何在文本框中插入当前日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆