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

Time:04-08

Student new to C# and trying to make a personal project where someone can login and manage tasks based on which user is logged in. I have connected the program to MySQL and am able to add new tasks but am having trouble figuring out how to "refresh" my checked box list after I add the new item.

The "dashboard" form containing the checkbox contains the following code:

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)
        {

        }
    }
}

And the form "new task" contains the following:

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

I have it so that the Dashboard form loads the list on start up but when I was trying to look up how to refresh it, I am having trouble figuring it out as It will not let me pull the "checkedListBox1" into the other form. Is it due to it being private or is there a better way to refresh the box easily?

Thanks in advance!

CodePudding user response:

You need to have code to refresh the data in your screen (sorry I finally worked out what dashboard_Load is - the event handler for the form load event, I was confused because its odd the see a form that doesnt have a capitalized name, no reason for that , just that its a class and traditionally in c# classes have CapitalLetters)

Make it a separate function (EDIT - forgot to make it 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);
        }
    }

your newTask form needs to be able to reach the dashboard form

Add

  public static dashboard Dash;

to the dashboard class.

Now change dashboard_Load to be

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

Now in your task add 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(); <<<<=======
    }

This should work. The static provides a way for another form to reach into the dashboard form.

  • Related