为用户创建状态字段 [英] Creating a statusfield for users

查看:102
本文介绍了为用户创建状态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我需要一些指导,以了解为什么它不起作用:

I need some guidance here on why this isn't working:

这就是问题所在,我想给我的用户一个小小的状态字段,以便他们可以检查需要多长时间,并为他们喝一两杯咖啡.

So here's the issue, I want to give my users a little status field so they can check how long it will take and get a coffee or two for them.

我的问题是statusfield(2个标签)在此过程中未更新.

My Problem is that the statusfield (2 Labels), are not updated during the process.

这是我当前的代码:

    private void Cancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void start_change_Click(object sender, EventArgs e)
    {

        DialogResult dr = MessageBox.Show("Start process?", "DateChanger", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
        if (dr == DialogResult.OK)
        {
            //get files
            List<String> d = new List<String>();
            label_status_title.Text = "Status: collecting Data, take a coffee while waiting.\nfiles changed: 0 files";
            d = getFiles("H:\\");
            int i = 0;
            double diff = 0.0;

            //modify files
            label_status_title.Text = "Status: changing files.\nfiles changed: 0/" + d.Count + " files.";
            foreach (String s in d)
            {
                    String label = "\nfile: " + s;
                    //create newDate and modify creation and lastwrite
                    DateTime actualDate = Directory.GetLastWriteTime(s).Date;
                    DateTime newDate = new DateTime(2015, 03, 01);
                    diff = (newDate - actualDate).TotalDays;
                    label += "\nactual creation date: " + Directory.GetCreationTime(s).Date;
                    label += "\nnew creation date: " + newDate.Date;
                    label += "\nactual last write date: " + Directory.GetLastWriteTime(s).Date;
                    label += "\nnew last write date: " + newDate.Date;

                    if (diff > 400)
                    {
                        try
                        {
                            //set new timevalues
                            Directory.SetCreationTime(s, newDate);
                            Directory.SetCreationTimeUtc(s, newDate);
                            Directory.SetLastWriteTime(s, newDate);
                            Directory.SetLastWriteTimeUtc(s, newDate);
                        }
                        catch (UnauthorizedAccessException UAE)
                        {
                        }
                        i++;
                        label += "\nchange needed.";
                    }
                    else
                    {
                        label += "\nchange not needed.";
                    }
                    label_status.Text = label;
                    label_status_title.Text = "Status: changing files.\nfiles changed: " + i + "/" + d.Count + " files.";
            }

            MessageBox.Show("Process finished, changed: " + i + "/" + d.Count + " files.");
        }
    }




    private List<String> getFiles(string sDir)
    {
        List<String> files = new List<String>();
        try
        {
            foreach (string f in Directory.GetFiles(sDir))
            {
                files.Add(f);
            }
            foreach (string d in Directory.GetDirectories(sDir))
            {
                files.AddRange(getFiles(d));
            }
        }
        catch (System.Exception excpt)
        {
            MessageBox.Show(excpt.Message);
        }
        return files;
    }

    private void DateChanger_Load(object sender, EventArgs e)
    {
        String label = "";
        label_status_title.Text = "Status: \nfiles changed: 0 files";
        label += "file: ";
        label += "\nactual creation date: ";
        label += "\nnew creation date: ";
        label += "\nactual last write date: ";
        label += "\nnew crealast writetion date: ";
        label_status.Text = label;
    }

我也尝试过使用MethodInvoker的建议,但这也不起作用.在此提供任何指导或建议.

I also tried the suggestion of using MethodInvoker, but that also didn't work either. Any guidance or suggestions here are appreciated.

谢谢.

米尔科

p.s.如果有比使用标签或文本框更好的解决方案,请随时告诉我. :)

p.s. if there is a better solution than using labels or text boxes for this feel free to tell me. :)

推荐答案

在为其分配新的Text值后只需刷新Label.

Just refresh the Label after assigning it a new Text value.

label_status_title.Text = "Status: changing files.\nfiles changed: " + i + "/" + d.Count + " files.";
label_status_title.Refresh(); //added

这篇关于为用户创建状态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