Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a PhoneBook App which adds and save users input in a textfile I am trying to have a function which enable the user to be able to select users details from a Listbox then delete them. As well when you click on the users a screen must popup with the users details and enable you to edit and save again to textfile.
I have tried the DeletedSelectedItemListBox which I have created below in order to be able to delete selected Item from my Listbox.

What I have tried:

C#
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PhoneBook.View;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace PhoneBook.ViewModel
{
    class MainWindowViewModel : ViewModelBase
    {
        #region Constructor
        public MainWindowViewModel()
        {
            ReadTextFile();
        }

        #endregion
        #region Properties
        public const string MyListPropertyName = "ListBox";
        private List<string> _listBox = null;
        public List<string> ListBox
        {
            get
            {
                return _listBox = _listBox ?? new List<string>();
            }
            set
            {
                if (_listBox == value)
                {
                    return;
                }
                RaisePropertyChanged(MyListPropertyName);
                _listBox = value;
                RaisePropertyChanged(MyListPropertyName);
            }
          
          
        }
        private string _selectedItem = null;
        public string SelectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                if (_selectedItem != null)
                {
                    return;
                }
                _selectedItem = value;
                RaisePropertyChanged();
            }
        }



        #endregion

        #region Method

        private void PopUpWindSelectedItem()
        {
           
        }

        private void DeleteSelectedItemListBox()
        {
            string FileName = (@"C: \Users\StanleyM\Desktop\PhoneBook\PhoneBook\bin\Debug\Personal.text");
            StreamReader streamReader = new StreamReader(FileName);
            string line = "";
            int Counter = -1;
            
            while ((line = streamReader.ReadLine()) != null)
            {
                foreach (var item in line)
                {
                    if (item.ToString() == SelectedItem.ToString())
                    {
                        Counter--;
                        ListBox.Remove(line);
                    }
                    
                }
               
            }

        }

        public void ReadTextFile()
        {
            string FileName = (@"C: \Users\StanleyM\Desktop\PhoneBook\PhoneBook\bin\Debug\Personal.text");
            DataTable dt = new DataTable("Client");
            StreamReader streamReader = new StreamReader(FileName);
            string line = "";
            int Counter = 0;
            while ((line = streamReader.ReadLine()) != null)
            {
                Counter++;
                ListBox.Add(item: line);
            }
        }

        private void PopUpWindow()
        {
            AddEditView PopUp = new AddEditView();
            PopUp.ShowDialog();

        }


        #endregion
        #region RelayCommand

        private RelayCommand _addCommand = null;
        public RelayCommand AddCommand
        {
            get
            {
                return _addCommand = _addCommand ?? new RelayCommand(() => PopUpWindow());
            }
        }

        private RelayCommand _deleteCommand = null;
        public RelayCommand DeleteCommand
        {
            get
            {
                return _deleteCommand = _deleteCommand ?? new RelayCommand(() => DeleteSelectedItemListBox());
            }
        }


        #endregion
    }
}
Posted
Updated 10-Sep-17 23:15pm
v2

1 solution

First thing, You need to use a Collection type That will notify the UI of any changes. So this:
C#
public const string MyListPropertyName = "ListBox";
private List<string> _listBox = null;
public List<string> ListBox
{
	get
	{
		return _listBox = _listBox ?? new List<string>();
	}
	set
	{
		if (_listBox == value)
		{
			return;
		}
		RaisePropertyChanged(MyListPropertyName);
		_listBox = value;
		RaisePropertyChanged(MyListPropertyName);
	}
}

can be changed to this:
C#
public ObservableCollection<string> ListBox { get; set; }
    = new ObservableCollection<string>();

Your XAML is missing so I can't see if the SelectedItem property is bound correctly.

I also can not check if your DeleteCommand is enabled correctly. The RelayCommand supports enabling and disabling the bound button. So you could wire it up like:
C#
public RelayCommand DeleteCommand { get; private set; }

private void InitializeRelayCommands()
{
    DeleteCommand  = new RelayCommand
                    (() => DeleteSelectedItemListBox(),
                     () => isSelected);
}

private bool isSelected;

private string selectedItem;
public string SelectedItem
{
    get { return selectedItem; }
    set
    {
        Set(ref selectedItem, value); // ** see below
        isSelected = !string.IsNullOrEmpty(SelectedItem);
    }
}

** The ViewModel needs to inherit the Galasoft ViewModelBase.

Now to delete the item, you only need to add the following where you want to remove the SelectedItem from the collection:
C#
if (!string.IsNullOrEmpty(SelectedItem))
{
    ListBox.Remove(SelectedItem);
    SelectedItem = string.Empty;
}

Lastly, to prevent any confusion, don't call the collection ListBox but instead something meaningful like ContactNames and SelectedItem as SelectedContact. Now your code is self-describing with these two simple changes:
C#
ContactNames.Remove(SelectedContact);

Also, you're using the incorrect namespace for the RelayCommand.Change:
C#
using GalaSoft.MvvmLight.Command;

to:
C#
using GalaSoft.MvvmLight.CommandWpf;

So your MainWindowViewModel now looks like:
C#
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using PhoneBook.View;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
 
namespace PhoneBook.ViewModel
{
    class MainWindowViewModel : ViewModelBase
    {
        #region Constructor
        
		public MainWindowViewModel()
        {
			InitializeRelayCommands();
            ReadTextFile();
        }
 
        #endregion
		
        #region Properties
        
        public ObservableCollection<string> ContactNames { get; set; }
            = new ObservableCollection<string>(); 

		private bool isSelected;
		 
		private string selectedContact;
		public string SelectedContact
		{
			get { return selectedContact; }
			set
			{
				Set(ref selectedContact, value);
				isSelected = !string.IsNullOrEmpty(selectedContact);
			}
		}
        #endregion
 
        #region Method
 
        private void PopUpWindSelectedContact()
        {
           
        }
 
        private void DeleteSelectedContactContactNames()
        {
            string FileName = (@"C: \Users\StanleyM\Desktop\PhoneBook\PhoneBook\bin\Debug\Personal.text");
            StreamReader streamReader = new StreamReader(FileName);
            string line = "";
            int Counter = -1;
            
            while ((line = streamReader.ReadLine()) != null)
            {
                foreach (var item in line)
                {
                    if (item == SelectedContact)
                    {
                        Counter--;
                        ContactNames.Remove(line);
                    }
                }
            }
        }
 
        public void ReadTextFile()
        {
            string FileName = (@"C: \Users\StanleyM\Desktop\PhoneBook\PhoneBook\bin\Debug\Personal.text");
            DataTable dt = new DataTable("Client");
            StreamReader streamReader = new StreamReader(FileName);
            string line = "";
            int Counter = 0;
            while ((line = streamReader.ReadLine()) != null)
            {
                Counter++;
                ContactNames.Add(item: line);
            }
        }
 
        private void PopUpWindow()
        {
            AddEditView PopUp = new AddEditView();
            PopUp.ShowDialog();
        }

        #endregion

        #region RelayCommand
 
        public RelayCommand AddCommand { get; private set; }
		public RelayCommand DeleteCommand { get; private set; }
		 
		private void InitializeRelayCommands()
		{
			AddCommand = new RelayCommand
			                (() => PopUpWindow());

			DeleteCommand  = new RelayCommand
							(() => DeleteSelectedContactContactNames(),
							 () => isSelected);
		}

        #endregion
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900