Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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:

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


where lstContracts is the listbox

c# code i wrote

C#
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
   }
Posted
Updated 19-Apr-13 4:33am
v2
Comments
Richard C Bishop 19-Apr-13 9:54am    
If you have working VB code, just use a converter to have it done for you if you are not sure on the syntax for C#.

http://www.developerfusion.com/tools/convert/csharp-to-vb/
anupama962010 19-Apr-13 10:01am    
When i convert in the above website it is giving me a syntax error

An error occured converting your code, probably due to a syntax error:
-- line 1 col 3: EOF expected

Richard C Bishop 19-Apr-13 10:04am    
Your VB code is wrong. You cannot exit sub inside an if statement.
anupama962010 19-Apr-13 10:09am    
but it is currently working in vb like that.Even if i try and convert like below then also i am getting the same error
If lstContracts.ListIndex = -1 Then
MsgBox "This Client does not have any groups associated with it.", vbCritical, "No Groups"
txtGroupNum = ""
End If
Richard C Bishop 19-Apr-13 10:13am    
Ok, well vbCritical is definitely a problem. You need to add & around it to add to the string.

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:
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[^]

Try this:
C#
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");

        }
    }
}
 
Share this answer
 
v3
Use the following to achieve your goal

C#
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.....
 
Share this answer
 
Can i use it as below?

C#
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;
   }
 
Share this answer
 
Comments
Richard C Bishop 19-Apr-13 11:07am    
Be careful not to post questions or comments as solutions to your own thread.
CHill60 19-Apr-13 11:11am    
You're going to get one message per checked box ... are you sure that's what you really want? Why not focus on getting the code working with a ListBox (as per your own comment "i have to use the same functionality in c# also")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900