如果选中列表框中的多个项目,如何选择和添加值? [英] How to select and add the values if multiiple items in a checked list box?

查看:106
本文介绍了如果选中列表框中的多个项目,如何选择和添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎在网上到处搜索,并尝试了所有可能的代码我知道但仍然没有成功。如果有人可以指导我,我会很感激吗?以下是我的代码。



I have almost searched everywhere on net as well as tried every possible code i know but still no success..I would appreciate if some one may guide me?Following is my code.

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sum = 0;
            foreach (object itemChecked in checkedListBox1.CheckedItems)
            {
                sum += Int32.Parse(checkedListBox1.SelectedItem.ToString());
            }
}

推荐答案

正如Marcin所提到的,而是使用 SelectedItem 事件,使用 ItemCheck [ ^ ],但我警告你,在ItemCheck事件发生之前,检查状态不会更新。

CheckedListBox事件 [ ^ ]
As Marcin had mentioned, instead using SelectedItem event, use ItemCheck[^], but i'd warn you that the check state is not updated until after the ItemCheck event occurs.
CheckedListBox Events[^]


因为'ItemCheck事件总是(好吧,它应该是:见下面的注释),然后是ValueChanged事件(是的,那不是这很有意义,但这就是它的方式),你可以做到这一点,以确保你的总和计算总是最新的。



这个例子假设你在Form上有一个多行TextBox,'textBox1和一个CheckedListBox',checkedListBox1:
Since an 'ItemCheck Event is always (well, it should be: see note below) followed by a ValueChanged Event (yes, that doesn't make a lot of sense, but that's the way it is), you can do this to make sure your 'sum calculation is always current.

This example assumes you have a multi-line TextBox on a Form, 'textBox1, and a CheckedListBox, 'checkedListBox1:
// flag to determine a change in CheckState of any Item
private bool IsCheckedChanged = false;

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    IsCheckedChanged = true;

    // for debugging only
    textBox1.Text += "change in Item CheckState" + Environment.NewLine;
}

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // for debugging only
    textBox1.Text += "index changed" + Environment.NewLine + Environment.NewLine;
}

private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
    // for debugging only
    textBox1.Text += "value changed" + Environment.NewLine;

    if (IsCheckedChanged)
    {
        int sum = 0;

        foreach (var chkItem in checkedListBox1.CheckedItems)
        {
            sum += Convert.ToInt32(chkItem);
        }

        // for debugging only
        textBox1.Text += "Sum: " + sum.ToString() + Environment.NewLine;

        IsCheckedChanged = false;
    }
}

如果在CheckedListBox中快速单击Items,并检查写入TextBox的内容,您可能会观察到一些不同的实例。事件被解雇了。是的,控件并不完美!



但是,大多数情况下,任何Item的CheckState中的更改都会触发ValueChanged事件,并且您可以评估状态所有CheckedItems都可靠。



在使用本机Windows控件(或第三方控件!)时,我强烈建议您尝试为您感兴趣的事件编写简单的EventHandler,编写一些有意义的输出到控制台或TextBox。学习和理解,执行顺序以及事件的相互依赖性,是真正确保您了解如何有效使用给定控件,而不会产生难以理解的细微错误的唯一方法。 debug。

If click Items rapidly in the CheckedListBox, and examine the content written to the TextBox, you will probably observe some instances where there is an irregularity in the way the various Events are fired. Yep, the Control ain't perfect !

But, most of the time a change in any Item's CheckState will trigger a ValueChanged Event, and you can evaluate the state of all CheckedItems reliably.

In using the native Windows Controls (or third-party Controls !), I strongly suggest you experiment with writing simple EventHandlers for the Events you are interested in that write some meaningful output to the Console or to a TextBox. Learning, and understanding, the order of execution, and inter-dependencies of the Events, is, imho, the only way to really be sure you understand how to use the given Control efficiently, and without creating subtle errors that are hard to understand/debug.


为什么在迭代CheckedItems时使用SelectedItem属性?这将起作用:

Why are you using SelectedItem property while you iterating CheckedItems?? This will work:
int sum = 0;
foreach (object item in checkedListBox1.CheckedItems)
{
    sum += Int32.Parse(item.ToString());
}


这篇关于如果选中列表框中的多个项目,如何选择和添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