WinForms动态数据|数据未显示

时间:2018-10-02 12:41:46

标签: c# winforms

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

  • 我在课堂上用电子邮件和报告创建了一个空字符串 逻辑
  • 在生成报告的代码中,例如我把“做 报告... 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());
            }
        }
}

1 个答案:

答案 0 :(得分:0)

为什么不使用事件:

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}%";
}