473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Deleting a given item in listview and in combobox

Hey there,
I have a listview with 2 columns that puts the text taken from a textbox in the first column and the value of a radiobutton in the second one. Upon pressing the "add" button the text and the value appear in the listview, and the text is also added to a combobox.

Expand|Select|Wrap|Line Numbers
  1. ListViewItem lvi3 = new ListViewItem(textBox3.Text);
  2. //now adding the radiobutton value
  3. lvi3.SubItems.Add(textfromradiobutton);
  4. //refreshing the list
  5. listView3.Items.Add(lvi3);
  6. //adding the text from textbox to the combobox
  7. comboBox1.Items.Add(textBox3.Text);
I've also a contextmenustrip which is attached to the table, and upon pressing the "delete row" button in it, it deletes a whole row from the listview.

Expand|Select|Wrap|Line Numbers
  1.         private void usuńRekordToolStripMenuItem2_Click(object sender, EventArgs e)
  2.         {
  3.             if (listView3.SelectedItems.Count != 0)
  4.             {
  5.                 foreach (ListViewItem lvi3 in listView3.SelectedItems)
  6.                     lvi3.Remove();
  7.             }
  8.         }
My problem is that even if I remove a given text from the listview, it stays in the combobox. How can I implement such a feature that when a given row is deleted from the listview, the same values disappear from the combobox?
Feb 18 '12 #1
3 2904
I have come across this issue many times before. The way I have solved it is that I have a separate list that is full of index numbers.

It ends up being something like this
Expand|Select|Wrap|Line Numbers
  1. List
  2. Index    Contents
  3. 0        4
  4. 1        2
  5. 2        3
  6. 3        1
  7. 4        0
  8. 5
  9. ...
  10.  
The index column is in the same order as the listbox so that the list's index = ListBox.SelectedIndex and the contents at that index is the corresponding SelectedIndex of the combobox.

The code is then implemented as follows
Expand|Select|Wrap|Line Numbers
  1. -Delete Button's OnClick Event-
  2. {
  3.     int ind = ListBox.SelectedIndex;
  4.     ListBox.Remove(ind);
  5.     ComboBox.Remove(list(ind));
  6.     (for loop that adjusts index numbers higher than ind, i.e. if ind=2, any index higher than that needs to be subtracted 1.)
  7. }
It is a nasty way of doing it, but it works.
Feb 21 '12 #2
Thanks for the response, I've accomplished the same effect in another way, though.

Expand|Select|Wrap|Line Numbers
  1. if (listView3.SelectedItems.Count != 0)
  2.             {
  3.                 foreach (ListViewItem lvi3 in listView3.SelectedItems)
  4.                 {  
  5.                     string selection = lvi3.Text;
  6.  
  7.                     comboBox1.Items.Remove(selection);
  8.                     lvi3.Remove();
  9.  
  10.                 }
  11.  
  12.  
  13.  
  14.  
  15.             }
Feb 21 '12 #3
cheers for that... that will be very useful.
Feb 26 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: koti | last post by:
hi i have written some code for selecting an item in combobox dropdown list which is in datagrid columm. by scrolling the mouse we select any item from the list. but by pressing the down key...
2
by: Brett Romero | last post by:
After I remove all items from a combobox, the last selected item remains in the display area. I've tried: mycbox.SelectedText = string.Empty; and mycbox.refresh(); How do I clear the...
0
by: marcobx | last post by:
I have a ComboBox and a List of objects to popolate items. I use the DataSource property and not the List collection: List<foolst = new List<foo>; //add elements into the list //......
7
by: crsqn91l | last post by:
Hi Friends... i error while i m adding new item "Create New" into Combobox which is already binded by one datasource using datatable with valuemember. plz help me in solving how to add new item...
4
by: =?Utf-8?B?bWFydGluMQ==?= | last post by:
Hi, All, when starting run app, how to let user add item/edit item to comboBox on the Form, Thanks, Martin
2
by: billa856 | last post by:
Hi, I have one projrct in MS Access. I have one form in which there is one Combobx(PalletNo_combo) and one Textbox(ShippedQuantity). Now when I select an item from Combobox then it will...
1
by: vishnu2008 | last post by:
Hi, I have a combobox with some comments in it .when we select any one item in combobox all the items should be displayed in textbox. please help me with code. Thanks
3
by: luiggye | last post by:
Hi I have a LISTVIEW where every item have 6 subitems (columns). lstBrowse.Columns.Add("Col1", 150, HorizontalAlignment.Left) lstBrowse.Columns.Add("Col2", 150, HorizontalAlignment.Left)...
13
by: saifulhaque | last post by:
Hi dear I am working with small program. In my project I have Database field S_NO (serial no).I want to rearrange serial no field after deleting an item from that table like( 1,2,3... etc).How can...
0
by: yousof | last post by:
i have web page that contian combobox filled from table If CtvAct.GetRecords("Fill_ActivityTb") = True Then AcivityCombo.DataSource =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.