【问题标题】:WinForms dynamic data | Data not showingWinForms 动态数据 |数据不显示
【发布时间】:2018-10-02 12:41:45
【问题描述】:

我有一个应用程序,它生成一封电子邮件,然后使用数据库中的值生成报告,所有这些都是与表单后面的代码分开的类。我创建了某种窗口,其中仅通过以下方式记录进度:

  • 我在课堂上用电子邮件和报告创建了一个空字符串 逻辑
  • 在生成报告的代码中,例如我已经把“制作 报告... 25%”;
  • 我在其他几个地方重复了此操作
  • 然后我将label.Text属性设置为字段为字符串

文本并没有像我预期的那样出现或改变。 假设 Form1 是进度表单之前的表单,而 Form2 是进度表单。

在表单 1 中,当单击“GO”按钮时,事件处理程序会创建一个新的 Form2 实例,然后是 form2.Show();。 在Form2里面我做了:

public partial class EmailLauncherProgress : Form
{
    ExportRecords exportRecords = new ExportRecords();
    public Form2()
    {
        InitializeComponent();

        label1.Text = exportRecords.reportProgress;
    }
}



public class ExportRecords
{
        public string reportProgress = "";
        public string exportFileName = "";

        public PrepareEmailAndReport()
        {
          MakeReport();
          MakeEmail();
          //other logic
        }

        public MakeReport()
        {
        //some logic
        reportprogress += "25%"
        }

        public MakeEmail()
        {
        //some logic
        reportprogress += "75%"
        }
}

public partial class MainForm : Form
{

        ExportRecords exportRecords = new ExportRecords();

        public MainForm()
        {
            InitializeComponent();
            //other logic to load form
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            Form1 emll = new Form1();

            if (emll.ShowDialog() == DialogResult.OK)
            {   
                /*GetRecipients just gets recipients from a tickbox returns 
                  as a list.*/
                exportRecords.PrepareAndEmailReport(emll.GetRecipients());
            }
        }
}

【问题讨论】:

  • 这段代码中的exportRecords 是什么?你没有给我们足够的代码来理解和重现这个问题。
  • 嗨@ADyson。我希望这会有所帮助,在此先感谢。
  • 好的,谢谢。很简单,您没有为 reportProgress 赋值。它是这样的:您创建一个新的 ExportRecords 对象。默认情况下,reportProgress 为空(因为这是您在类中定义它的方式)。然后你问它的价值。它的值仍然是空的,所以这就是你在屏幕上看到的。
  • 那么,每次我按照 MakeReport 和 MakeEmail 方法中所示附加字符串时,我必须如何调用该属性。
  • 您还没有运行这些方法。如果您在询问 reportsProgress 的值之前运行其中任何一个,您将看到结果。

标签: c# winforms


【解决方案1】:

为什么不使用事件:

public class ExportRecords
{
    public delegate void ExportRecordsHandler(int percentage);
    public event ExportRecordsHandler ExportRecordsProgressChanged;

    public void Execute()
    {
         //Executes your whole Process
         MakeReport(); //Reports Progress up to  25%
         MakeEmail();  //Reports Progress up to  75%
         SendEmail();  //Reports Progress up to 100%
    }

    public void MakeReport()
    {
         //some logic
         ExportRecordsProgressChanged?.Invoke(25);
    }

    public void MakeEmail()
    {
        //some logic
        ExportRecordsProgressChanged?.Invoke(75);
    }
}

所以在你的应用中你可以这样做:

public Form2()
{
    InitializeComponent();

    ExportRecords records = new ExportRecords();
    records.ExportRecordsProgressChanged += Progress_Changed;
    records.Execute(); //Run your actual process.
}

public void Progress_Changed(int percentage)
{
    //Event Handler get's called when ever the Event is invoked.
    label1.Text = $"{percentage}%";
}

【讨论】:

  • 嗨菲利克斯!感谢您的输入。我很好地实现了它,调试后我可以看到我的 label1.Text 被分配了 25% 但是它在前端没有改变,即我的标签仍然是空白的。你能帮忙吗?
  • 我用了刷新的方法,现在一切都好了!再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多