在RichText框中插入时间戳c# [英] Insert Timestamp in RichText Box c#

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

问题描述

我使用richTextBox1来显示用户的聊天。



我想在richTextBox1中的日期和时间在richTextBox1中发送或接收的每条消息中。





这是在richTextBox1中显示聊天的消息处理程序代码。



< pre lang =cs> private void OnRoomMessage( object sender,agsXMPP.protocol.client.Message msg)
{
if base .InvokeRequired)
{
base .BeginInvoke( new agsXMPP.protocol.client.MessageHandler(OnRoomMessage), new object [] {sender,msg});
}
其他
{
尝试
{
if (msg.Body!= null
{
if (msg.From.Server.StartsWith( 会议))
{
richTextBox1.SelectionColor = Color.Green;
richTextBox1.AppendText(msg.From.Resource + :\t);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText(msg.Body + \ n\ n);
richTextBox1.Focus();
textBox9.Focus();
}
}
}
catch {}
}
}

解决方案

将这些行添加到您要将文本添加到richtextBox的代码中

 CultureInfo culture = new CultureInfo(en-gb); 
string timeStamp = DateTime.Now.ToString(G,culture);

将en-gb替换为您想要的文化格式。

然后将该文本附加到您想要时间戳的richtextbox中。例如

 richTextBox1.AppendText(timeStamp +\ n \ n); 





进一步的参考信息 [ ^ ]


使用这个



 richTextBox1.AppendText(DateTime.Now.ToString(  yyyyMMddHHmmssffff)+   \\ \
\\\
);


I m using a richTextBox1 to show chat of user.

I want Date and time in the richTextBox1 Of every message sent or received in richTextBox1.


Here is the message handler code that show chat in richTextBox1.

private void OnRoomMessage(object sender, agsXMPP.protocol.client.Message msg)
        {
            if (base.InvokeRequired)
            {
                base.BeginInvoke(new agsXMPP.protocol.client.MessageHandler(OnRoomMessage), new object[] { sender, msg });
            }
            else
            {
                try
                {
                    if (msg.Body != null)
                    {
                        if (msg.From.Server.StartsWith("conference"))
                        {
                            richTextBox1.SelectionColor = Color.Green;
                            richTextBox1.AppendText(msg.From.Resource + " :\t");
                            richTextBox1.SelectionColor = Color.Red;
                            richTextBox1.AppendText(msg.Body + "\n\n");
                            richTextBox1.Focus();
                            textBox9.Focus();
                        }
                    }
                }
                catch { }
            }
        }

解决方案

Add these lines to your code where you are adding the text to the richtextBox

CultureInfo culture = new CultureInfo("en-gb");
string timeStamp = DateTime.Now.ToString("G", culture);

Replace "en-gb" with the culture format that you want.
Then append that text to the richtextbox wherever you want your timestamp. E.g.

richTextBox1.AppendText(timeStamp+ "\n\n");



Further reference information [^]


use this

richTextBox1.AppendText(DateTime.Now.ToString("yyyyMMddHHmmssffff") + "\n\n");


这篇关于在RichText框中插入时间戳c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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