0

Hi,
In my page their is one combobox and one listbox. When i select an item in combobox then selected item added to listbox. Combobox has ispostback property to true.
Now my question is,
how to check item is already present in listbox on combobox selected item changed event.


Thanks in advance

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by bhagawatshinde
0

The bottom line is to compare strings.Take the selected item in the combo box and compare it with all the strings in the list box.I am guessing you're conversant with programming in asp.net i.e vb.net or c#.I'll give u a code snippet in vb.net:

'assuming combo box=cbocombo and list box=lstlist
'this code runs in the combo box selectedindex changed event

Dim selectedstr as string=cbocombo.selecteditem.tostring

For i as integer= 0 to lstlist.items.count-1

Next
0

The bottom line is to compare strings.Take the selected item in the combo box and compare it with all the strings in the list box.I am guessing you're conversant with programming in asp.net i.e vb.net or c#.I'll give u a code snippet in vb.net:

'assuming combo box=cbocombo and list box=lstlist
'this code runs in the combo box selectedindex changed event

Dim selectedstr as string=cbocombo.selecteditem.tostring

For i as integer= 0 to lstlist.items.count-1
   if lstlist.items(i).tostring=selectedstr then
       exit sub      
   Then

Next

lstlist.items.add(selectedstr)

.

Its something close to this but i'm not sure on the syntax for listbox methods the IDE
can help out hopefully

0

there'a a typo in the code the if in the for loop should be closed by and end if not a then

0

Thanks sarama2030 for your solution i have just modified it. It works fine now thanks once again.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Boolean ischeck = false;
        string selectedstr = DropDownList1.SelectedValue;
        if (ListBox1.Items.Count > 0)
        {
            for (int i = 0; (i <= (ListBox1.Items.Count - 1)); i++)
            {
                if ((ListBox1.Items[i].ToString() == selectedstr))
                {
                    ischeck = true;
                }                              
            }
        }
        else
        {
            ischeck = true;
            ListBox1.Items.Add(selectedstr);
        }
        if (ischeck == true)
        {
            return;
        }
        else
        {
            ListBox1.Items.Add(selectedstr);
        }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.