Application Settings | System.Collections.Specialized.StringCollection Only Saves One Value - c#

In the application settings I have a setting called Locations and the type is a System.Collections.Specialized.StringCollection.
I also have a textbox and a button that are supposed to add values to the StringCollection.
After entering the value into the textbox, the value is supposed to be added into a listbox and into the StringCollection. The code for adding a new value to the listbox and StringCollection is as follow:
string newPrintLocation = tbAddPrintLocation.Text;
if (lbPrintLocations.Items.Contains(newPrintLocation) == false)
{
lbPrintLocations.Items.Add(newPrintLocation);
Properties.Settings.Default.Locations.Add(newPrintLocation);
cbPrintLocation.Items.Add(newPrintLocation);
tbAddPrintLocation.Clear();
}
The goal is when the user restarts the form the values are added back into the listbox. Currently this is the code that I use to try to achieve that:
foreach (var item in Properties.Settings.Default.Locations)
{
lbPrintLocations.Items.Add(item);
}
The problem is that only the first value gets added to the listbox but none of the rest. At this point I have no idea what I am doing wrong. So I was wondering if someone could help me out or point me to the right direction.
All help is greatly appreciated.
Cheers,
Quartermain.

Related

How do I properly select a row in ObjectListView?

I'm using ObjectListView with C# and .Net 4.0. I wrote code that reloads the listview and then re-selects the last selected index.
The re-selection code is quite simple:
olvListView.SelectedIndex = i;
This appears to work, because the item is selected. However, if I then click the up or down arrow, the selection jumps up to the second row (no matter what row I selected), suggesting that the selection was actually set on the first row, no matter what was the value of i.
What am I doing wrong here?
The underlying ListView Control distinguishes between 'selection' and 'focus'.
olvListView.SelectedIndex = i; changes the selection but not the focus. But the focused row is the one that the keyboard input relates to.
Either change the focus the as well
olvListView.SelectedIndex = i;
olvListView.FocusedItem = olvListView.SelectedItems[0];
or call
olvListView.SelectObject(aModelObject);
The second solution would be the preferred way to select an item when working with OLV, however you say you "wrote code that reloads the listview", so the reference to the original item is probably different. Maybe you should just refresh the items that changed, instead of reloading everything. That way you could preserve the selection.
Example if your olv have datasource from "class_z.list" and foreach have only one result.
foreach(class_z a in class_z.list.Where(x=>x.id==id_value))
{
olv.SelectedObject = z;
}

DropDownList Showing First Item Empty

Every time this Dropdown is showing first Item Blank
ComboBox cb;
List<string> namesCollection=new List<string>();
namesCollection.Add("---- Select ----");
namesCollection.Add("ABC1");
namesCollection.Add("ABC2");
namesCollection.Add("ABC3");
namesCollection.Add("ABC4");
foreach(string pname in namesCollection)
cb.Items.Add(pname);
Does anyone have solution for this ?
You appear to be defining the ComboBox right there in your code, so I'll assume it's actually displayed somewhere in your form / window.
It's normal for the ComboBox to display a blank line initially.
Specify the item you want to display, immediately after populating the ComboBox with data:
cb.SelectedIndex = 0;
You probably have a line feed in your items collection. Hard to see. Adding a cb.Items.Clear() before populating is probably a good idea anyway and will get rid of the problem, if you can't locate it.

Select a row in listview

I am web developer , working on a part of my project developed in WinForms. So my question could be a basic one. Try to bear with it.
I have two list views on my page and a remove button that works for both.
Problems.
I am not able to select a row in both the list view when I run my program, may be some property needed for it?
If I am able to select the row I want to detect which list view item has been selected, so how would I do that?
I have three columns and have bound the data by using the code below.
listView1.Columns.Add("ID",20);
listView1.Columns.Add("Name",40);
listView1.Columns.Add("Mobile",40);
foreach (var item in dataList)
{
newItem = new ListViewItem();
newItem.SubItems.Add(item.ID.ToString());
newItem.SubItems.Add(item.Name);
newItem.SubItems.Add(item.Mobile.ToString());
listView1.Items.Add(newItem);
}
but the ID column is left blank and the data starts to bind in these sense.
ID Name Mobile
1 abc
2 xyz
So how do I properly show the data?
Lastly I want to use my ID column to delete the data. So if I give width=0, is this the best way to hide a column?
See ListView.FullRowSelect property.
See ListView.SelectedItems property. Note, that by default ListView allows multiselection.
Set item text via constructor: newItem = new ListViewItem(item.ID.ToString());, then add rest of subitems (except of item.ID).
If you want to delete the column, just remove it from the columns collection.
Set
listView1.Items[index].Selected=true;

