将Console.WriteLine转换为文本框

时间:2011-10-01 00:52:11

标签: c#

我在网上查了一下,但我找不到具体的例子......

我想要的是将这些Console.WriteLine显示在文本框中。

    // Show data before change
 Console.WriteLine("name before change: {0}", 
 thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);

 // Change data in Customers table, row 9, CompanyName column
 thisDataSet.Tables["Customers"].Rows[9]["CompanyName"] = "Acme, Inc.";

 // Call Update command to mark change in table
 thisAdapter.Update(thisDataSet, "Customers");

 Console.WriteLine("name after change: {0}",
 thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]); 

我试过了;

         string1=("name before change: {0}",
         thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);


        thisDataSet.Tables["Customers"].Rows[9]["CompanyName"] = "Acme, Inc.";
         thisAdapter.Update(thisDataSet, "Customers");

        string2=("name after change: {0}",
        thisDataSet.Tables["Customers"].Rows[9]["CompanyName"].ToString());
        thisConnection.Close();

        textBox1.Text = string1() + string2();

4 个答案:

答案 0 :(得分:2)

 string1=("name before change: {0}",
     thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);

应该是

string1=string.Format("name before change: {0}",
     thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);

(对于string2相同)和

textBox1.Text = string1() + string2();

应该是

textBox1.Text = string1 + string2;

答案 1 :(得分:1)

我会使用StringBuilder然后使用string.Format来提高可读性:

var sb = new StringBuilder();

sb.AppendFormat("name before change: {0}",
thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);

thisDataSet.Tables["Customers"].Rows[9]["CompanyName"] = "Acme, Inc.";
thisAdapter.Update(thisDataSet, "Customers");

sb.AppendFormat("name after change: {0}",
thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);

textBox1.Text = sb.ToString();

答案 2 :(得分:1)

为您的文本框写一个StringWriter。这是一个TextWriter(“控制台”显示为),因此您可以轻松交换Console.Out或普通的StringWriter来编写邮件。在Windows应用程序中,您可以将编写器的内容放入文本框中。

////////////////////////////////////////
// for a console application
TextWriter writer = Console.Out;

// for a windows application
TextWriter writer = new StringWriter();
////////////////////////////////////////

// Show data before change
writer.WriteLine("name before change: {0}", 
    thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);

// Change data in Customers table, row 9, CompanyName column
thisDataSet.Tables["Customers"].Rows[9]["CompanyName"] = "Acme, Inc.";

// Call Update command to mark change in table
thisAdapter.Update(thisDataSet, "Customers");

writer.WriteLine("name after change: {0}",
    thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);

////////////////////////////////////////
// for a windows application
textBox1.Text = writer.ToString();
////////////////////////////////////////

答案 3 :(得分:0)

你可以这样做:textBox1.Text = string.Format("name before change: {0}", thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]); 要么: textBox1.Text = "name before change: " + thisDataSet.Tables["Customers"].Rows[9]["CompanyName"];