简体   繁体   中英

Deleting an element from multiple arrays

I'm currently making a to-do list windows form application as a project. I have the program moreorless working, however the one thing that currently isn't working is the delete feature.

Basically the way my program currently works is that I use whatever task title that they select inside the List Box, the data associated with it (ie priority, start date, end date) are all found by using the same index position. This index position is found by using a Linear Search within the task title array, using the title that the user selects in the list box as the key.

I clear the contents of that element of the array by resetting it to the original default value that was present in that element before the data was added, for example I reset contents of the element within the task title array by setting the contents to "null".

I then refresh the list box contents using the following:

taskListBox.DataSource = null;
taskListBox.DataSource = DataArrays.titleArr;

However, the full code for this method is below.

public void deleteSelectedTask(ref ListBox taskListBox)
{
    MainForm main = new MainForm();
    DataInput input = new DataInput();

    string key = Convert.ToString(taskListBox.SelectedValue);

    int i = getTaskKey(key);

    DataArrays.titleArr[i] = null;
    DataArrays.descArr[i] = null;
    DataArrays.priorityArr[i] = 0;
    DataArrays.startDateArr[i] = Convert.ToDateTime("01/01/0001");
    DataArrays.endDateArr[i] = Convert.ToDateTime("01/01/0001");
    DataArrays.completeArr[i] = null;

    taskListBox.DataSource = null;
    taskListBox.DataSource = DataArrays.titleArr;
}

At present this method kind-of works and technically does remove the element, however for some reason this affects the data source for the array and doesn't always successfully refresh.

For example, say you had 3 tasks inputted into the array. You delete the task that was in element position 3, this method works fine! The task is successfully removed from the array and is not seen once the list box refreshes.

HOWEVER, if the user decides to delete a task that is a predescessor to another task, ie tasks 1 or 2, the list box decides not to display any of the consecutive tasks EVEN IF they haven't been selected to be deleted. For example, if you delete task 1 from the list, tasks 2 and 3 would also disappear. OR if you delete task 2 from the list, task 3 would also disappear but task 1 would remain.

I'm not sure what the problem is, I felt like having some fresh eyes on this code may aid this situation.

EDIT:

DataArrays is literally used for storing my arrays:

    public class DataArrays
    {
        public static string[] titleArr = new string[9];
        public static string[] descArr = new string[9];
        public static int[] priorityArr = new int[9];
        public static DateTime[] startDateArr = new DateTime[9];
        public static DateTime[] endDateArr = new DateTime[9];
        public static string[] completeArr = new string[9];
    }

Instead of storing the properties in separate arrays, you should use a single list of objects.

public class TaskItem
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Priority { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Complete { get; set; }
}

private List<TaskItem> TaskList = new List<TaskItem>();

public void deleteSelectedTask(ref ListBox taskListBox)
{
    var item = (TaskItem) taskListBox.SelectedItem;
    if (item == null) return;

    TaskList.Remove(item);

    taskListBox.DataSource = null;
    taskListBox.DisplayMember = "Title";
    taskListBox.DataSource = TaskList;
}

I've confirmed that it works with Lists. Assuming a form Form1, with a listbox ListBox1, The following gives me a ListBox showing "orange":

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        List<String> fruits = new List<String>{ "apple", "orange" };

        this.listBox1.DataSource = fruits.ToArray();
        this.listBox1.DataSource = null;
        fruits.RemoveAt(0);
        this.listBox1.DataSource = fruits.ToArray();
    }
}

The following, using Array, not list, does not work:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

       String [] fruits = { "apple", "orange" };

        this.listBox1.DataSource = fruits;
        this.listBox1.DataSource = null;
        fruits[0] = null;
        this.listBox1.DataSource = fruits;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM