快速更改发送到控制台的格式化字符串以写入TextBox? [英] quickly change formatted strings sent to Console to write to TextBox ?

查看:105
本文介绍了快速更改发送到控制台的格式化字符串以写入TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一些示例代码,这些示例代码使用了写入控制台的格式化字符串:like:

Suppose you have some sample code that uses formatted strings written to the Console: like:

Console.WriteLine(
"{0} (quantity {1}) was replaced by {2}, (quantity {3}).", 
item.Description, 
item.Quantity, 
replacement.Description, 
replacement.Quantity);

而且,您想快速将所有这种用法的实例转换为写入屏幕上的TextBox,本质上用其参数替换所有{#n}占位符,后跟.ToString()...,当然还要添加必要的括号引号和字符串串联运算符"+".

关于快速转换方案的任何想法吗?

另一种询问方式是:询问我是否可以从运行的WinForm程序中获取使用Console.WriteLine产生的文本,该文本通常显示在Visual Studio的输出"窗口中.

谢谢,Bill

And, you''d like to quickly convert all instances of this type of usage to write to an on-screen TextBox, essentially replacing all the {#n} placeholders with their arguments followed by .ToString() ... and of course adding the necessary bracketing quotes, and the string concatenation operator "+."

Any ideas for a quick conversion scheme ?

Another way to ask this is: to ask if I can somehow grab, from within a running WinForm program, the text that results from the use of Console.WriteLine, which normally shows up in the Output window in Visual Studio.

thanks, Bill

推荐答案

为什么?
将其替换为string.Format:
Why?
Replace it with string.Format:
Console.WriteLine(
   "{0} (quantity {1}) was replaced by {2}, (quantity {3}).", 
   item.Description, 
   item.Quantity, 
   replacement.Description, 
   replacement.Quantity);


myTextBox.Text = String.Format(
   "{0} (quantity {1}) was replaced by {2}, (quantity {3}).", 
   item.Description, 
   item.Quantity, 
   replacement.Description, 
   replacement.Quantity);


我认为您正在寻找这个

I think you are looking for this

StringBuilder str=new StringBuilder();
str.AppendFormat("{0} (quantity {1}) was replaced by {2}, (quantity {3}).",
                  item.Description,
                  item.Quantity,
                  replacement.Description,
                  replacement.Quantity
                 );
textBox1.Text = str.ToString();


我认为这样会更容易:

使用完全相同的名称和签名定义您使用的控制台方法:

I think this would be easier:

Define the Console methods you use with exact same name and signatures:

//[EDIT...]
internal class UiConsole {

    internal UiConsole(TextBox sink) { this.Sink = sink; }

    internal string void WriteLine(string value) { /* this is easy */} 

    internal string void WriteLine() { /* this is easy, too, just use Environment.NewLine */}

    internal string void WriteLine(string format, params object[] values) {
        string stringValue = string.Format(format, values);
        //add stringValue where you want it, for example, add to Sink
    } //WriteLine

    TextBox Sink;

//class UiConsole



以某种方式用您的新类替换Console,而无需在编写输出的代码部分中更改单个字符.这也很容易.下次,花一些时间编写输出接口并提供控制台实现.当您需要将输出定向到其他位置时,只需更改实现即可.



例如,您可以像这样使用它:



Replace Console with you new class somehow, the way you would not need to change a single character in the part of code which writes your output. This is easy, too. Next time, spend some time to write output interface and supply, say, console implementation of it. When you need to direct output somewhere else, just change the implementation.



For example, you can use it like this:

class SomeUseOfOutput {
 
   internal SomeUseOfOutput(UiConsole console) { this.Console = console; } 
    
   void OutputExample() {
       Console.WriteLine("Starting...");
       Console.WriteLine("Iteration #{0}: x={1}, y={2}", count, x, y);
       //... 
   } //OutputExample

   UiConsole Console; //same name as System.Console, for easy transition

} //SomeUseOfOutput


在此示例中,如果将using SystemSystem.Console一起使用,则方法OutputExample可以在此类之外工作;当您将其放在类SomeUseOfOutput中时,它将开始使用其UiConsole Console实例,并将相同的内容写入文本框.这样,无需以任何方式修改OutputExample的代码.

顺便说一句,我通常会做完全相同的事情,但是使用ListBox,而不是TextBox. TextBox没有附加操作,如果使用Text属性,附加操作将变得非常昂贵,并且文本缓冲区的大小将变得太长.我演示了对TextBox<code> in response to one of the questions; it never uses <code>Text的高性能附加,但是操纵选择和文本长度,通过将要附加的文本分配给SelectedText属性来完成附加. (实际上,我描述了伪代码.)使用ListBox,您只需添加一行并调整滚动(选择新行).

[END EDIT]

顺便问一下,以防万一:您是否知道仍可以在WPF或Forms应用程序(或任何地方)中使用控制台?

只需创建一个窗口化的应用程序即可.然后在项目属性中将应用程序类型更改为控制台应用程序".我不会破坏您的应用程序,它只会显示一个控制台. 控制台应用程序"仅表示:除其他外,显示一个控制台". 窗口应用程序"仅表示:隐藏控制台.这些类型的名称以及使用组合框的事实确实令人误解.这些类型不能像人们想象的那样相互替代.

—SA


In this example, the method OutputExample can work outside of this class if you use using System with System.Console; when you put it in the class SomeUseOfOutput, it starts working with its instance of UiConsole Console and write the same thing to your text box. In this way, there is no need to modify the code of OutputExample in any way.

By the way, I usually do exact same thing, but use ListBox, not TextBox. The TextBox does not have append operation, and appending becomes too expensive if you use Text property and the size of the text buffer becomes too long. I demonstrated high-performance append to TextBox<code> in response to one of the questions; it never uses <code>Text, but manipulates selection and text length, the append is done via assigning the text to be appended to SelectedText property. (In fact, I described the pseudo-code.) With ListBox, you only add a line and adjust scrolling (select new line).

[END EDIT]

By the way, want to ask you just in case: do you know that you can still use console in the WPF or Forms application (or anywhere)?

Just create a windowed application. Then change the application type to "Console Application" in the project properties. I won''t disrupt your application, it will only show a console. "Console Application" simply means: "among other thing, show a console". "Window Application" simply means: hide the console. The names of those types and the fact of using combo box is really misleading. Those types are not alternative to each other as one would think.

—SA


这篇关于快速更改发送到控制台的格式化字符串以写入TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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