Reputation: 33
I'm developing an universal windows app and when I add an item to a listbox it appears twice with no errors
Here is the code
private async void btnNew_Click(object sender, RoutedEventArgs e)
{
listBox.Items.Add(nameBox.Text);
string type = comboBox.SelectedItem.ToString();
URL url = new URL();
url.type = type;
url.name = nameBox.Text;
url.info = infoBox.Text;
url.url = urlBox.Text;
url.isProtected = isProtectedSwitch.IsOn;
await url.saveToXML();
infoBox.Text = "";
nameBox.Text = "";
urlBox.Text = "";
comboBox.SelectedIndex = -1;
}
And if needed:
public async Task saveToXML()
{
//Directory.CreateDirectory(@"C:\Link");
URL newURL = new URL();
newURL.type = type;
newURL.name = name;
newURL.info = info;
newURL.url = url;
newURL.isProtected = isProtected;
newURL.amountOfClicks = amountOfClicks;
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"Link\" + newURL.name + ".xml", CreationCollisionOption.ReplaceExisting);
Stream fileStream = await file.OpenStreamForWriteAsync();
DataContractSerializer serialize = new DataContractSerializer(typeof(URL));
serialize.WriteObject(fileStream, newURL);
}
Upvotes: 0
Views: 575
Reputation: 65
Clean listbox before you add items.
listBox.Items.Clear();
It is posible, that you double click this button, so code fires twice.
Upvotes: 0
Reputation: 47570
Cannot see any issue in the code you have provided.
Probably you are not clearing exiting listBox.Items
before adding Items. Make sure you listBox.Items.Clear()
prior adding new list of items.
Upvotes: 1