Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI Friends,Iamin a big problem and stuck for3 days without finding a solution for my issue,please let me know a solution for the below query please email me removed email

I have a list of software in combobox 4 and while form load a list of software will be listed in listbox1,so while updating if i click on the software list in combobox4 its getting duplicated in the listbox,the issue is that i cant cross check wether the selected combobox values are already in the list.The listbox is entered in to the database using comma seperation and while listing in the listbox it gets split and shows as two different values.

C#
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox4.Text != "")
    {
        string sc = comboBox4.SelectedItem.ToString();

        listBox1.Items.Remove("");
        listBox1.Items.Add(comboBox4.SelectedItem);
        listBox1.Items.Remove("");
        comboBox4.Items.Remove(comboBox4.SelectedItem);
    }
}

C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string li = listBox1.Text;
            comboBox4.Items.Remove("");
            comboBox4.Items.Add(li);
            comboBox4.Items.Remove("");
            listBox1.Items.Remove("");
            listBox1.Items.Remove(listBox1.SelectedItem);
         }

//values for Listbox1
C#
obj.cn.Open();
SqlCommand cmd2 = new SqlCommand("Select software from specs where mfcitno='" + label2.Text + "'", obj.cn);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
    ard = dr2["software"].ToString();
}
dr2.Close();
obj.cn.Close();
if (ard != null)
{
    string sofaa1 = ard.ToString();
    string[] arr1 = sofaa1.Split(',');
    foreach (string item in arr1)
    {
        listBox1.Items.Add(item);
    }
 }
Posted
Updated 17-Aug-12 0:59am
v2
Comments
Malli_S 17-Aug-12 7:05am    
foreach (string item in arr1)
{
listBox1.Items.Remove(item);
listBox1.Items.Add(item);
}

I didn't test it, but you can use this logic.
sarsamma 17-Aug-12 7:11am    
this will not let duplication as the whole items will be in combobox and some of the items will only be listed,so while adding values from combobox there should not be any duplicate values.

The above criteria shown is just to load values from database to listbox.

I need something that when we add values from combobox it should check the value in listbox wether its already added or not if not then the value should add from combobx to listbox and update.

Thanks for the comment seeking a reply for the same.
Rock (Multithreaded) 17-Aug-12 9:39am    
Ok sarsamma,

string sofaa1 = ard.ToString();
string[] arr1 = sofaa1.Split(',');
foreach (string item in arr1)
{
if(!listBox1.Items.Contains(item))// For remove list duplicacy
{
listBox1.Items.Add(item);
}
}

So the problem is that items moved from the combobox to the listbox may be duplicates of items already in the listbox. (Right?)
You need to keep track of which items are already in the listbox, independent of the database.
C#
// Add class field:
private HashSet<string> SelectedSoftware = new HashSet<string>();

// ... initialize the listbox
obj.cn.Open();
using (SqlCommand cmd2 = new SqlCommand("Select software from specs where mfcitno='" + label2.Text + "'", obj.cn))
{
  using (SqlDataReader dr2 = cmd2.ExecuteReader())
  {
    while (dr2.Read())
    {
      ard = dr2["software"].ToString();
    }
  }
}
obj.cn.Close();
// ard *should* be declared as string
if (ard != null)
{
  //line below is unnecessary, ard already is string from .ToString() above
  //string sofaa1 = ard.ToString(); 
  string[] arr1 = ard.Split(',');
  foreach (string item in arr1)
  {
    listBox1.Items.Add(item);
    SelectedSoftware.Add(item);
  }
}

Then when selecting from the combobox, check if the item is already in SelectedSoftware, and if so, don't add it again. And keep SelectedSoftware updated:
C#
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
  if (!string.IsNullOrEmpty(comboBox4.Text))
  {
    string sc = comboBox4.SelectedItem.ToString();
    if (!SelectedSoftware.Contains(sc))
    {
      //listBox1.Items.Remove("");  // WHY?
      listBox1.Items.Add(sc);
      SelectedSoftware.Add(sc);
      //listBox1.Items.Remove("");  // WHY?
      comboBox4.Items.Remove(comboBox4.SelectedItem);
    }
  }
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  string li = listBox1.Text;
  //comboBox4.Items.Remove("");  // WHY?
  comboBox4.Items.Add(li);
  //comboBox4.Items.Remove("");  // WHY?
  //listBox1.Items.Remove("");   // WHY?
  listBox1.Items.Remove(li);
  SelectedSoftware.Remove(li);
}


