这种效果如何实现呢? 点击button后,textBox1.text = button1.text (变色,已实现)但是,再次点击后,黄色消失(已实现),textBox1.text已有的button1.text的内容消失第三次点击,又出现第四次点击,又消失 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 bool ok=true;button_click(){ok=!ok;if(ok)textbox1.text=button1.text;} ok =!ok; 什么意思啊? 分别点击 button1,button2,button3,它们的text出现在 textBox1,再次分别点击 button1,button2,button3,依次在textBox消失不是用 textBox1.Text = ""; 本帖最后由 caozhy 于 2012-11-03 22:25:42 编辑 点击 button1,button2,button3,.... 后,它们的text按点击顺序出现在 textBox1 并且,在它们之间自动添加添加 "," 用于间隔我之前用的是 textBox1.Text += button6.Text + "," 效果不是很好 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; } } }} 你最好是将多个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(); } 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都可以调用这个事件处理函数 如果button1.text是"button1,button2",textbox1.text正好包含button1.text,那么是会不会认为button2已经被点击过了呢?for/foreach遍历button的bool型标记必不可少。 根据业务来看,是不会出现你说的情况的。一个button代表一个不同的关键字。 比如:button1.text="吃",button2.text="喝",button.text="吃喝"挺简单的业务了吧,这种情况不可避免的会出现,而且遍历是一种不可避免的过程,jquery easyui中的tree插件里面树形checkbox级联操作也是这种最原始的遍历,而不是简单的检查indexof()==-1 如果我们约定关键字中不出现引号("")的话,那么我们把关键字用引号括起来就完美解决这个问题了。 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()); }也可以把引号换成其他关键字中绝对不会出现的字符就行了。 关于地图API ,求高手指点 C#项目怎样设置成可覆盖安装 请问电子白板的录制功能怎么实现? OWC11 如何设定Style(LineStyle) 请教高手,是个dataGridView下面的滚动条做到联动的问题! vfp数据库(单表)绑定DATAGRID的问题! DataGrid怎么可以像excel一样显示单元格的网格呀 怎样截取Message(信使服务)发来的消息 And ... 未将对象引用设置到对象的实例,高手请进啊 ,急 ,在线等待 !!!谢谢了 HELP ME!!!!!!!!!!!!!!!! 【求助】c# winform程序打包后配置文件如何更改? C#连接池如何实现的
button_click()
{
ok=!ok;
if(ok)
textbox1.text=button1.text;
}
它们的text按点击顺序出现在 textBox1 并且,在它们之间自动添加添加 "," 用于间隔我之前用的是
textBox1.Text += button6.Text + "," 效果不是很好
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;
}
}
}
}
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();
}
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都可以调用这个事件处理函数
如果button1.text是"button1,button2",
textbox1.text正好包含button1.text,那么是会不会认为button2已经被点击过了呢?for/foreach遍历button的bool型标记必不可少。
一个button代表一个不同的关键字。
比如:button1.text="吃",button2.text="喝",button.text="吃喝"挺简单的业务了吧,这种情况不可避免的会出现,而且遍历是一种不可避免的过程,
jquery easyui中的tree插件里面树形checkbox级联操作也是这种最原始的遍历,而不是简单的检查indexof()==-1
如果我们约定关键字中不出现引号("")的话,那么我们把关键字用引号括起来就完美解决这个问题了。 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());
}也可以把引号换成其他关键字中绝对不会出现的字符就行了。