C# 如何将日期时间附加到ConsoleTextBox?
我根据创建了一个文本框。它的目的是显示写入控制台的所有内容。这节课:C# 如何将日期时间附加到ConsoleTextBox?,c#,datetime,textbox,console,C#,Datetime,Textbox,Console,我根据创建了一个文本框。它的目的是显示写入控制台的所有内容。这节课: public class TextBoxConsole : TextWriter { TextBox output = null; //Textbox used to show Console's output. /// <summary> /// Custom TextBox-Class used to print the Console outpu
public class TextBoxConsole : TextWriter
{
TextBox output = null; //Textbox used to show Console's output.
/// <summary>
/// Custom TextBox-Class used to print the Console output. It also saves the output to a *.txt-File on the Desktop.
/// </summary>
/// <param name="_output">Textbox used to show Console's output.</param>
public TextBoxConsole(TextBox _output)
{
output = _output;
output.ScrollBars = ScrollBars.Both;
output.WordWrap = true;
}
/// <summary>
/// Appends text to the textbox and to the logfile
/// </summary>
/// <param name="value">Input-string which is appended to the textbox and logfile.</param>
public override void Write(char value)
{
base.Write(value);
output.AppendText(value.ToString());//Append char to the textbox
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
现在,我想在写入控制台的每条语句前面添加当前时间。我不能直接在Writechar value方法中实现这一点,因为这将在每个字符之前追加时间。有没有不向每个控制台写入时间的解决方案。WriteLine语句?向TextBoxConsole添加额外的重载
然后你可以直接打电话给
InitializeComponent();
writer = new TextBoxConsole(tbConsole);
Console.SetOut(writer);
writer.Write("DATE_TIME_FORMATTED_AS_STRING");
我还没有完全测试过这一点,但它应该让你朝着正确的方向前进 在文本框的“属性”窗口中的“事件”下,为按键生成事件
private void txtConsole_KeyPress(object sender, KeyPressEventArgs e)
{
// checks if the last key that was pressed was the enter key
if (e.KeyChar == (char)Keys.Return)
{
// once the user presses enter write the date
Console.WriteLine("");
}
}
这是有道理的,但我怎么能称之为过载?Write仍然调用Writechar值方法。
InitializeComponent();
writer = new TextBoxConsole(tbConsole);
Console.SetOut(writer);
writer.Write("DATE_TIME_FORMATTED_AS_STRING");
private void txtConsole_KeyPress(object sender, KeyPressEventArgs e)
{
// checks if the last key that was pressed was the enter key
if (e.KeyChar == (char)Keys.Return)
{
// once the user presses enter write the date
Console.WriteLine("");
}
}