Read all item from listbox in windows application
I have two listbox in windows Form c#, the first one has all valid rooms at this time & the 2nd has the current rooms reserved to the Guest, & the user can change this data by transfer between this listboxex.
as开发者_开发百科 example:
list1: 1d ,5d ,6d , 1r, 12r
list2: 2d, 4d
the user can delete 2d and put instead of it 6d.
what I need how can I read all data from list2 after user update it, & use this to update my table in sql server.
To copy a selected item from one list box to another you could use the following code. Ensure that the SelectionMode for the listbox is Single. This bit of code reads the selected item, adds it to the other ListBox and removes it from the current ListBox. You do not need to cast it to a string as there is no reason to read the actual data. This also allows you to have complex objects as listBoxItems.
object obj = listBox1.SelectedItem;
listBox2.Items.Add(obj);
listBox1.Items.Remove(obj);
If you link this to left/right buttons, you can ensure that the user is not able to duplicate rooms or add room numbers which are outside the scope.
Try code below:
string TimeslotItems = "";
foreach (ListItem item in listBox2.Items)
{
TimeslotItems += item.ToString() + ","; // /n to print each item on new line or you omit /n to print text on same line
}
There is no 'item added' event on ListBox. When you use a BindingList as your datasource you can register events to the BindingList itself and respond to those. If you want an item added event on the List itself look at Item added events question for ListBox
To iterate over your list items:
foreach (string s in listBox1.Items)
{
string name = s;
}
In regards to your database transfer, what way do you want to go? Using Entity Framework? Direct SQL execution?
精彩评论