Listbox item stay focused after selection

I don't know if the title express what I want. I have a ListBox in WPF where I generate many elements. When I click on a element while still generating I want my selected item to not move down the list, so I cannot see it anymore, I want to stay in the exact position where I click on it.
If this is possible, can someone point some ideas on how to do it in C#?
Thanks.
Assuming that this is even a good idea and that you are using winforms
Step 1:
Determine the index of the selected item in the source.
Step 2:
When your adding items to the ListBox split the ListBox at the index where the item previously was insert the item at that point, then add on the remainder of the items, while making sure that you've removed the item if it is now elsewhere in the list.
Code:
//Let's assume that you know how to get the position of the item when it is clicked and save the
//item to a variable called OriginalItem
public void PutTheItemInTheSameSpot()
{
var listboxitems = (List<Integer>)YourListBox.DataSource;
var originalClikedItem = OriginalItem;
var topPart = new List<Integer>();
for (i = 0; i < itemPosition; i++)
{
topPart.Add(listboxItems[i]);
}
topPart.Add(originalClickedItem);
var bottomPart = listboxitems.Remove(toppart);
YourListBox.DataSource = toppart.AddRange(bottomPart);
}
Saw your edit about it being WPF
The could should work in idea.
Just a thought: you could try having your view respond to an event whenever an item is added to your ListBox. In the event handler, you could force the selected item to scroll into view presumably keeping it in the current "viewable" position:
listBox.ScrollIntoView(listBox.SelectedItem);
I've never tried this before so it may or may not produce the desired affect?

Tick correct checkbox if 'checked' within the database

I am using c# .net.
Thanks in advance for any help.
I have searched the web, but don't think I am using the right words, as nothing being returned is really helping.
I have a 'edit' section within my web-form which allows the user to tick (using a checklist) certain information.
For example:
• Receive newsletters
• Receive phone calls etc
The checklist is populated from a database table called Requirements.
When the user ticks a certain checkbox this information should be stored within another table userRequirement.
I can display all the requirements (from Requirements) by looping through and adding another item:
foreach (tblRequirement singleRequirement in viewAllRequirement)
{
requirementCheckBoxList.Items.Add(new ListItem(singleRequirement.requirementName,singleRequirement.rrequirementID.ToString(),true));
}
However how do I then loop through the userRequirement and automatical tick the right checkboxes?
For Example:
User selects ‘Receive Newsletters’
checkbox and presses the ‘Update’
button.
This is then stored within
the userRequirement table along with
the users ID
If the user wants to
edit their details again, they can
do. They are taken to the ‘edit’
page. Here the ‘Receive Newslettlers’
should already be selected.
Should I be using a if statement? If so can anyone help by providing an example?
Thanks
Clare
You can loop through all the items in the CheckBoxList using a foreach loop like so:
foreach (ListItem item in requirementCheckBoxLis.Items)
{
item.Selected = true; // This sets the item to be Checked
}
You can then set whether an item is checked by setting its Selected property to true. Does that help any?
In your loop, you can select the proper items as they're being entered into the CheckBoxList. Might look something like this (I don't know how your tblRequirement object works):
foreach (tblRequirement singleRequirement in viewAllRequirement)
{
ListItem newItem = new ListItem(singleRequirement.requirementName,singleRequirement.rrequirementID.ToString(),true));
//If item should be checked
if(singleRequirement.Checked)
newItem.Selected = true;
requirementCheckBoxList.Items.Add(newItem);
}

Categories

Resources