C# 输出到文本框

标签 c# textbox output

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

a) 我们绕过了所有关于 GUI 的章节(将在下一季度的 C# 类(class)中出现),并且

b) 我已经检查了这本书,甚至 Stack Overflow 和其他在线资源,但还是想不通。

不幸的是,我的导师昨晚选择不在类里面讨论这个练习。因为他和我不在同一页上(不是不喜欢,更多的是化学问题),我试图自己解决这个问题。而且上交的截止日期已经过去,所以我现在只要求个人启迪。

所以,这是我创建的代码。我写它输出到控制台只是为了表明我有问题的基 native 制。我知道我必须在 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        

最佳答案

To create a Windows Forms Application project Start Visual Studio 2010.

On the File menu, point to New, and then select Project.

The New Project dialog box appears.

In the Installed Templates pane, expand Visual Basic or Visual C#, and then select Windows.

Above the middle pane, select the target framework from the drop-down list.

In the middle pane, select the Windows Forms Application template.

NoteNote The Windows Forms Application template in the .NET Framework 4 targets the Client Profile by default.

In the Name text box, specify a name for the project.

In the Location text box, specify a folder to save the project. Click OK.

The Windows Forms Designer opens and displays Form1 of the project.

SOURCE

然后将文本框从工具箱中拖放到窗体上。

双击表单上的任意位置,文本框除外,这将打开表单后面的代码,您将进入表单加载事件。

添加:

textBox1.Text = "您要放入文本框的文本";

在:

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

按 F5

Youtube Form Youtube textbox

关于C# 输出到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37450339/

相关文章:

python - 计算距离并打印出来

visual-studio - 来自实体数据模型的 VS2012 错误 : "Could not parse configuration file."

c# - 如何在 C# 中创建字典树

c# - 如何轻松初始化元组列表?

javascript - 在数字之间添加空格

javascript - 如何删除 Internet Explorer 中默认文本框中的十字图标

r - 为每行插入一个数字输入 - R Shiny

c# - 如何在我的 C# 应用程序中获得类似于 Spy++ 的功能?

c# - 如果不存在则创建应用程序配置文件 C#

c++ - 用于音频输出的简单c++ win控制台程序?