2012-07-11 73 views
-1

我添加了开始时不可见的标签,但是当我单击构建按钮时,我将标签设置为“正在加载...”,并且当完成该过程时,我将“完成”我编译的代码唯一的东西是标签“label2”。 为什么它不想得到我放的文字?完成状态窗体C#

public partial class MDGSelectiveForm : Form 
{ 
    EA.Repository modelRepository; 
    private NeatDiagram.CoreDiagram.MDGForm diagramBuilder; 



    public MDGSelectiveForm(EA.Repository repository) 
    { 
     InitializeComponent(); 
     **this.label2 = new Label(); 
     this.label2.Visible = false;** 
     this.modelRepository = repository; 
    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     **label2.Text = "Loading..."; 
     label2.Visible = true; 
     label2.Refresh();** 

     //Creo un'istanza di MDGBuilder 
     diagramBuilder = new CoreDiagram.MDGForm(); 

     String typeOfDiagram1 = ""; 
     String typeOfDiagram2 = ""; 
     String typeOfDiagram3 = ""; 

     if (checkBox1.Checked) 
      { 
      typeOfDiagram1 = checkBox1.Text; 
      diagramBuilder.createDiagram(typeOfDiagram1, modelRepository);  
      } 

     if (checkBox2.Checked) 
      { 
      typeOfDiagram2 = checkBox2.Text; 
      diagramBuilder.createDiagram(typeOfDiagram2, modelRepository); 
      } 

     if (checkBox3.Checked) 
      { 
      typeOfDiagram3 = checkBox3.Text; 
      diagramBuilder.createDiagram(typeOfDiagram3, modelRepository); 
      } 

     **label2.Text = "Complete";** 

     **label2.Refresh();** 


    } 
+0

我们不会为您编写代码。 – JohnD 2012-07-11 17:17:03

+1

你想知道如何在表单中放置文本,或者如何找出流程完成的时间或其他内容? – 2012-07-11 17:17:42

+0

@MrLister如何在表单中放置文本,如果你能帮助我,谢谢。 – Defi 2012-07-11 17:20:42

回答

3

如果你想显示状态,你可以使用一个StatusStrip,或者你可以用你想要的文字添加标签,其可见性设置为false,然后将其设置为true,当你需要显示的消息。

编辑: 要到你的跟进回应的问题,因为这不适合在评论 -

试着将标签后抛出Application.DoEvents()。这可能会解决它。请注意,这不是处理这种情况的最佳方式。如果因为CreateDiagram是一个长时间运行的操作而添加此操作,则不应从您的按钮OnClick处理程序调用CreateDiagram。任何长时间运行的操作都应该在自己的线程中完成。您可以使用BackGroundWorkerTask或任何您选择的线程来执行操作。在线程中,您可以调用UI以更新标签。更新后在标签上调用Invalidate。

+0

我重新编辑了这个问题,你能帮我吗?万分感谢! – Defi 2012-07-12 12:53:44

+0

我在代码中看不到标签,您在哪里设置它。如果您在点击处理程序中多次更改标签,则需要在设置“Text”属性后在标签上调用“Refresh”。你可以尝试'_myLabel.Invalidate()',但我相当确定你将需要执行'_myLabel.Refresh()'。 – pstrjds 2012-07-12 13:00:27

+0

我试过了,它也不起作用...我不明白为什么。我认为这很简单。 – Defi 2012-07-12 13:13:21