如何使用每个单词的一个按钮(单击)将一个单词添加(附加)到同一个句子

时间:2013-02-12 21:07:21

标签: c#

我非常感谢任何帮助。我有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();
        }
    }
}

5 个答案:

答案 0 :(得分:3)

使用lb1SentenceText.Text+=whatever.Text;

+ =相当于lbSentence.Text = lblSentenceText.Text + whatever.Text

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

所以:

string rhs="Hello " ;
string lhs = "World";
string rhs = rhs + lhs;//Hello World

答案 1 :(得分: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;
        }
    }

答案 2 :(得分:0)

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

答案 3 :(得分:0)

尝试:

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

+ =运算符意味着添加到,并且修改的变量等于附加到原始值的添加(Int,String)(x = 1; x + = 3; x现在是4)

试试这个:

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;
}

答案 4 :(得分:0)

//Simple.Create a global variable and within each button click event do this;

string yourStrVar = ""; //Must be global

yourStrVar+=  ((Button)sender).Text
相关问题