How to Update .NET Windows Controls at Runtime

If you change the value of a control on Windows .NET form during runtime, it may not be updated with the changes. The following code should change the Text property of ToolStripStatusLabel, but it doesn’t work while its processing in the middle of its function. When its the last command in the function, it will change the label."

label_Folder.Text = "Converting documents to PDF.";

If additional code follows the label_Folder statement, it may not update the Text property. You can force the property to update with the Refresh method:

label_Folder.Text = "Converting documents to PDF.";
label_Folder.Refresh ();

The label will then be updated with the new content. If your using a ToolStripStatusLabel, you don’t have the option of using Refresh this way, i.e.

toolStripStatusLabel_ProcessingStatus.Text = "Status: 12 of 19 documents processed.";
toolStripStatusLabel_ProcessingStatus.Refresh ();

Instead, use this.Refresh () to force an update:

toolStripStatusLabel_ProcessingStatus.Text = "Status: 12 of 19 documents processed.";
this.Refresh ();

Hello World Example in iText

iText is a .NET library for creating and manipulating PDF’s. It was originally written in Java but it was also ported to .NET. The book, “iText in Action”, has examples in Java only which will only be useful if a .NET developer knows this language.  C# examples can be found at iText.NET site. The code needs to be slightly modified for it to compile with Visual Studio 2010.

The first thing that need to change is the library references. It uses a Java style code which may have been used in an older version of iText for .NET.

using com.lowagie.text;
using com.lowagie.text.pdf;

should be written as:

using iTextSharp.text;
using iTextSharp.text.pdf;

If you cut and paste the code from the examples to VS, you will see multiple errors. Change getInstance to GetInstance, open to Open, add to Add, close to Close. If your not sure about the function being used, delete it (include the period), type a period and the available functions will be listed.

The code will then compile correction in VS 2010. I recommend purchasing iText in Action because it will save you time in learning how to use iText. A knowledge of Java is beneficial, but not necessary to port the examples to .NET.

The following is a simple example of using iText with C#.


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace Chap0101
  class Program
    static void Main (string [] args)
      Console.WriteLine ("Chapter 1 example 1: Hello World");
      // step 1: creation of a document-object
      Document document = new Document ();
      // step 2:
      // we create a writer that listens to the document
      // and directs a PDF-stream to a file
      PdfWriter.GetInstance (document, new FileStream ("Chap0101.pdf", FileMode.Create));
      // step 3: we open the document
      document.Open ();
      // step 4: we add a paragraph to the document
      document.Add (new Paragraph ("Hello World"));
      // step 5: we close the document
      document.Close ();