简体   繁体   English

从多个数组中删除一个元素

[英]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". 我通过将其重置为添加数据之前该元素中存在的原始默认值来清除该数组元素的内容,例如,我通过将内容设置为“ 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. 例如,假设您有3个任务输入到数组中。 You delete the task that was in element position 3, this method works fine! 您删除元素位置3中的任务,此方法可以正常工作! 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. 但是,如果用户决定删除另一个任务的前一个任务,即任务1或2,则列表框决定不显示任何连续的任务,即使尚未选择要删除它们。 For example, if you delete task 1 from the list, tasks 2 and 3 would also disappear. 例如,如果您从列表中删除任务1,则任务2和3也将消失。 OR if you delete task 2 from the list, task 3 would also disappear but task 1 would remain. 或者,如果您从列表中删除任务2,任务3也将消失,但任务1将保留。

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: DataArrays实际上用于存储数组:

    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": 假设窗体Form1带有一个列表框ListBox1,以下内容为我提供了一个显示“橙色”的列表框:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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