如何修改我输入的记录?

时间:2015-04-30 03:27:44

标签: c# .net database winforms events

我正在使用Windows窗体创建待办事项列表。 这就是我到目前为止所拥有的。 enter image description here

此表格的代码如下

namespace To_Do_List
{
    public partial class To_Do_List : Form
    {
        public To_Do_List()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) //this is the exit button on the toolstrip
        {
            Application.Exit(); //exits the program when the Exit button is clicked in dropdown menu
        }

        private void button4_Click(object sender, EventArgs e)//This creates the button to open the about form
        {
            AboutMyProgram aboutForm = new AboutMyProgram();
            aboutForm.ShowDialog(); //opens about form
        }

        private void button3_Click(object sender, EventArgs e) //This creates the button to open the form which ammends tasks
        {
            AmmendItem CreateForm = new AmmendItem(this);
            CreateForm.Show();
        }

        private void button1_Click(object sender, EventArgs e) // This creates the button to open the form which creates new tasks
        {
            AddItem CreateForm = new AddItem(this);
            CreateForm.Show();
        }


        public void listView1_SelectedIndexChanged_1(object sender, EventArgs e) // This creates the table
        {


        }

        private void button2_Click(object sender, EventArgs e) //This Allows the user to delete entries
        {
            if (listView1.SelectedItems != null)
            {
                var confirmation = MessageBox.Show(
                    "Are you sure you want to delete this?",
                    "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Question
                );

                if (confirmation == DialogResult.Yes)
                {
                    for (int i = listView1.Items.Count - 1; i >= 0; i--)
                    {
                        if (listView1.Items[i].Selected)
                        {
                            listView1.Items[i].Remove();
                            i--;
                        }
                    }
                }
            }
        }
    }
}

要添加任务,表单如下所示 enter image description here

再次,代码如下

namespace To_Do_List
{
    public partial class AddItem : Form
    {

        To_Do_List home;

        public AddItem(To_Do_List parent)
        {
            InitializeComponent();
            home = parent;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void openListToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void sortByDateToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        public void textBox1_TextChanged(object sender, EventArgs e)//Title Box
        {

        }

        public void /*Description of Task*/richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        public void /*Priority Box*/comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close(); //causes the window to close but keeps the application running
        }

        private void aboutToDoListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutMyProgram aboutForm = new AboutMyProgram();
            aboutForm.ShowDialog(); //opens about form displaying copyright information
        }

        public void button1_Click(object sender, EventArgs e) //create task button
        {
            ListViewItem item1 = new ListViewItem(Title.Text);

            item1.SubItems.Add(Description.Text);
            item1.SubItems.Add(Priority.Text);
            item1.SubItems.Add(Date.Text);
            string value = "";
            bool isChecked = Completed.Checked;
            if (isChecked)
                item1.SubItems.Add(Completed.Text);
            else
                value = null;

            home.listView1.Items.Add(item1);

            this.Close();


        }

        private void Date_ValueChanged(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e) //Closes the window
        {
            this.Close();
        }
    }
}

修订表格与新任务表格完全相同。我如何能够选择一条记录,按下修改按钮并实际更改? 我很难过。

此外,这不是这个问题的一部分,但我会问,以防万一有人知道。 当我再次打开应用程序时,我将如何实际保存这些记录以便它们在那里?

非常感谢您的阅读,我知道我刚刚抛弃了所有内容,但我对Windows Forms非常陌生,因此我不想错过任何重要的内容。

  

修改

我在正确的道路上有这样的事情吗?

 public void button1_Click(object sender, EventArgs e)
    {
        To_Do_List editform = new To_Do_List(Title.Text);
        editform.Description.Text = listView1.SelectedItems[0].SubItems[0].Text;

仍然无法让它工作:/

1 个答案:

答案 0 :(得分:1)

此弹出窗口代码。

public partial class frmToDoDetails : Form
{
    public string TaskTitle { get; set; }
    public string Description { get; set; }
    public bool EditMode { get; set; }

    public frmToDoDetails()
    {
        InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        TaskTitle = txtTitle.Text;
        Description = txtDesc.Text;
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }

    private void frmToDoDetails_Load(object sender, EventArgs e)
    {
        if (EditMode)
        {
            txtTitle.Text = TaskTitle;
            txtDesc.Text = Description;
        }
        else {
            txtTitle.Text = string.Empty;
            txtDesc.Text = string.Empty;            
        }
        txtTitle.Focus();
    }
}

这是列表

public partial class frmToDo : Form
{
    public frmToDo()
    {
        InitializeComponent();
    }

    private void btnNew_Click(object sender, EventArgs e)
    {
        frmToDoDetails frm = new frmToDoDetails();

        if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string[] arr = new string[2];
            ListViewItem itm; 

            arr[0] = frm.TaskTitle;
            arr[1] = frm.Description;

            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);
        }

    }

    private void frmToDo_Load(object sender, EventArgs e)
    {
        listView1.View = View.Details;
        listView1.GridLines = true;
        listView1.FullRowSelect = true;

        //Add column header
        listView1.Columns.Add("Title", 100);
        listView1.Columns.Add("Desc", 70); 

    }

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
      ListViewItem currentItem=   listView1.SelectedItems[0];
      frmToDoDetails frm = new frmToDoDetails();
      frm.TaskTitle = currentItem.SubItems[0].Text;
      frm.Description = currentItem.SubItems[1].Text;
      frm.EditMode = true;
      if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      {
             currentItem.SubItems[0].Text=frm.TaskTitle;
            currentItem.SubItems[1].Text=frm.Description;
      }
    }
}

我没有添加完整的字段(优先级,日期等)。

我希望它会对你有所帮助。