Even better, if possible, would be to remove from the combobox the elements that are already in the listbox, as soon as the listbox is loaded, so they cannot be added to the listbox if they are already there. (Imagine the case where the listbox is initially empty and the combobox has all of the possible selections. At any point in the sequence of selections from the combobox or listbox, all of the possible selections are in one or the other control. This is what your code already does, it just doesn't start out that way.) If the user can enter a new value in the combobox, then you'll need to ensure it isn't a duplicate, as above.

Finally, from your code it appears that simply changing the selected items in the listbox and combobox causes the items to be moved from one to the other. It seems like a better user experience to have a pair of buttons that move the selected items from one list to the other.
 
Share this answer
 
Comments
Matt T Heffron 20-Aug-12 13:17pm    
So...Does this do what you want?
I solved this myself just taking an option of contain.Before that we should make sure that there is no space while trimming down to an array.

Please use this code for triming a comma seperated string to form an array.

C#
string[] spli = Regex.Split(soft1.Trim(), @"\s*[,]\s*");

this will remove the space from string after splitting it to an array.

SQL
if (!listBox1.Items.Contains(searchst))
               {

do the coding here
}
 
Share this answer
 
Why don't you use FindString[^] before inserting the item?


Sample code from the above specified link.

C#
private void FindAllOfMyString(string searchString)
{
   // Set the SelectionMode property of the ListBox to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;

   // Set our intial index variable to -1.
   int x =-1;
   // If the search string is empty exit.
   if (searchString.Length != 0)
   {
      // Loop through and find each item that matches the search string.
      do
      {
         // Retrieve the item based on the previous index found. Starts with -1 which searches start.
         x = listBox1.FindString(searchString, x);
         // If no item is found that matches exit.
         if (x != -1)
         {
            // Since the FindString loops infinitely, determine if we found first item again and exit.
            if (listBox1.SelectedIndices.Count > 0)
            {
               if(x == listBox1.SelectedIndices[0])
                  return;
            }
            // Select the item in the ListBox once it is found.
            listBox1.SetSelected(x,true);
         }

      }while(x != -1);
   }
}
 
Share this answer
 
v2
Comments
sarsamma 17-Aug-12 7:13am    
How can i use the find string actually iam new to c# and getting developed
Malli_S 17-Aug-12 7:35am    
The link also has the sample code. :)
Malli_S 17-Aug-12 7:37am    
I've pasted the sample code from the above link. Apply that to your code.
sarsamma 17-Aug-12 8:03am    
no it doesnt rid any how,

could you please explain is the searchstring is the value i selected from combobox

please specify
Try This :

ArrayList ar = new ArrayList();


DataSet ds1 = new DataSet();
SqlDataAdapter myda = new SqlDataAdapter("select Query for bind into the Combobox", Connection Object);
myda.Fill(ds1);
for (int h = 0; h < ds1.Tables[0].Rows.Count; h++)
{
      if (!ar.Contains(ds1.Tables[0].Rows[h][0].ToString()))
      {
             ar.Add(Convert.ToString(ds1.Tables[0].Rows[h][0].ToString()));
      }
}
foreach(string s in ar)
        {
            Listbox1.Items.Add(s);
        }  


Hope this will help you to solve ur Problemmmm
 
Share this answer
 
v2
C#
dr2.Close();
            obj.cn.Close();
            if (ard != null)
            {
                string sofaa1 = ard.ToString();
                string[] arr1 = sofaa1.Split(',');

                listBox1.Items.clear(); //Clear all old items first

                foreach (string item in arr1)
                {
                    listBox1.Items.Add(item);
                }
             }
 
Share this answer
 
v2
Comments
sarsamma 17-Aug-12 7:17am    
No Arun this is not the solution!!!

this will not let duplication as the whole items will be in combobox and some of the items will only be listed,so while adding values from combobox there should not be any duplicate values.

The above criteria shown is just to load values from database to listbox.

I need something that when we add values from combobox it should check the value in listbox wether its already added or not if not then the value should add from combobx to listbox and update.

Thanks for the comment seeking a reply for the same.
While adding the record in the combobox, bind the combobox items with key value pair.
when entering the item in the list box after selecting the item from the conbobox ,check whether the value of the item that is being entered already exists or not.
if it exists then show an error message saying that the item already exists thus not allowing the duplicate records on entering the listbox.
 
Share this answer
 
Comments
CHill60 25-Jul-13 14:04pm    
"check whether the value of the item that is being entered already exists or not" ... that was the OP's problem a year ago. Some sample code would have helped to explain this better ... e.g. see Solution 5 ...

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