c# listbox numerical order of items - c#

Hello I'm searching throught internet for hours but still I can not find the answer. (or I just don't know how to ask)
I need a simple numerical order of items in listbox fe. I am adding every 3rd items to listbox from txt file.
When I have this in txt file:
Acc 4
name4
pass4
Acc 5
name5
pass5
I'll add to listbox only Acc4 and Acc5
And now when I select some item in listbox I want to know which one is selected but I don't want fe.: Acc6 (is selected) but I want to know Acc6is 3nd one in listbox (then order is 2)
PS: sorry for my end and thx for help

If you are looking for the index of the selected item in a listbox try
{listBoxName}.SelectedIndex
This will give you the index (0 based) of a selected item. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.selectedindex?view=netframework-4.7.2
If you want the item try
{listBoxName}.SelectedItem
This gives the currently selected item https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.selecteditem?view=netframework-4.7.2

Related

Place list item at specific position in CheckBox List

I have two checkboxlist
checkboxlist1 has 5 items and checkboxlist2 has 3 items
During a button event click, i need to move the selected items from checkboxlist1 to checkboxlist2
but i need to make sure the items selected from checkboxlist1 needs to be placed above or below an item checked in checkboxlist2
how to add items at a specific position in checkboxlist2 and move the remaining items down
Below code will actually put the item at the end
checkboxlist2.Items.Add(listitemselectedfromcheckboxlist1)
try this
checkboxlist2.Items.Insert(index , listitemselectedfromcheckboxlist1)
Have you tried manipulating Items by using Insert method overload ?
I mean
checkboxlist2.Items.Insert(index:3,item);
How about using this for multiple selections,
checkboxlist1.SelectedIndices.Cast<int>().ToList().ForEach(x =>
{
checkboxlist2.Items.Insert(x, checkboxlist1.Items[x]);
});
Hope this helps...

c# listview: Get selected subitem

I have a ListView with 9 columns and an unknown number of rows.
In two different columns, the user shall be able to click (or double-click) on a subitem and I need to know which of these two subitems in this row was chosen.
So I need to know either which SubItem of a ListView has been chosen or in which column it was.
Thank you!
Tom

Split a listview into multiple columns

I am populating a ListView with a DataTable from the code behind. The problem is, that the ListView is becoming too long with too many items.
I am wondering, how can I make this:
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Into this:
Item 1 Item 4
Item 2 Item 5
Item 3 Item 6
Maybe I'm overlooking something, but I can't seem to find the answer.
Please not that I want to keep it in one ListView, so I'm not looking to split the items and put them into multiple ListViews.
Solution: Use a DataList in stead of a ListView. See Tim's answer.
Use a DataList instead. It has a RepeatDirection and RepeatColumns properties. The former would be RepeatDirection.Vertical and the latter 2 in your case.
Gets or sets the number of columns to display in the DataList control.

Assigning the selected values of checkboxlist items to listbox

I am developing a small business application to display the report retrieved from database and I have a checkboxlist consisting of four checkboxes, also a combobox. Now, I am retrieving the required info from database to combobox with 4 items while on page load, which is ok. Not only this, but also retrieving the checkboxlist DataTextField on the checkboxes from database,which is also ok.
As for my question on this issue, lets assume that I have a combobox with 2 items, name and age and also 4 checkboxes named with George, Michael, 23, 33.
Here, 23 and 33 belong to Name(combobox.selectedindex[0]) items of checkboxes and George, Michael belong to the Age item of combobox. Then, I want to display on the listbox that when clicking Name item of combobox, checkboxes of Michael and George come to the screen as checked automatically and when clicking the button, those selected values of checkboxes are displayed on the listbox for both nMme and Age items of combobox in the same logic.
I tried to make this several times, but fail to achieve this goal. How to do this, can you please lead the way to me on my this issue?
Thank a a lot for replying.
The simple way would be to add Name or Age as checkBoxListItem.Tag to the corresponding values and everytime you change selection in the combobox you iterate through all the checkBoxItems and chack the ones with the corresponding tag and uncheck the others.

How To Access Repeater's Items Randomly

I have a repeater with an ItemTemplate contains a CheckBox, TextBox
My repeater represents a check list plus a TextBox to put a comment to each item I select from the list.
Now, I've a page for editing an existing shopping cart in my database and it uses this repeater to enable the user to update his list. So I need to update the List with the previous selected values from the database. I can't think of a simple logic or way to access only the repeater's items that are on the previous list ...I'm thinking about trying to access each item that's in my database only instead of looping and looping.
I know It's really confusing so I'll just put it in a more illustrative way:
Database View
Items
=======================
ID Name
--- -----
1 Banana
2 Apple
3 Strawberry
4 Orange
ShopCart_Items
========================================
ItemID ItemName CartID Value
------- --------- ------- ------
2 Apple 1 1
4 Orange 1 2
2 Apple 2 2
PageView
First the repeater is populated with all the items that I have in my 'Items' table and no boxes check or anything.
Then I choose a specific cart (say cartId = 1) to edit, and here's what I'm really confused on how to do:
Now the repeater will have like 15-20 items, so I need to access just the items that the user choose on the current cart (cartId = 1) that the user want to edit, so he could know what did he choose and start choose new values, check/uncheck CheckBoxes, etc.
I'm sorry for all this long question, but I'm really confused should/can I access each item directly or what logic do you advice me to use ? ..Thanks =)
I believe that you have mix two different entities in a one repeater - an orders and order items.
In my opinion the better approach is to place two repeaters onto the page: the first one for displaying orders and the second one for displaying order items for the selected order. This way you may use simple SqlDataSource controls for both repeaters and add a ControlParameter to the datasource used for retrieving order items depending on current order selection in the first repeater.

Categories

Resources