【问题标题】:C# Output To a Text BoxC# 输出到文本框
【发布时间】:2016-09-23 20:05:37
【问题描述】:

这听起来真的很愚蠢,但我正在上一门 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        

【问题讨论】:

  • 我想我需要根据两个答案来澄清我的问题。我知道如何创建表单应用程序以及如何向表单添加文本框。我的问题是,我将可执行代码放在哪里? Program.cs 还是 Form1.cs?我如何“链接”这两者?

标签: c# textbox output


【解决方案1】:

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

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

出现新建项目对话框。

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

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

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

NoteNote .NET Framework 中的 Windows 窗体应用程序模板 4 默认情况下以客户端配置文件为目标。

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

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

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

SOURCE

然后从工具箱中拖出文本框,放到表单上。

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

添加:

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

在:

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

按 F5

Youtube Form Youtube textbox

【讨论】:

  • 我认为我的问题不够清楚。我知道如何在 Vis Studio 2015 中创建一个表单并向该表单添加一个文本框。那么问题就变成了,我将实际程序的代码放在哪里?在 program.cs 或 Form1.cs 中?
【解决方案2】:

您只需要一个Form 和一个TextBox 作为Form 的子代:

var form = new Form{Width = 300, Height = 100, Text = "The form"};
var textbox = new TextBox{Parent=form, Size = form.ClientRectangle.Size, Multiline = true};

textbox.Text = "Your Text";

form.ShowDialog();

您还需要这个using System.Windows.Forms 并引用System.Windows.Forms.dll

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多