2

我非常感谢任何帮助。我有 20 多个按钮,每个按钮都有一个单词、空格或句点。每次我单击一个按钮时,先前存在的单词都会被清除并替换为新单词。在单击“清除”按钮之前,我需要每个单词和/或空格保持原位。

也许这是以前在不同的搜索词下被问过/回答过的?我倾向于认为我需要识别一个字符串变量,但不知道如何开始。

===============

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmSentenceBuilder : Form
    {
        public frmSentenceBuilder()
        {
            InitializeComponent();
        }

        private void frmSentenceBuilder_Load(object sender, EventArgs e)
        {

        }

        private void btnA_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnA.Text;
        }

        private void btn_a_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btn_a.Text;
        }

        private void btnAn_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnAn.Text;
        }

        private void btn_an_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btn_an.Text;
        }

        private void btnThe_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnThe.Text;
        }

        private void btn_the_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btn_the.Text;
        }

        private void btnman_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnman.Text;
        }

        private void btnwoman_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnwoman.Text;
        }

        private void btndog_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btndog.Text;
        }

        private void btncat_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btncat.Text;
        }

        private void btncar_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btncar.Text;
        }

        private void btnbicycle_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnbicycle.Text;
        }

        private void btnbeautiful_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnbeautiful.Text;
        }

        private void btnbig_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnbig.Text;
        }

        private void btnsmall_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnsmall.Text;
        }

        private void btnstrange_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnstrange.Text;
        }

        private void btnlookedat_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnlookedat.Text;
        }

        private void btnrode_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnrode.Text;
        }

        private void btnspoketo_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnspoketo.Text;
        }

        private void btnlaughedat_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnlaughedat.Text;
        }

        private void btndrove_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btndrove.Text;
        }

        private void btnSpace_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = " ";
        }

        private void btnperiod_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnperiod.Text;
        }

        private void btnexclam_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = btnexclam.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lblSentenceText.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
4

5 回答 5

3

使用lb1SentenceText.Text+=whatever.Text;.

+= 等价于lbSentence.Text = lblSentenceText.Text + whatever.Text.

基本上,它将右侧附加或连接stringstring左侧。希望它有意义吗?

所以:

string rhs="Hello " ;
string lhs = "World";
string rhs = rhs + lhs;//Hello World
于 2013-02-12T21:08:44.453 回答
2

请受到启发。你有很多冗余代码。

    btnA.Click += AppendButtonText;
    btn_a.Click += AppendButtonText;
    ...


    private void AppendButtonText(object sender, EventArgs e)
    {
        var button = sender as Button;
        if (button != null)
        {
            lblSentenceText.Text += button.Text;
        }
    }
于 2013-02-12T21:14:27.443 回答
0
//Simple.Create a global variable and within each button click event do this;

string yourStrVar = ""; //Must be global

yourStrVar+=  ((Button)sender).Text
于 2013-02-12T21:33:25.957 回答
0

在按钮单击事件上,将其更改为+=而不是=(清除按钮除外)。这相当于写作something = something + newValue;

于 2013-02-12T21:11:49.173 回答
0

尝试:

lb1SentenceText.Text = lb1SentenceText.Text + *something*.text

+= 运算符表示加到,修改后的变量等于原值(x = 1;x += 3;x 现在为 4)追加的 (Int, String)

尝试这个:

button.Click += new System.EventHandler(ButtonClick);
button1.Click += new System.EventHandler(ButtonClick);
// And for each button, one of those.
private void ButtonClick(object sender, System.EventArgs e)
{
// Do whatever you want to do here, you can place the TEXT to be appended on the button, if so:
lb1SentenceText.Text += sender.Text;
}
于 2013-02-12T21:18:12.993 回答