What's new in Whidbey? : Part 1 - ClipBoard Improvements.

   There are lots of new things in the Whidbey release for Windows Forms. 

   There are a bunch of new controls and components like StripControls (ToolStrip, MenuStrip, ContextMenuStrip, StatusStrip), DataGridView, SplitContainer, WebBrowser. We have also added some richness to existing controls and components. For example we have exposed the scrollbars on scrollablecontrol as properties, added a bunch of new properties to the treeview and the tooltip, provided auto complete feature on the combo-box and the text-box and added list-view virtualization.

   In the designer space we have exposed a framework to build simple light weight designers that automagically uses the new serialization code, snaplines and other designer features. We have added snap-lines and a new mechanism to show the properties that are commonly used which we call the smart-tags extension. We have also enabled configuration settings management and provided extensible interfaces for the users to plug-in their own configuration settings.

   In order to explain some of the new features in Whidbey, I have decided to take one feature at a time and explain how it works and how it can be used. I will also try to explain how it is different (and easy) to use the same feature in Whidbey as compared to Everett (if applicable). So let’s get started...

 

ClipBoard :

   ClipBoard is an existing class from Everett which can be found in System.Windows.Forms namespace. The basic functionality of this class is to allow user to set data into the clipboard and retrieve it. This is how the class looked in Everett

public sealed class System.Windows.Forms.Clipboard :
object
{

// Constructors

// Methods
public virtual bool Equals(object obj);
public static System.Windows.Forms.IDataObject GetDataObject();
public virtual int GetHashCode();
public Type GetType();
public static void SetDataObject(object data);
public static void SetDataObject(object data, bool copy);
public virtual string ToString();
} // end of System.Windows.Forms.Clipboard

   The user can use the SetDataObject to place the data on the clipboard. The bool “copy” denotes if the data should remain on the clipboard after the application exits.

Consider a form with a picture box and a button. The following code would place the image of the picture box on the clipboard.

private void button1_Click(object sender, System.EventArgs e)

{

// Takes the image and puts it on the clipboard.

      if(this.pictureBox1.Image != null)

            Clipboard.SetDataObject(this.pictureBox1.Image, true);

}

   Now consider another application which has a panel and a button. Now you can set the background image of the panel to the image of the picture box that was copied to the clipboard earlier using the following code.

private void button2_Click(object sender, System.EventArgs e)

{

// Declares an IDataObject to hold the data returned from the // clipboard.

      // Retrieves the data from the clipboard.

      IDataObject iData = Clipboard.GetDataObject();

  // Determines whether the data is in a format you can use.

      if(iData.GetDataPresent(DataFormats.Bitmap))

      {

            // Yes it is, so display it.

this.panel1.BackgroundImage = (Bitmap)iData.GetData(DataFormats.Bitmap);

      }

      else

      {

            // No it is not.

            this.Text = "Could not retrieve data off the clipboard.";

      }

}

   In Whidbey the ClipBoard class looks as follows:

public sealed class System.Windows.Forms.Clipboard :
object
{

// Constructors

// Methods
public static void Clear();
public static bool ContainsAudio();
public static bool ContainsData(string format);
public static bool ContainsFileDropList();
public static bool ContainsImage();
public static bool ContainsText();
public static bool ContainsText(System.Windows.Forms.TextDataFormat format);
public virtual bool Equals(object obj);
public static System.IO.Stream GetAudioStream();
public static object GetData(string format);
public static System.Windows.Forms.IDataObject GetDataObject();
public static System.Collections.Specialized.StringCollection GetFileDropList();
public virtual int GetHashCode();
public static System.Drawing.Image GetImage();
public static string GetText();
public static string GetText(System.Windows.Forms.TextDataFormat format);
public Type GetType();
public static void SetAudio(byte[] audioBytes);
public static void SetAudio(System.IO.Stream audioStream);
public static void SetData(string format, object data);
public static void SetDataObject(object data);
public static void SetDataObject(object data, bool copy);
public static void SetDataObject(object data, bool copy, int retryTimes, int retryDelay);
public static void SetFileDropList(System.Collections.Specialized.StringCollection filePaths);
public static void SetImage(System.Drawing.Image image);
public static void SetText(string text);
public static void SetText(string text, System.Windows.Forms.TextDataFormat format);
public virtual string ToString();
} // end of System.Windows.Forms.Clipboard

 

   A bunch of static helper functions have been added to the ClipBoard class that would wrap the data in the DataObject and pass it onto the clipboard in the correct format. The ContainsXXX methods can be used by the user to query the current contents of the clipboard without actually getting the IDataObject from the clipboard and calling GetDataPresent on it. The helper functions are provided on commonly used DataFormats but users can still use the old SetDataObject and GetDataObject for custom formats.

   In Whidbey, you can programmatically clear the clipboard calling the Clear method. This removes all the contents of the clipboard and can be helpful if you want to purge the contents of the clipboard periodically.

   Coming to the same example that we discussed above, the same code in Whidbey would look like the following:

private void button1_Click(object sender, EventArgs e)

{

// Set the image (this will throw an exception if image passed in // is null)

     Clipboard.SetImage(this.pictureBox1.Image);

}

private void button2_Click(object sender, EventArgs e)

{

     // Check if ClipBoard has image...

     if (Clipboard.ContainsImage())

     {

         // if so, set the image to the background image of the panel

         this.panel1.BackgroundImage = Clipboard.GetImage();

     }

     else

     {

         this.Text = "No Image in ClipBoard";

     }

}

   As you can see the code here is greatly reduced as compared to Everett and most of the work is done by the clipboard class. The user just needs to call the proper helper functions.

Whidbey security restrictions using ClipBoard

   Clipboard works for application running under STA apartment thread only. More information about STA and MTA thread can be found here. In full-trust the clipboard allows to set and get any data. In partial trust (Internet zone) data only in the following four formats can be set into the clipboard:

 Formats allowed: Text, UnicodeText, System.String and CSV.

All other formats are restricted in partial trust.

 

Coming up next is TreeView... so stay tuned.