点击button后,textBox1.text = button1.text (变色,已实现)但是,再次点击后,黄色消失(已实现),textBox1.text已有的button1.text的内容消失第三次点击,又出现第四次点击,又消失

解决方案 »

  1.   

    bool ok=true;
    button_click()
    {
    ok=!ok;
    if(ok)
    textbox1.text=button1.text;
    }
      

  2.   

    ok =!ok;   什么意思啊?
      

  3.   

    分别点击 button1,button2,button3,它们的text出现在 textBox1,再次分别点击 button1,button2,button3,依次在textBox消失不是用 textBox1.Text = "";
      

  4.   

    本帖最后由 caozhy 于 2012-11-03 22:25:42 编辑
      

  5.   

    点击 button1,button2,button3,.... 后,
    它们的text按点击顺序出现在 textBox1  并且,在它们之间自动添加添加 "," 用于间隔我之前用的是 
    textBox1.Text += button6.Text + "," 效果不是很好
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections.Specialized;namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();  
            }
            Dictionary<Button, bool> list = new Dictionary<Button, bool>();
            private void Form1_Load(object sender, EventArgs e)
            {
                list.Add(button1, false);
                list.Add(button2, false);
                list.Add(button3, false);
            }        private void button1_Click(object sender, EventArgs e)
            {
                list[(Button)sender] = !list[(Button)sender];
                SetTextbox();
            }        private void button2_Click(object sender, EventArgs e)
            {
                list[(Button)sender] = !list[(Button)sender];
                SetTextbox();
            }        private void button3_Click(object sender, EventArgs e)
            {
                list[(Button)sender] = !list[(Button)sender];
                SetTextbox();
            }
            void SetTextbox()
            {
                string str = "";
                foreach (KeyValuePair<Button, bool> oo in list)
                {
                    if (oo.Value)
                    {
                        str += oo.Key.Text + ",";
                        oo.Key.BackColor = Color.Blue;
                    }
                    else
                        oo.Key.BackColor = Color.White;                textBox1.Text = str;
                }
            }
        }
    }
      

  7.   

    你最好是将多个button的click事件绑定到同一个button_click事件上,这样会少些很多代码。。button1.click+=new Eventhandler(button_click);
    button2.click+=new Eventhandler(button_click);
    button3.click+=new Eventhandler(button_click);
            private void button_Click(object sender, EventArgs e)
            {
                list[(Button)sender] = !list[(Button)sender];
                SetTextbox();
            }
      

  8.   


            List<string> slist = new List<string>();        private void button_Click(object sender, EventArgs e)
            {
                Button button = sender as Button;
                if (slist.Contains(button.Text))
                {
                    slist.Remove(button.Text);
                    button.BackColor = System.Drawing.SystemColors.Control;
                }
                else
                {
                    slist.Add(button.Text);
                    button.BackColor = Color.Yellow;
                }
                textBox1.Text = string.Join(",", slist.ToArray());
            }
        }所有button都可以调用这个事件处理函数
      

  9.   


    如果button1.text是"button1,button2",
    textbox1.text正好包含button1.text,那么是会不会认为button2已经被点击过了呢?for/foreach遍历button的bool型标记必不可少。
      

  10.   

    根据业务来看,是不会出现你说的情况的。
    一个button代表一个不同的关键字。
      

  11.   


    比如:button1.text="吃",button2.text="喝",button.text="吃喝"挺简单的业务了吧,这种情况不可避免的会出现,而且遍历是一种不可避免的过程,
    jquery easyui中的tree插件里面树形checkbox级联操作也是这种最原始的遍历,而不是简单的检查indexof()==-1
      

  12.   


    如果我们约定关键字中不出现引号("")的话,那么我们把关键字用引号括起来就完美解决这个问题了。        private List<string> slist = new List<string>();        private void button_Click(object sender, EventArgs e)
            {
                Button button = sender as Button;
                string temp = string.Concat('"', button.Text, '"');
                if (slist.Contains(temp))
                {
                    slist.Remove(temp);
                    button.BackColor = System.Drawing.SystemColors.Control;
                }
                else
                {
                    slist.Add(temp);
                    button.BackColor = Color.Yellow;
                }
                textBox1.Text = string.Join(",", slist.Select(str => str.Replace("\"", string.Empty)).ToArray());
            }也可以把引号换成其他关键字中绝对不会出现的字符就行了。