//问题1:循环的终止条件始终变动
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{//问题2:循环内部If条件语句出现问题
if (checkedListBox1.CheckedItems.Contains(checkedListBox1.Items[i]))
{
checkedListBox3.Items.Add(checkedListBox1.Items[i].ToString() + "被移至右侧");
checkedListBox1.Items.Remove(checkedListBox1.Items[i]);
}
}
|
private
void button1_Click(object sender, EventArgs e)
{
foreach (object o in checkedListBox1.CheckedItems)
{
checkedListBox2.Items.Add(o);
}
for (int i = checkedListBox1.Items.Count - 1; i >= 0; i- -)
{
if (checkedListBox1.CheckedItems.Contains(checkedListBox1.Items[i]))
{
checkedListBox3.Items.Add(checkedListBox1.Items[i].ToString() + "被移至左侧");
checkedListBox1.Items.Remove(checkedListBox1.Items[i]);
}
}
}
|
public
partial class Form4 : Form
{
//建立数组,用以存储星期数据
private string[] weekleft;
private string[] weekright;
//左右键点击次数
int LeftClicknum = 0;
int RightClicknum = 0;
public Form4()
{
InitializeComponent();
}
/// <summary>
/// 初始化事件在左侧的checkedListBox加载星期信息
/// </summary>
private void Form4_Load(object sender, EventArgs e)
{
checkedListBox1.Items.Add("星期一");
checkedListBox1.Items.Add("星期二");
checkedListBox1.Items.Add("星期三");
checkedListBox1.Items.Add("星期四");
checkedListBox1.Items.Add("星期五");
checkedListBox1.Items.Add("星期六");
checkedListBox1.Items.Add("星期日");
//注意:下面内容是初始化时候,首先填充满左侧的数组weekleft[]
weekleft = new string[checkedListBox1.Items.Count];
for (int i = 0; i < weekleft.Length; i++)
{
weekleft[i] = checkedListBox1.Items[i].ToString();
}
//通过设置checkedListBox的CheckOnClick为true,可以使得单选一次既可以勾选一行信息。
checkedListBox1.CheckOnClick = true;
checkedListBox2.CheckOnClick = true;
checkedListBox3.CheckOnClick = true;
}
/// <summary>
/// 移至右侧部分项
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
//建立两个列表,分别保存左右的勾选信息
List<string> ListLeft = new List<string>();
List<string> ListRight = new List<string>();
//通过for循环,遍历左侧行信息
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
//如果左侧第i行信息包含在勾选的集合之内
if (checkedListBox1.CheckedItems.Contains(checkedListBox1.Items[i]))
{
Array.Clear(weekleft, i, 1);
//表示从左侧weekleft数组中清除第i个值,向后1位。目的是将已经填充满的左侧数组信息开始减少。
ListRight.Add(checkedListBox1.Items[i].ToString());
//将移除的项目添加到临时列表ListRight,代表右侧列表。
}
}
//判断左侧是否是第一次点击;
if (LeftClicknum == 0)
{
//如果是则将右侧列表转换成为数组后赋值给右侧数组weekright[]
weekright = ListRight.ToArray();
//左侧点击次数累加1
LeftClicknum++;
}
else
{
//如果不是第一次点击,根据右侧checkedListBox内已经填充的个数进行循环
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
ListRight.Add(checkedListBox2.Items[i].ToString());
//先把右侧已经添加的行信息注入到右侧列表ListRight内
}
weekright = ListRight.ToArray();
//然后将右侧列表ListRight转换成为数组后赋值给右侧数组weekright[]
}
//建立列表缓冲保存
foreach (string s in weekleft)
{
if (!string.IsNullOrEmpty(s)) ListLeft.Add(s);//遍历左侧数组,添加左侧列表
}
weekleft = ListLeft.ToArray();
//将左侧列表转化成为数组后再复制给左侧数组。
//注意:由于C#中没有动态数组的概念,因此采取这样的方式改变数组的长度和内容。
checkedListBox1.Items.Clear();
checkedListBox2.Items.Clear();
//将左右两侧的checkedListBox全部清空,准备将数组信息开始填充
for (int i = 0; i < weekleft.Length; i++)
{
checkedListBox1.Items.Add(weekleft[i].ToString());
}
for (int j = 0; j < weekright.Length; j++)
{
checkedListBox2.Items.Add(weekright[j].ToString());
}
}
………
|
本文出自 “熊猫写程序” 博客,转载请与作者联系!