我正在尝试使用 VS 执行 Windows 窗体应用程序(对我来说都是新的,代码和所有)来完成新 C# 类的作业。我们被指派创建一个句子生成器,其中包含各种按钮单词;然后当应用程序运行时,用户可以点击按钮来造句;然后显示在 Label 控件中。
好吧,我已经构建了表单,并且从我在该站点上找到的其他信息中找到了类似的问题;已经做到了这一点。但我的问题是 - 我的导师说我们应该在标签输出中连接结果,但首先我不知道如何与随机点击字母或单词的人一起做**(到目前为止我们学到的东西)。
我让它用下面的代码运行(没有在标签中连接);除了“spaceButton”事件放入“(Space)”的文本因为那是它的文本......我在代码中将它更改为“”并且如果我点击它运行,它现在将放入空格但改变了正在运行的应用程序中的文本到空白按钮。我不知道如何解决这个问题。
我之前有过这名讲师,虽然我可以解决“sentenceOutputLabel”中的串联 - 我很可能得到零,因为我没有在输出标签中串联。
劳拉
所有代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sentence_Builder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void upperCaseAButton_Click(object sender, EventArgs e)
{
string output;
output = upperCaseAButton.Text;
sentenceOutputLabel.Text += output;
}
private void lowerCaseAButton_Click(object sender, EventArgs e)
{
string output;
output = lowerCaseAButton.Text;
sentenceOutputLabel.Text += output;
}
private void upperCaseAnButton_Click(object sender, EventArgs e)
{
string output;
output = upperCaseAnButton.Text;
sentenceOutputLabel.Text += output;
}
private void lowerCaseAnButton_Click(object sender, EventArgs e)
{
string output;
output = lowerCaseAnButton.Text;
sentenceOutputLabel.Text += output;
}
private void upperCaseTheButton_Click(object sender, EventArgs e)
{
string output;
output = upperCaseTheButton.Text;
sentenceOutputLabel.Text += output;
}
private void lowerCaseTheButton_Click(object sender, EventArgs e)
{
string output;
output = lowerCaseTheButton.Text;
sentenceOutputLabel.Text += output;
}
private void manWordButton_Click(object sender, EventArgs e)
{
string output;
output = manWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void womanWordButton_Click(object sender, EventArgs e)
{
string output;
output = womanWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void dogWordButton_Click(object sender, EventArgs e)
{
string output;
output = dogWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void catWordButton_Click(object sender, EventArgs e)
{
string output;
output = catWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void carWordButton_Click(object sender, EventArgs e)
{
string output;
output = carWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void bicycleWordButton_Click(object sender, EventArgs e)
{
string output;
output = bicycleWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void beautifulWordButton_Click(object sender, EventArgs e)
{
string output;
output = beautifulWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void bigWordButton_Click(object sender, EventArgs e)
{
string output;
output = bigWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void smallWordButton_Click(object sender, EventArgs e)
{
string output;
output = smallWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void strangeWordButton_Click(object sender, EventArgs e)
{
string output;
output = strangeWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void lookedAtWordButton_Click(object sender, EventArgs e)
{
string output;
output = lookedAtWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void rodeWordButton_Click(object sender, EventArgs e)
{
string output;
output = rodeWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void spokeToWordButton_Click(object sender, EventArgs e)
{
string output;
output = spokeToWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void laughedAtWordButton_Click(object sender, EventArgs e)
{
string output;
output = laughedAtWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void droveWordButton_Click(object sender, EventArgs e)
{
string output;
output = droveWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void spaceButton_Click(object sender, EventArgs e)
{
string output;
output = spaceButton.Text = " ";
sentenceOutputLabel.Text += output;
}
private void periodButton_Click(object sender, EventArgs e)
{
string output;
output = periodButton.Text;
sentenceOutputLabel.Text += output;
}
private void exclamButton_Click(object sender, EventArgs e)
{
string output;
output = exclamButton.Text;
sentenceOutputLabel.Text += output;
}
/ I DON'T EVEN KNOW WHAT I NEED THIS below FOR NOW
private void sentenceOutputLabel_Click(object sender, EventArgs e)
{
//string output;
// sentenceOutputLabel.Text = sentenceOutputLabel.Text;
}
private void clearButton_Click(object sender, EventArgs e)
{
sentenceOutputLabel.Text = "";
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}`
请您参考如下方法:
如果我正确理解作业描述,这就是我会做的:
using System;
using System.Windows.Forms;
namespace StringBuilder
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnInput_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
if (button.Text == "Space")
lblOutput.Text += " ";
else
lblOutput.Text += button.Text;
}
}
}
}
使用这段代码,每个按钮都可以分配给 btnInput_Click 事件处理程序,而 lblOutput 只是输出。
