C# 输出到文本框
C# Output To a Text Box
这听起来真的很愚蠢,但我在 C# 中学习 class,我们跳过这本书,只在控制台应用程序中工作。我们得到了一个练习,根据冠词、名词、动词和介词的数组在字符串中构建句子,并将字符串的第一个单词的第一个字母大写。更重要的是,它希望输出到文本框。这不是问题,除了
a) 我们绕过了所有关于 GUI 的章节(将在下一季度的 C# class 中出现),并且
b) 我已经查看了这本书,甚至 Stack Overflow 和其他在线资源,但无法弄清楚。
不幸的是,我的导师在昨晚 class 中选择不讨论这个练习。因为他和我不在同一页上(不是不喜欢,更多的是化学问题),我试图自己解决这个问题。而且上交的截止日期已经过去了,所以我现在只要求个人启迪。
所以,这是我创建的代码。我写它输出到控制台只是为了表明我有问题的基本机制。我知道我必须在 GUI window 中创建一个带有文本框的单独表单,但我不知道如何将输出发送到文本框而不是控制台。
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
您只需要 Form
和 TextBox
作为 Form
的 child:
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
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.
然后将文本框从工具箱中拖放到窗体上。
双击表单上的任意位置,文本框除外,这将打开表单后面的代码,您将进入表单加载事件。
添加:
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
这听起来真的很愚蠢,但我在 C# 中学习 class,我们跳过这本书,只在控制台应用程序中工作。我们得到了一个练习,根据冠词、名词、动词和介词的数组在字符串中构建句子,并将字符串的第一个单词的第一个字母大写。更重要的是,它希望输出到文本框。这不是问题,除了
a) 我们绕过了所有关于 GUI 的章节(将在下一季度的 C# class 中出现),并且
b) 我已经查看了这本书,甚至 Stack Overflow 和其他在线资源,但无法弄清楚。
不幸的是,我的导师在昨晚 class 中选择不讨论这个练习。因为他和我不在同一页上(不是不喜欢,更多的是化学问题),我试图自己解决这个问题。而且上交的截止日期已经过去了,所以我现在只要求个人启迪。
所以,这是我创建的代码。我写它输出到控制台只是为了表明我有问题的基本机制。我知道我必须在 GUI window 中创建一个带有文本框的单独表单,但我不知道如何将输出发送到文本框而不是控制台。
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
您只需要 Form
和 TextBox
作为 Form
的 child:
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
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.
然后将文本框从工具箱中拖放到窗体上。
双击表单上的任意位置,文本框除外,这将打开表单后面的代码,您将进入表单加载事件。
添加:
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