在 C# 中添加新项目后尝试刷新选中框

Trying to Refresh the Checked box after adding a new item in C#

C# 的新手,正在尝试制作一个个人项目,让某人可以登录并根据登录的用户管理任务。我已将程序连接到 MySQL 并且能够添加新任务,但是添加新项目后,我无法弄清楚如何“刷新”我的复选框列表。

包含复选框的“仪表板”表单包含以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

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

        //Logs the user out
        private void button_logout_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to log out?", "Logout", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                this.Hide();
                Form_login fl = new Form_login();
                fl.Show();
                MessageBox.Show("Logout successful");
            }
        }

        //loads the tasks and user data after logging in
        private void dashboard_Load(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(helpconn.conVal("taskdb")))
                {
                    //Opens connection and loads all uncompleted tasks associated with the logged in user
                    conn.Open();
                    MySqlCommand ldtask = new MySqlCommand("SELECT task FROM tasks WHERE user_id = 1 AND completed = 0", conn);
                    MySqlDataReader dr = ldtask.ExecuteReader();

                    while (dr.Read())
                    {
                        string newItem = dr["task"].ToString();
                        checkedListBox1.Items.Add(newItem);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Creates a new task
        private void button1_Click(object sender, EventArgs e)
        {
            newtask nt = new newtask();
            nt.Show();
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

“新任务”表格包含以下内容:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

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

        private void button_addtask_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(helpconn.conVal("taskdb")))
                {
                    conn.Open();
                    MySqlCommand addtask = new MySqlCommand($"INSERT INTO tasks (user_id, task, completed) VALUES (1, '{ textBox_newtask.Text }', 0)", conn);
                    addtask.ExecuteNonQuery();
                    this.Hide();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

我有它,以便仪表板表单在启动时加载列表,但是当我试图查找如何刷新它时,我无法弄清楚它,因为它不会让我拉出“checkedListBox1”变成另一种形式。是因为它是私有的还是有更好的方法可以轻松刷新框?

提前致谢!

您需要有代码来刷新屏幕中的数据(抱歉,我终于弄清楚了 dashboard_Load 是什么 - 表单加载事件的事件处理程序,我很困惑,因为看到表单很奇怪没有大写的名称,没有理由,只是它是 class 并且传统上在 c# classes 中有大写字母)

让它成为一个单独的函数(编辑 - 忘记让它成为 public)

  public void load_dash()
    {
        try
        {
            using (MySqlConnection conn = new MySqlConnection(helpconn.conVal("taskdb")))
            {
                //Opens connection and loads all uncompleted tasks associated with the logged in user
                conn.Open();
                MySqlCommand ldtask = new MySqlCommand("SELECT task FROM tasks WHERE user_id = 1 AND completed = 0", conn);
                MySqlDataReader dr = ldtask.ExecuteReader();

                while (dr.Read())
                {
                    string newItem = dr["task"].ToString();
                    checkedListBox1.Items.Add(newItem);
                }

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

您的 newTask 表单需要能够访问仪表板表单

添加

  public static dashboard Dash;

到仪表板 class。

现在把dashboard_Load改成

  private void dashboard_Load(object sender, EventArgs e)
    {
       load_dash(); // the old code
       Dash = this; // wire up the static 
    }

现在在你的任务中添加 do

    private void button_addtask_Click(object sender, EventArgs e)
    {
        try
        {
            using (MySqlConnection conn = new MySqlConnection(helpconn.conVal("taskdb")))
            {
                conn.Open();
                MySqlCommand addtask = new MySqlCommand($"INSERT INTO tasks (user_id, task, completed) VALUES (1, '{ textBox_newtask.Text }', 0)", conn);
                addtask.ExecuteNonQuery();
                this.Hide(); 
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        dashboard.Dash.load_dash(); <<<<=======
    }

这应该有效。 static 为另一个表单提供了一种进入仪表板表单的方法。