c#中的自动刷新列表框

时间:2016-09-05 14:37:34

标签: c# visual-studio list listbox

我有一个每X秒执行一次的代码。它会更新链接到表单中列表框的列表。

我想在列表更新时刷新列表框。 现在我通过按钮"刷新"来更新它们。因为该按钮可以访问列表框。

private void button3_Click(object sender, EventArgs e)
    {
        listBox1.DataSource = null;
        listBox2.DataSource = null;
        listBox3.DataSource = null;
        listBox4.DataSource = null;
        listBox1.DataSource = subject;
        listBox2.DataSource = from;
        listBox3.DataSource = date;
        listBox4.DataSource = timeLeft;
    }

没有这个按钮,有没有办法做到这一点?自动?

整个代码:

    //class email
    public class email
    {
        public string title { get; set; }
        public DateTime date { get; set; }
        public string sender { get; set; }
        public bool treated { get; set; }
        public bool toDelete { get; set; }

        public email() { }

        public email(string eTitle, DateTime eDate, string eSender, bool eTreated, bool eToDelete)
        {
            title = eTitle;
            date = eDate;
            sender = eSender;
            treated = eTreated;
            toDelete = eToDelete;
        }

    }

    //Initialisation de la fenetre principale
    public Form1()
    {
        InitializeComponent();
        comboBox1.Text = "30";
        label1.Text = "Process stopped";
        listBox1.DataSource = subject;
        listBox2.DataSource = from;
        listBox3.DataSource = date;
        listBox4.DataSource = timeLeft;
        initiate();

        System.Timers.Timer timer = new System.Timers.Timer(10000);
        timer.Elapsed += OnTimedEvent;
        timer.Enabled = true;

    }

    //Prise des informations outlook
    private static void initiate()
    {
        if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
        {
            try
            {
                app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                ns = app.GetNamespace("MAPI");
                inbox = ns.Folders["Support N2 MAS"].Folders["Inbox"];
                items = inbox.Items;

            }
            catch (COMException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else
        {
            MessageBox.Show("Please, start outlook..");
        }
    }

    //Run
    private void Run_Click(object sender, EventArgs e)
    {
        isRunning = true;
        label1.Text = "Process running";
    }

    //Stop
    private void Stop_Click(object sender, EventArgs e)
    {
        isRunning = false;
        label1.Text = "Process stopped";
    }

    //Icone barre des taches
    private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
    {
        this.Show();
        RemedyMailAlert.Visible = false;
        isVisible = true;
    }

    //Traitement toutes les X secondes        
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        if (isRunning)
        {
            if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
            {
                try
                {
                    DateTime dateToCheck = DateTime.Now.AddDays(-5);
                    currentList.Clear();

                    foreach (Object obj in items)
                    {
                        if (obj is Outlook.MailItem)
                        {
                            Outlook.MailItem mail = (Outlook.MailItem)obj;

                            if(mail.CreationTime > dateToCheck && !mail.Subject.ToString().Contains("INC000") && mail.CreationTime < DateTime.Now.AddMinutes(-reminder) && mail.UnRead)
                            {
                                email email = new email();
                                email.title = mail.Subject.ToString();
                                email.date = mail.CreationTime;
                                email.sender = mail.SenderName;
                                email.treated = false;
                                email.toDelete = false;

                                currentList.Add(email);
                            }
                        }
                    }
                    Compare();
                    Clean();
                    Display();
                }
                catch (COMException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Please, start outlook..");

            }
        }
        else
        {
            //doesn't run so we don't do anything..
        }
    }

    //Compare
    public static void Compare()
    {
        for (int i = 0; i < currentList.Count; i++)
        {
            int a = 0;
            for (int j = 0; j < listEmail.Count; j++)
            {
                if (listEmail[j].date == currentList[i].date && listEmail[j].title == currentList[i].title && listEmail[j].sender == currentList[i].sender)
                {
                    a++;
                }
            }

            if (a == 0)
            {
                listEmail.Add(currentList[i]);
            }                
        }

    }

    //Clean
    public static void Clean()
    {
        List<int> toDelete = new List<int>();

        for (int i = 0; i < listEmail.Count; i++)
        {
            int a = 0;
            for (int j = 0; j < currentList.Count; j++)
            {
                if (currentList[j].date != listEmail[i].date || listEmail[i].title != currentList[j].title)
                {
                    a++;
                }
            }

            if (a == currentList.Count)
            {
                toDelete.Add(i);
            }
        }

        for (int i = 0; i < toDelete.Count; i++)
        {
            listEmail.RemoveAt(toDelete[i]);
        }
    }

    //Display
    public static void Display()
    {
        fillListBoxes();
        //otherthings to come

    }

    //Traitement pour remplir les listbox
    public static void fillListBoxes()
    {
        subject.Clear();
        from.Clear();
        date.Clear();
        timeLeft.Clear();

        for (int i = 0; i < listEmail.Count; i++)
        {
            subject.Add(listEmail[i].title);
            from.Add(listEmail[i].sender);
            date.Add(listEmail[i].date.ToShortTimeString());
            int time = (int)(DateTime.Now - listEmail[i].date).TotalMinutes;
            timeLeft.Add((60 - time).ToString());
            listEmail[i].treated = true;
        }

    }


    //reglage du temps du reminder
    private void button1_Click(object sender, EventArgs e)
    {
        reminder = int.Parse(comboBox1.SelectedItem.ToString());
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Display();
        listBox1.DataSource = null;
        listBox2.DataSource = null;
        listBox3.DataSource = null;
        listBox4.DataSource = null;
        listBox1.DataSource = subject;
        listBox2.DataSource = from;
        listBox3.DataSource = date;
        listBox4.DataSource = timeLeft;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Hide();
        RemedyMailAlert.Visible = true;
        isVisible = false;
    }
}

0 个答案:

没有答案