C# 输出到文本框

JBM*_*JBM 4 c# textbox output

这听起来真的很愚蠢,但我正在上一门 C# 课程,在那里我们跳过这本书,只在控制台应用程序中工作。我们得到了一个练习,根据冠词、名词、动词和介词的数组在字符串中构建句子,并将字符串的第一个单词中的第一个字母大写。关键是,它希望输出到文本框。这不会是一个问题,除非

a) 我们跳过了所有关于 GUI 的章节(将在下一季度的 C# 课程中介绍),并且

b) 我查过这本书,甚至 Stack Overflow 和其他在线资源,但无法弄清楚。

不幸的是,我的老师昨晚选择不在课堂上讨论这个练习。因为他和我不在同一页上(不是不喜欢,更多的是化学反应),我试图自己解决这个问题。上交的最后期限已经过去,所以我现在只要求个人的启发。

所以,这是我创建的代码。我写它输出到控制台只是为了表明我有问题的基本机制。我知道我必须在 GUI 窗口中创建一个带有文本框的单独表单,但我不知道如何将输出发送到文本框而不是控制台。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _16._4_StoryWriter
    {
       class StoryWriter
       {
          static void Main(string[] args)
          {
             string[] articles = { "the", "a", "one", "some", "any" };
             string[] nouns = { "boy", "girl", "dog", "town", "car" };
             string[] verbs = { "drove", "jumped", "ran", "walked", "skipped" };
             string[] preps = { "to", "from", "over", "under", "on" };

             string articleStory = "";
             string nounStory = "";
             string verbStory = "";
             string prepStory = "";

             Random random = new Random();


             for (int counter = 1; counter <= 10; ++counter)
             {
                int randomNext = random.Next(5);
                articleStory = articles[randomNext];


                randomNext = random.Next(5);
                nounStory = nouns[randomNext];

                randomNext = random.Next(5);
                verbStory = verbs[randomNext];

                randomNext = random.Next(5);
                prepStory = preps[randomNext];

                Console.WriteLine(UppercaseFirst(articleStory) + " " + nounStory + " " + verbStory + " " + prepStory + ".");
             } // End For

             Console.Read();
          } // End Main

          static string UppercaseFirst(string s) // Borrowed from dotnetperls.com tutorial for making first letter uppercase
          {
             if (string.IsNullOrEmpty(s)) // Checks for an empty string
             {
                return string.Empty;
             }
             char[] a = s.ToCharArray(); // Creates array of characters from a string
             a[0] = char.ToUpper(a[0]); // Selects value of zeroth position and changes to upper case
     return new string(a); // Passes new string back
          } // End method

       } // End Class
    } // End Namespace        
Run Code Online (Sandbox Code Playgroud)

Cla*_*ius 5

创建 Windows 窗体应用程序项目 启动 Visual Studio 2010。

在文件菜单上,指向新建,然后选择项目。

出现“新建项目”对话框。

在已安装的模板窗格中,展开 Visual Basic 或 Visual C#,然后选择 Windows。

在中间窗格上方,从下拉列表中选择目标框架。

在中间窗格中,选择 Windows 窗体应用程序模板。

注注 .NET Framework 4 中的 Windows 窗体应用程序模板默认面向客户端配置文件。

在名称文本框中,指定项目的名称。

在位置文本框中,指定一个文件夹来保存项目。单击确定。

Windows 窗体设计器打开并显示项目的 Form1。

来源

然后从工具箱中拖动文本框并将其放置在表单上。

双击表单上的任意位置,除了文本框,这将打开表单背后的代码,您将处于表单加载事件中。

添加:

textBox1.Text = "Your text to put in textbox";

在:

private void Form1_Load(object sender, EventArgs e)
{
    textBox1.Text = "Your text to put in textbox";
}
Run Code Online (Sandbox Code Playgroud)

按F5

Youtube 表单 Youtube 文本框