I currently have my code save text from a two text boxes to a text file and load it in the listview. how would i if i pressed a button get it to remove not only the item from the listview but from the text file as well so it won’t load it the next time i open the program?
my current code for saving is
private void btnAddxuid_Click(object sender, EventArgs e)
{
try
{
ListViewItem lvi = new ListViewItem();
lvi.Text = txtxuidGamertag.Text;
lvi.SubItems.Add(txtXuid.Text);
listXuid.Items.Add(lvi);
TextWriter xuids = new StreamWriter(xuidspath, true);
xuids.WriteLine(txtxuidGamertag.Text + "-" + txtXuid.Text);
txtXuid.Clear();
txtxuidGamertag.Clear();
xuids.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The simple solution (without reworking your overall approach) would be to simply attach to the OnClick even of a remove button. The handler would then decide which item to remove (i’ll assume the ‘selected’ one), remove it then write over the file with the current contents of the listview.
Example (note, this is pseudo-code)
In general, you should try to keep the ‘data and logic’ in your application away from your user interface. In this case, that would mean writing a model class for elements in the list, binding the list items source to a collection of those items then dealing with the backing file source when needed (i.e, load the data-structure at startup and write to it only when you need to). The presentation would be handled by some kind of data template.
If you’re interested, check out WPF and MVVM.