选中的列表框值 [英] Checked List box value

查看:120
本文介绍了选中的列表框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





根据我在加载列表框后第一次的要求,我正在检查是否在列表框中选中了这些项目。在vb代码默认情况下,如果我没有从列表框中选择任何内容,ListIndex值为0.



但在c#代码中如何编写相同的代码。我写的代码下面提到的是无效的。



我在VB6中使用ListBox控件



和CheckedListBox是用于c#



vb6代码:



Hi,

As per my requirement for the first time after the listbox is loaded i am checking whether the items are selected in the listbox. In vb code by default if i am not selecting anything from listbox ListIndex value is 0.

but in c# code how to write the same code.the code i have written is mentioned below which is not working.

I am using ListBox control in VB6

and CheckedListBox is being used in c#

vb6 code:

If lstContracts.ListIndex = -1 Then
  MsgBox "This Client does not have any groups associated with it.", vbCritical, "No Groups"
  txtGroupNum = ""
  Exit Sub
End If





其中lstContracts是列表框



c#code我写的





where lstContracts is the listbox

c# code i wrote

if (ListBoxHelper.GetSelectedIndex(lstContracts) == -1)
   {
      MessageBox.Show("This Client does not have any groups associated with it.", "No Groups", MessageBoxButtons.OK, MessageBoxIcon.Error);
      txtGroupNum.Text = "";
      if (KeyAscii == 0)
         {
            eventArgs.Handled = true;
         }
      return
   }

推荐答案

请按照以下链接: CheckedListBox Class [ ^ ]

注意: CheckedListBox。 CheckedItems属性 [ ^ ]在CheckedListBox中存储已检查项目的集合。您可以使用许多 CheckedListBox方法 [ ^ 也是。

$ / b


因为CheckedListBox显示一个ListBox,其中每个项目的左侧都显示一个复选框,您可以使用ListBox属性,如:SelectedItems,SelectedIndicies。 br />
Please, follow the this link: CheckedListBox Class[^]
Pay attention on: CheckedListBox.CheckedItems property[^] which stores the collection of checked items in CheckedListBox. You can use many CheckedListBox methods[^] too.


Because CheckedListBox displays a ListBox in which a check box is displayed to the left of each item, you can use ListBox properties, like: SelectedItems, SelectedIndicies.
Quote:

ListBox类提供了许多引用所选项的方法。 不使用SelectedItems属性来获取单选ListBox中当前选定的项,而是可以使用SelectedItem属性。如果要获取当前在选定项中选择的项的索引位置。 ListBox而不是项本​​身使用SelectedIndex属性。 此外,如果要获取多选ListBox中所有选定项的索引位置,可以使用SelectedIndices属性。

The ListBox class provides a number of ways to reference selected items. Instead of using the SelectedItems property to obtain the currently selected item in a single-selection ListBox, you can use the SelectedItem property. If you want to obtain the index position of an item that is currently selected in the ListBox, instead of the item itself, use the SelectedIndex property. In addition, you can use the SelectedIndices property if you want to obtain the index positions of all selected items in a multiple-selection ListBox.





SelectedItems [ ^ ]

SelectedIndex [ ^ ]



试试这个:



SelectedItems[^]
SelectedIndex[^]

Try this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 30; i++)
            {
                this.checkedListBox1.Items.Add(String.Format("Item_{0}", i), CheckState.Unchecked);
            
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckedListBox clb = this.checkedListBox1;

            string s = String.Format("Selected items count: {0}, currently selected index: {1}", clb.SelectedItems.Count, clb.SelectedIndex);
            MessageBox.Show( s,"Message");

        }
    }
}


使用以下方法实现目标



Use the following to achieve your goal

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           if (checkedListBox1.CheckedItems.Count == 0)
           {
               MessageBox.Show("This Client does not have any groups associated with it.", "No Groups", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }









希望这对你有帮助.....





Hope this helps you.....


我能用它如下吗?



Can i use it as below?

foreach (int indexChecked in lstContracts.CheckedIndices)
   {
     MessageBox.Show("This Client does not have any groups associated with it.", "No Groups", MessageBoxButtons.OK, MessageBoxIcon.Error);
     txtGroupNum.Text = "";
     if (KeyAscii == 0)
        {
           eventArgs.Handled = true;
        }
      return;
   }


这篇关于选中的列表框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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