<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5763115508258462541</id><updated>2025-04-26T04:44:14.868-07:00</updated><title type='text'>The BinSoft Blog</title><subtitle type='html'>Welcome to my development blog. Within these posts you will find a wide variety of code samples, grips and whatever else I decide to share with you. It&#39;s my way of giving something back to the many people that have provided me with snippets and advice over the years.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://anotherdeveloper.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://anotherdeveloper.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>David</name><uri>http://www.blogger.com/profile/17125105494896617758</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-6Yl_ErF3NNVSrB7IulUTHlDMmmh767BWPlSrmelnGa0OXxbSCs-grZU8h5xEc3YRXc99m0jycHOMcEx57pRp7h0KYYeFLhoBHSiihtzsxJW_hcExOm8xE9N5f3FVwA/s220/57949_1509560393038_1655215657_1271155_1556591_n2.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5763115508258462541.post-1025113954542587853</id><published>2012-04-28T11:13:00.001-07:00</published><updated>2012-04-28T11:22:34.038-07:00</updated><title type='text'>Using EventWaitHandle to Wait for Delegates</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;[C# - WinForms - .NET 2.0+]&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I recently had a to create a database query queue system in an very complicated exisiting application. I wanted to make something quickly that would not break the exisitng modules that relied on the old method until I could go through each individual function that required change. The old way was to run a query and return&amp;nbsp; a DataSet object. The new way queued each request and returned the DataSet via a delegate. This required that the original method create a query object, use an inline delegate and then wait until the delegate fired before it could continue. Basically I wanted to make an asynchronous operation synchronous. Welcome the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;EventWaitHandle&lt;/a&gt; object.&lt;br /&gt;
&lt;br /&gt;
The code below is a snippet of the actual method I used. It&amp;nbsp;adds an item to the queue and use the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/58195swd.aspx&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;WaitOne&lt;/a&gt;() method of the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;EventWaitHandler&lt;/a&gt; object to wait for a signal which is fired as soon as the delegate is called.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public DataSet RunDSQuery(string query)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; EventWaitHandle signal = new EventWaitHandle(false, EventResetMode.ManualReset);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DataSet ds = null;

&amp;nbsp;&amp;nbsp;&amp;nbsp; DatabaseQueueItem queueItem = new DatabaseQueueItem();
&amp;nbsp;&amp;nbsp;&amp;nbsp; queueItem.Command = this.DbConnection.CreateCommand();
&amp;nbsp;&amp;nbsp;&amp;nbsp; queueItem.Command.CommandText = query;
&amp;nbsp;&amp;nbsp;&amp;nbsp; queueItem.Result_Callback = delegate(object sender, DataSet cbds, Exception cbex)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ds = cbds;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;signal.Set();
&amp;nbsp;&amp;nbsp;&amp;nbsp; };

&amp;nbsp;&amp;nbsp;&amp;nbsp; _DatabaseQueue.Add(queueItem);

&amp;nbsp;&amp;nbsp;&amp;nbsp; signal.WaitOne();&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return ds;
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://anotherdeveloper.blogspot.com/feeds/1025113954542587853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://anotherdeveloper.blogspot.com/2012/04/using-eventwaithandle-to-wait-for.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/1025113954542587853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/1025113954542587853'/><link rel='alternate' type='text/html' href='http://anotherdeveloper.blogspot.com/2012/04/using-eventwaithandle-to-wait-for.html' title='Using EventWaitHandle to Wait for Delegates'/><author><name>David</name><uri>http://www.blogger.com/profile/17125105494896617758</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-6Yl_ErF3NNVSrB7IulUTHlDMmmh767BWPlSrmelnGa0OXxbSCs-grZU8h5xEc3YRXc99m0jycHOMcEx57pRp7h0KYYeFLhoBHSiihtzsxJW_hcExOm8xE9N5f3FVwA/s220/57949_1509560393038_1655215657_1271155_1556591_n2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5763115508258462541.post-2947582748609000811</id><published>2012-03-18T17:49:00.000-07:00</published><updated>2012-03-18T17:49:20.504-07:00</updated><title type='text'>IIS 7 Web Farm Framework - Error When Syncing a New Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;I was setting up a new IIS 7 web farm to replace five IIS 6 web servers. We started off by configuring&amp;nbsp;a control server running Windows Server&amp;nbsp;2008 R2 x32&amp;nbsp;and IIS 7.5. This control server was also set to be&amp;nbsp;the Primary server role in syncing the other web servers. &lt;br /&gt;
&lt;br /&gt;
Each web server would be a fresh install of Windows Web Server 2008 R2 x64&amp;nbsp;and IIS 7.5. We&amp;nbsp;added&amp;nbsp;each server&amp;nbsp;and the platform was provisioned. Next step was to provision the applications and this is where&amp;nbsp;a very unhelpful error kept occuring on each server.&lt;br /&gt;
&lt;br /&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;em&gt;&quot;Failed to run operation &#39;RunRemote&#39;. &amp;nbsp;Failed to run method &#39;Microsoft.Web.Farm.SyncApplicationsRemoteMethod&#39; on server &#39;server name&#39;. &amp;nbsp;Exception in response stream. &amp;nbsp;Invalid index. (Exception from HRESULT: 0x80070585). &amp;nbsp;Exception in response stream. &amp;nbsp;Invalid index. (Exception from HRESULT: 0x80070585).&quot;&lt;/em&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
The stack trace that was logged in the WebFarmService.log file (location: C:\Program Files\IIS\Microsoft Web Farm Framework) was not any more helpful.&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;&quot;at Microsoft.Web.Deployment.IAppHostElement.GetPropertyByName(String bstrSubName)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.CrossPlatformRuleHandler.UpdateObjectEntryIfNeccessary(IAppHostElement sectionElement, SectionEntries entries)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.CrossPlatformRuleHandler.UpdateSectionEntriesIfNeccessary(IAppHostConfigLocation location, LocationSectionEntries locSecEntries)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.CrossPlatformRuleHandler.UpdateNativeModulesIfNeccessary(AdminManagerWrapper wrapper)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.CrossPlatformRuleHandler.PostSync(DeploymentSyncContext syncContext)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.DeploymentSyncContext.ProcessSync(DeploymentObject destinationObject, DeploymentObject sourceObject)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.DeploymentObject.SyncToInternal(DeploymentObject destObject, DeploymentSyncOptions syncOptions, PayloadTable payloadTable, ContentRootTable contentRootTable)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.DeploymentObject.SyncTo(DeploymentProviderOptions providerOptions, DeploymentBaseOptions baseOptions, DeploymentSyncOptions syncOptions)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Deployment.DeploymentObject.SyncTo(DeploymentBaseOptions baseOptions, DeploymentSyncOptions syncOptions)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Farm.SyncApplicationsRemoteMethod.RunRemote(ServerRemoteMethodContext methodContext, Object[] parameters)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Farm.WebFarmAgentService.HandleRunMethod(HttpListenerContext context)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; --- End of inner exception stack trace ---&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Farm.RunRemoteAsyncResult.ResponseCompleteCallback(IAsyncResult result)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; --- End of inner exception stack trace ---&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Farm.ProvisionApplicationsOperationProvider.EndRunOperation(IAsyncResult result)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Farm.ServerOperationContext.EndRunOperationProvider(IAsyncResult result)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; at Microsoft.Web.Farm.RunOperationContext.FinalOperationCompleteCallback(IAsyncResult result)&quot;&lt;/em&gt;&lt;br /&gt;
&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
It turns out that for some reason&amp;nbsp;the Web Farm Framwork&#39;s provisioning tool does not work well with the IIS 7.5 &quot;Default Web Site&quot; setup. The fix took a while to figure out but is pretty simple. &lt;br /&gt;
&lt;br /&gt;
• Delete the &quot;Default Web Site&quot; site&amp;nbsp;from the destination IIS server&lt;br /&gt;
• Run msdeploy.exe on the destination server to sync the source server&#39;s &quot;Default Web Site&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MSDeploy.exe can be found at &quot;C:\program files\IIS\Microsoft Web Deploy V2&quot; and the syntax for running the sync is &lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;msdeploy -verb:sync -source:apphostconfig=&quot;Default Web Site&quot;,computername=SourceServerName -dest:apphostconfig=&quot;Default Web Site&quot;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
This worked for me and I hope it saves you some time as well.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://anotherdeveloper.blogspot.com/feeds/2947582748609000811/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://anotherdeveloper.blogspot.com/2012/03/iis-7-web-farm-framework-error-when.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/2947582748609000811'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/2947582748609000811'/><link rel='alternate' type='text/html' href='http://anotherdeveloper.blogspot.com/2012/03/iis-7-web-farm-framework-error-when.html' title='IIS 7 Web Farm Framework - Error When Syncing a New Server'/><author><name>David</name><uri>http://www.blogger.com/profile/17125105494896617758</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-6Yl_ErF3NNVSrB7IulUTHlDMmmh767BWPlSrmelnGa0OXxbSCs-grZU8h5xEc3YRXc99m0jycHOMcEx57pRp7h0KYYeFLhoBHSiihtzsxJW_hcExOm8xE9N5f3FVwA/s220/57949_1509560393038_1655215657_1271155_1556591_n2.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5763115508258462541.post-1438848394212492615</id><published>2012-01-21T07:52:00.000-08:00</published><updated>2012-01-21T07:57:09.078-08:00</updated><title type='text'>How-to Hide the .NET PrintPreviewControl&#39;s &quot;Generating Previews&quot; Dialog</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;strong&gt;&lt;span style=&quot;color: #38761d; font-family: Arial;&quot;&gt;[C# - WinForms - .NET 2.0+]&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: black; font-family: Arial;&quot;&gt;&lt;span style=&quot;font-family: Times New Roman;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black; font-family: Arial;&quot;&gt;&lt;span style=&quot;color: black; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;;&quot;&gt;I have been working on revising an old program and decided to add a Print Preview dialog to the print functionality. To my dismay the standard .NET PrintPreviewControl shows this annoying little, application-focus stealing &quot;Generating Previews&quot; dialog every time the PrintPreviewControl is refreshed. &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times New Roman;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;;&quot;&gt;So I started searching on Google to see how to disable this little annoyance and found that there is no out-of-the-box way to disable the dialog. On top of that I saw posts from people having the same frustrations dating back to 2005. Now why Microsoft never added a ShowDialog property to this control so their users would have the option of turning this happy little feature on and off is beyond me. &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times New Roman;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;;&quot;&gt;I decided that I would have to create my own control and get rid of that dialog box once and for all. Turns out this is easier said than done. I won&#39;t go into the detail of this code but it works and that pesky dialog box no longer shows up. There is a lot of reflected code in this control and I can&#39;t guarantee that it will be compatible with future versions but that&#39;s all tweakable.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times New Roman;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;;&quot;&gt;Download and enjoy.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: black; font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Times New Roman;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml.Serialization;


namespace CustomControls
{
 public class PrintControllerNoStatusDialog : PrintController
 {
  private PrintDocument document;
  private int pageNumber;
  private PrintController underlyingController;


  public PrintControllerNoStatusDialog(PrintController underlyingController)
  {
   this.underlyingController = underlyingController;
  }


  public override void OnEndPage(PrintDocument document, PrintPageEventArgs e)
  {
   this.underlyingController.OnEndPage(document, e);
   
   this.pageNumber++;
   base.OnEndPage(document, e);
  }

  public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
  {
   this.underlyingController.OnEndPrint(document, e);   
   base.OnEndPrint(document, e);
  }

  public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
  {
   base.OnStartPage(document, e);
   
   Graphics graphics = this.underlyingController.OnStartPage(document, e);

   return graphics;
  }

  public override void OnStartPrint(PrintDocument document, PrintEventArgs e)
  {
   base.OnStartPrint(document, e);
   
   this.document = document;
   this.pageNumber = 1;
  
   try
   {
    this.underlyingController.OnStartPrint(document, e);
   }
   catch
   {
    throw;
   }   
  }

  public override bool IsPreview
  {
   get
   {
    return ((this.underlyingController != null) &amp;&amp; this.underlyingController.IsPreview);
   }
  }

  
 }


 internal class NativeMethods
 {
  [StructLayout(LayoutKind.Sequential)]
  public class SCROLLINFO
  {
   public int cbSize;
   public int fMask;
   public int nMin;
   public int nMax;
   public int nPage;
   public int nPos;
   public int nTrackPos;

   public SCROLLINFO()
   {
    this.cbSize = Marshal.SizeOf(typeof(NativeMethods.SCROLLINFO));

   }
   public SCROLLINFO(int mask, int min, int max, int page, int pos)
   {
    this.cbSize = Marshal.SizeOf(typeof(NativeMethods.SCROLLINFO));
    this.fMask = mask;
    this.nMin = min;
    this.nMax = max;
    this.nPage = page;
    this.nPos = pos;
   }
  }

  [StructLayout(LayoutKind.Sequential)]
  public struct RECT
  {
   public int left;
   public int top;
   public int right;
   public int bottom;

   public RECT(int left, int top, int right, int bottom)
   {
    this.left = left;
    this.top = top;
    this.right = right;
    this.bottom = bottom;
   }

   public RECT(Rectangle r)
   {
    this.left = r.Left;
    this.top = r.Top;
    this.right = r.Right;
    this.bottom = r.Bottom;
   }

   public static NativeMethods.RECT FromXYWH(int x, int y, int width, int height)
   {
    return new NativeMethods.RECT(x, y, x + width, y + height);
   }

   public Size Size
   {
    get
    {
     return new Size(this.right - this.left, this.bottom - this.top);
    }
   }
  }


  internal static class Util
  {
   public static int HIWORD(int n)
   {
    return ((n &gt;&gt; 0x10) &amp; 0xffff);
   }

   public static int HIWORD(IntPtr n)
   {
    return HIWORD((int)((long)n));
   }

   public static int LOWORD(int n)
   {
    return (n &amp; 0xffff);
   }

   public static int LOWORD(IntPtr n)
   {
    return LOWORD((int)((long)n));
   }
  }

 }

 internal static class SafeNativeMethods
 {
  [DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto, ExactSpelling = true)]
  public static extern bool GetScrollInfo(HandleRef hWnd, int fnBar, [In, Out] NativeMethods.SCROLLINFO si);

  [DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto, ExactSpelling = true)]
  public static extern bool ScrollWindow(HandleRef hWnd, int nXAmount, int nYAmount, ref NativeMethods.RECT rectScrollRegion, ref NativeMethods.RECT rectClip);

  }

 internal static class UnsafeNativeMethods
 {
  [DllImport(&quot;gdi32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
  public static extern int GetDeviceCaps(HandleRef hDC, int nIndex);

  [DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
  public static extern int SetScrollPos(HandleRef hWnd, int nBar, int nPos, bool bRedraw);

  [DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto, ExactSpelling = true)]
  public static extern int SetScrollInfo(HandleRef hWnd, int fnBar, NativeMethods.SCROLLINFO si, bool redraw);

 }

 [ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch), DefaultProperty(&quot;Document&quot;)]
 public class PrintPreviewControl : Control
 {
  private bool antiAlias;
  private bool autoZoom = true;
  private const int border = 10;
  private int columns = 1;
  private const double DefaultZoom = 0.3;
  private PrintDocument document;
  private static readonly object EVENT_STARTPAGECHANGED = new object();
  private bool exceptionPrinting;
  private Size imageSize = Size.Empty;
  private Point lastOffset;
  private bool layoutOk;
  private PreviewPageInfo[] pageInfo;
  private bool pageInfoCalcPending;
  private Point position = new Point(0, 0);
  private int rows = 1;
  private Point screendpi = Point.Empty;
  private const int SCROLL_LINE = 5;
  private const int SCROLL_PAGE = 100;
  private int startPage;
  private Size virtualSize = new Size(1, 1);
  private double zoom = 0.3;

  public event EventHandler StartPageChanged
  {
   add
   {
    base.Events.AddHandler(EVENT_STARTPAGECHANGED, value);
   }
   remove
   {
    base.Events.RemoveHandler(EVENT_STARTPAGECHANGED, value);
   }
  }

  [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  public event EventHandler TextChanged
  {
   add
   {
    base.TextChanged += value;
   }
   remove
   {
    base.TextChanged -= value;
   }
  }

  // Methods
  public PrintPreviewControl()
  {
   this.ResetBackColor();
   this.ResetForeColor();
   base.Size = new Size(100, 100);
   base.SetStyle(ControlStyles.ResizeRedraw, false);
   base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.Opaque, true);
  }

  private int AdjustScroll(Message m, int pos, int maxPos, bool horizontal)
  {
   switch (NativeMethods.Util.LOWORD(m.WParam))
   {
    case 0:
     if (pos &lt;= 5)
     {
      pos = 0;
      return pos;
     }
     pos -= 5;
     return pos;

    case 1:
     if (pos &gt;= (maxPos - 5))
     {
      pos = maxPos;
      return pos;
     }
     pos += 5;
     return pos;

    case 2:
     if (pos &lt;= 100)
     {
      pos = 0;
      return pos;
     }

     pos -= 100;
     return pos;

    case 3:
     if (pos &gt;= (maxPos - 100))
     {
      pos = maxPos;
      return pos;
     }

     pos += 100;
     return pos;

    case 4:
    case 5:
     {
      NativeMethods.SCROLLINFO si = new NativeMethods.SCROLLINFO
      {
       cbSize = Marshal.SizeOf(typeof(NativeMethods.SCROLLINFO)),
       fMask = 0x10
      };

      int fnBar = horizontal ? 0 : 1;

      if (!SafeNativeMethods.GetScrollInfo(new HandleRef(this, m.HWnd), fnBar, si))
      {
       pos = NativeMethods.Util.HIWORD(m.WParam);
       return pos;
      }

      pos = si.nTrackPos;

      return pos;
     }
   }
   return pos;
  }

  private void CalculatePageInfo()
  {
   if (!this.pageInfoCalcPending)
   {
    this.pageInfoCalcPending = true;
    try
    {
     if (this.pageInfo == null)
     {
      try
      {
       this.ComputePreview();
      }
      catch
      {
       this.exceptionPrinting = true;
       throw;
      }
      finally
      {
       base.Invalidate();
      }
     }
    }
    finally
    {
     this.pageInfoCalcPending = false;
    }
   }
  }

  private void ComputeLayout()
  {
   this.layoutOk = true;

   if (this.pageInfo.Length == 0)
   {
    base.ClientSize = base.Size;
   }
   else
   {
    Graphics wrapper = Graphics.FromHwndInternal(this.Handle);
    IntPtr hdc = wrapper.GetHdc();

    this.screendpi = new Point(UnsafeNativeMethods.GetDeviceCaps(new HandleRef(wrapper, hdc), 0x58), UnsafeNativeMethods.GetDeviceCaps(new HandleRef(wrapper, hdc), 90));

    wrapper.ReleaseHdcInternal(hdc);
    wrapper.Dispose();

    Size physicalSize = this.pageInfo[this.StartPage].PhysicalSize;
    Size size2 = new Size(PixelsToPhysical(new Point(base.Size), this.screendpi));

    if (this.autoZoom)
    {
     double num = (size2.Width - (10 * (this.columns + 1))) / ((double)(this.columns * physicalSize.Width));
     double num2 = (size2.Height - (10 * (this.rows + 1))) / ((double)(this.rows * physicalSize.Height));

     this.zoom = Math.Min(num, num2);
    }

    this.imageSize = new Size((int)(this.zoom * physicalSize.Width), (int)(this.zoom * physicalSize.Height));

    int x = (this.imageSize.Width * this.columns) + (10 * (this.columns + 1));
    int y = (this.imageSize.Height * this.rows) + (10 * (this.rows + 1));

    this.SetVirtualSizeNoInvalidate(new Size(PhysicalToPixels(new Point(x, y), this.screendpi)));
   }
  }

  private void ComputePreview()
  {
   int startPage = this.StartPage;

   if (this.document == null)
   {
    this.pageInfo = new PreviewPageInfo[0];
   }
   else
   {
    new PrintingPermission(PrintingPermissionLevel.SafePrinting).Demand();

    PrintController printController = this.document.PrintController;
    PreviewPrintController underlyingController = new PreviewPrintController
    {
     UseAntiAlias = this.UseAntiAlias
    };

    this.document.PrintController = new PrintControllerNoStatusDialog(underlyingController);
    this.document.Print();

    this.pageInfo = underlyingController.GetPreviewPageInfo();
    this.document.PrintController = printController;
   }

   if (startPage != this.StartPage)
   {
    this.OnStartPageChanged(EventArgs.Empty);
   }
  }

  private void InvalidateLayout()
  {
   this.layoutOk = false;
   base.Invalidate();
  }

  public void InvalidatePreview()
  {
   this.pageInfo = null;
   this.InvalidateLayout();
  }

  protected override void OnPaint(PaintEventArgs pevent)
  {
   using (Brush brush = new SolidBrush(this.BackColor))
   {
    if ((this.pageInfo == null) || (this.pageInfo.Length == 0))
    {
     pevent.Graphics.FillRectangle(brush, base.ClientRectangle);

     if ((this.pageInfo != null) || this.exceptionPrinting)
     {
      StringFormat format = new StringFormat
      {
       Alignment = TranslateAlignment(ContentAlignment.MiddleCenter),
       LineAlignment = TranslateLineAlignment(ContentAlignment.MiddleCenter)
      };

      SolidBrush brush2 = new SolidBrush(this.ForeColor);

      try
      {
       if (this.exceptionPrinting)
       {
        pevent.Graphics.DrawString(&quot;Document cannot be displayed.&quot;, this.Font, brush2, base.ClientRectangle, format);
       }
       else
       {
        pevent.Graphics.DrawString(&quot;Document does not contain any pages.&quot;, this.Font, brush2, base.ClientRectangle, format);
       }

       goto Label_04D7;
      }
      finally
      {
       brush2.Dispose();
       format.Dispose();
      }
     }

     base.BeginInvoke(new MethodInvoker(this.CalculatePageInfo));
    }
    else
    {
     Point point2 = new Point();

     if (!this.layoutOk)
     {
      this.ComputeLayout();
     }

     Size size = new Size(PixelsToPhysical(new Point(base.Size), this.screendpi));
     Point point = new Point(this.VirtualSize);

     point2 = new Point(Math.Max(0, (base.Size.Width - point.X) / 2), Math.Max(0, (base.Size.Height - point.Y) / 2));
     point2.X = point2.X - this.Position.X;
     point2.Y = point2.Y - this.Position.Y;

     this.lastOffset = point2;

     int num = PhysicalToPixels(10, this.screendpi.X);
     int num2 = PhysicalToPixels(10, this.screendpi.Y);
     Region clip = pevent.Graphics.Clip;
     Rectangle[] rectangleArray = new Rectangle[this.rows * this.columns];
     Point empty = Point.Empty;
     int num3 = 0;

     try
     {
      for (int j = 0; j &lt; this.rows; j++)
      {
       empty.X = 0;
       empty.Y = num3 * j;

       for (int k = 0; k &lt; this.columns; k++)
       {
        int index = (this.StartPage + k) + (j * this.columns);

        if (index &lt; this.pageInfo.Length)
        {
         Size physicalSize = this.pageInfo[index].PhysicalSize;

         if (this.autoZoom)
         {
          double num7 = (size.Width - (10 * (this.columns + 1))) / ((double)(this.columns * physicalSize.Width));
          double num8 = (size.Height - (10 * (this.rows + 1))) / ((double)(this.rows * physicalSize.Height));
          this.zoom = Math.Min(num7, num8);
         }

         this.imageSize = new Size((int)(this.zoom * physicalSize.Width), (int)(this.zoom * physicalSize.Height));

         Point point4 = PhysicalToPixels(new Point(this.imageSize), this.screendpi);
         int x = (point2.X + (num * (k + 1))) + empty.X;
         int y = (point2.Y + (num2 * (j + 1))) + empty.Y;

         empty.X += point4.X;
         num3 = Math.Max(num3, point4.Y);
         rectangleArray[index - this.StartPage] = new Rectangle(x, y, point4.X, point4.Y);

         pevent.Graphics.ExcludeClip(rectangleArray[index - this.StartPage]);
        }
       }
      }

      pevent.Graphics.FillRectangle(brush, base.ClientRectangle);
     }
     finally
     {
      pevent.Graphics.Clip = clip;
     }

     for (int i = 0; i &lt; rectangleArray.Length; i++)
     {
      if ((i + this.StartPage) &lt; this.pageInfo.Length)
      {
       Rectangle rect = rectangleArray[i];
       pevent.Graphics.DrawRectangle(Pens.Black, rect);
       pevent.Graphics.FillRectangle(new SolidBrush(this.ForeColor), rect);

       rect.Inflate(-1, -1);

       if (this.pageInfo[i + this.StartPage].Image != null)
       {
        pevent.Graphics.DrawImage(this.pageInfo[i + this.StartPage].Image, rect);
       }

       rect.Width--;
       rect.Height--;

       pevent.Graphics.DrawRectangle(Pens.Black, rect);
      }
     }
    }
   }

  Label_04D7:
   base.OnPaint(pevent);
  }

  protected override void OnResize(EventArgs eventargs)
  {
   this.InvalidateLayout();
   base.OnResize(eventargs);
  }

  protected virtual void OnStartPageChanged(EventArgs e)
  {
   EventHandler handler = base.Events[EVENT_STARTPAGECHANGED] as EventHandler;
   if (handler != null)
   {
    handler(this, e);
   }
  }

  private static Point PhysicalToPixels(Point physical, Point dpi)
  {
   return new Point(PhysicalToPixels(physical.X, dpi.X), PhysicalToPixels(physical.Y, dpi.Y));
  }

  private static Size PhysicalToPixels(Size physicalSize, Point dpi)
  {
   return new Size(PhysicalToPixels(physicalSize.Width, dpi.X), PhysicalToPixels(physicalSize.Height, dpi.Y));
  }

  private static int PhysicalToPixels(int physicalSize, int dpi)
  {
   return (int)(((double)(physicalSize * dpi)) / 100.0);
  }

  private static Point PixelsToPhysical(Point pixels, Point dpi)
  {
   return new Point(PixelsToPhysical(pixels.X, dpi.X), PixelsToPhysical(pixels.Y, dpi.Y));
  }

  private static Size PixelsToPhysical(Size pixels, Point dpi)
  {
   return new Size(PixelsToPhysical(pixels.Width, dpi.X), PixelsToPhysical(pixels.Height, dpi.Y));
  }

  private static int PixelsToPhysical(int pixels, int dpi)
  {
   return (int)((pixels * 100.0) / ((double)dpi));
  }

  [EditorBrowsable(EditorBrowsableState.Never)]
  public override void ResetBackColor()
  {
   this.BackColor = SystemColors.AppWorkspace;
  }

  [EditorBrowsable(EditorBrowsableState.Never)]
  public override void ResetForeColor()
  {
   this.ForeColor = Color.White;
  }

  private void SetPositionNoInvalidate(Point value)
  {
   Point position = this.position;

   this.position = value;
   this.position.X = Math.Min(this.position.X, this.virtualSize.Width - base.Width);
   this.position.Y = Math.Min(this.position.Y, this.virtualSize.Height - base.Height);

   if (this.position.X &lt; 0)
   {
    this.position.X = 0;
   }
   if (this.position.Y &lt; 0)
   {
    this.position.Y = 0;
   }

   Rectangle clientRectangle = base.ClientRectangle;

   NativeMethods.RECT rectScrollRegion = NativeMethods.RECT.FromXYWH(clientRectangle.X, clientRectangle.Y, clientRectangle.Width, clientRectangle.Height);
   SafeNativeMethods.ScrollWindow(new HandleRef(this, base.Handle), position.X - this.position.X, position.Y - this.position.Y, ref rectScrollRegion, ref rectScrollRegion);
   UnsafeNativeMethods.SetScrollPos(new HandleRef(this, base.Handle), 0, this.position.X, true);
   UnsafeNativeMethods.SetScrollPos(new HandleRef(this, base.Handle), 1, this.position.Y, true);
  }

  internal void SetVirtualSizeNoInvalidate(Size value)
  {
   this.virtualSize = value;
   this.SetPositionNoInvalidate(this.position);

   NativeMethods.SCROLLINFO si = new NativeMethods.SCROLLINFO
   {
    fMask = 3,
    nMin = 0,
    nMax = Math.Max(base.Height, this.virtualSize.Height) - 1,
    nPage = base.Height
   };

   UnsafeNativeMethods.SetScrollInfo(new HandleRef(this, base.Handle), 1, si, true);

   si.fMask = 3;
   si.nMin = 0;
   si.nMax = Math.Max(base.Width, this.virtualSize.Width) - 1;
   si.nPage = base.Width;

   UnsafeNativeMethods.SetScrollInfo(new HandleRef(this, base.Handle), 0, si, true);
  }
    
  private void WmHScroll(ref Message m)
  {
   if (m.LParam != IntPtr.Zero)
   {
    base.WndProc(ref m);
   }
   else
   {
    Point position = this.position;
    int x = position.X;
    int maxPos = Math.Max(base.Width, this.virtualSize.Width);
    position.X = this.AdjustScroll(m, x, maxPos, true);
    this.Position = position;
   }
  }

  private void WmKeyDown(ref Message msg)
  {
   Keys keys = ((Keys)((int)msg.WParam)) | Control.ModifierKeys;
   Point position = this.Position;
   int x = 0;
   int num2 = 0;
   switch ((keys &amp; Keys.KeyCode))
   {
    case Keys.Prior:
     if ((keys &amp; ~Keys.KeyCode) != Keys.Control)
     {
      if (this.StartPage &gt; 0)
      {
       this.StartPage--;
      }
      return;
     }
     x = position.X;
     if (x &lt;= 100)
     {
      x = 0;
      break;
     }
     x -= 100;
     break;

    case Keys.Next:
     if ((keys &amp; ~Keys.KeyCode) != Keys.Control)
     {
      if (this.StartPage &lt; this.pageInfo.Length)
      {
       this.StartPage++;
      }
      return;
     }
     x = position.X;
     num2 = Math.Max(base.Width, this.virtualSize.Width);
     if (x &gt;= (num2 - 100))
     {
      x = num2;
     }
     else
     {
      x += 100;
     }
     position.X = x;
     this.Position = position;
     return;

    case Keys.End:
     if ((keys &amp; ~Keys.KeyCode) == Keys.Control)
     {
      this.StartPage = this.pageInfo.Length;
     }
     return;

    case Keys.Home:
     if ((keys &amp; ~Keys.KeyCode) == Keys.Control)
     {
      this.StartPage = 0;
     }
     return;

    case Keys.Left:
     x = position.X;
     if (x &lt;= 5)
     {
      x = 0;
     }
     else
     {
      x -= 5;
     }
     position.X = x;
     this.Position = position;
     return;

    case Keys.Up:
     x = position.Y;
     if (x &lt;= 5)
     {
      x = 0;
     }
     else
     {
      x -= 5;
     }
     position.Y = x;
     this.Position = position;
     return;

    case Keys.Right:
     x = position.X;
     num2 = Math.Max(base.Width, this.virtualSize.Width);
     if (x &gt;= (num2 - 5))
     {
      x = num2;
     }
     else
     {
      x += 5;
     }
     position.X = x;
     this.Position = position;
     return;

    case Keys.Down:
     x = position.Y;
     num2 = Math.Max(base.Height, this.virtualSize.Height);
     if (x &gt;= (num2 - 5))
     {
      x = num2;
     }
     else
     {
      x += 5;
     }
     position.Y = x;
     this.Position = position;
     return;

    default:
     return;
   }

   position.X = x;
   this.Position = position;
  }

  private void WmVScroll(ref Message m)
  {
   if (m.LParam != IntPtr.Zero)
   {
    base.WndProc(ref m);
   }
   else
   {
    Point position = this.Position;
    int y = position.Y;
    int maxPos = Math.Max(base.Height, this.virtualSize.Height);

    position.Y = this.AdjustScroll(m, y, maxPos, false);

    this.Position = position;
   }
  }

  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  protected override void WndProc(ref Message m)
  {
   switch (m.Msg)
   {
    case 0x114:
     this.WmHScroll(ref m);
     return;

    case 0x115:
     this.WmVScroll(ref m);
     return;

    case 0x100:
     this.WmKeyDown(ref m);
     return;
   }
   base.WndProc(ref m);
  }

  [DefaultValue(true)]
  public bool AutoZoom
  {
   get
   {
    return this.autoZoom;
   }
   set
   {
    if (this.autoZoom != value)
    {
     this.autoZoom = value;
     this.InvalidateLayout();
    }
   }
  }

  [DefaultValue(1)]
  public int Columns
  {
   get
   {
    return this.columns;
   }
   set
   {
    if (value &lt; 1)
    {
     object[] args = new object[] { &quot;Columns&quot;, value.ToString(CultureInfo.CurrentCulture), 1.ToString(CultureInfo.CurrentCulture) };
     throw new ArgumentOutOfRangeException(&quot;Columns&quot;, String.Format(&quot;InvalidLowBoundArgument=Value of &#39;{1}&#39; is not valid for &#39;{0}&#39;. &#39;{0}&#39; must be greater than {2}.&quot;, args));
    }

    this.columns = value;
    this.InvalidateLayout();
   }
  }

  protected override CreateParams CreateParams
  {
   [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
   get
   {
    CreateParams createParams = base.CreateParams;
    createParams.Style |= 0x100000;
    createParams.Style |= 0x200000;
    return createParams;
   }
  }

  [DefaultValue((string)null)]
  public PrintDocument Document
  {
   get
   {
    return this.document;
   }
   set
   {
    this.document = value;
   }
  }

  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
  private Point Position
  {
   get
   {
    return this.position;
   }
   set
   {
    this.SetPositionNoInvalidate(value);
   }
  }

  [AmbientValue(2), Localizable(true)]
  public override RightToLeft RightToLeft
  {
   get
   {
    return base.RightToLeft;
   }
   set
   {
    base.RightToLeft = value;
    this.InvalidatePreview();
   }
  }

  [DefaultValue(1)]
  public int Rows
  {
   get
   {
    return this.rows;
   }
   set
   {
    if (value &lt; 1)
    {
     object[] args = new object[] { &quot;Rows&quot;, value.ToString(CultureInfo.CurrentCulture), 1.ToString(CultureInfo.CurrentCulture) };
     throw new ArgumentOutOfRangeException(&quot;Rows&quot;, String.Format(&quot;InvalidLowBoundArgumentEx=Value of &#39;{1}&#39; is not valid for &#39;{0}&#39;. &#39;{0}&#39; must be greater than or equal to {2}.&quot;, args));
    }
    this.rows = value;
    this.InvalidateLayout();
   }
  }

  [DefaultValue(0)]
  public int StartPage
  {
   get
   {
    int startPage = this.startPage;

    if (this.pageInfo != null)
    {
     startPage = Math.Min(startPage, this.pageInfo.Length - (this.rows * this.columns));
    }

    return Math.Max(startPage, 0);
   }
   set
   {
    if (value &lt; 0)
    {
     object[] args = new object[] { &quot;StartPage&quot;, value.ToString(CultureInfo.CurrentCulture), 0.ToString(CultureInfo.CurrentCulture) };
     throw new ArgumentOutOfRangeException(&quot;StartPage&quot;, String.Format(&quot;InvalidLowBoundArgumentEx=Value of &#39;{1}&#39; is not valid for &#39;{0}&#39;. &#39;{0}&#39; must be greater than or equal to {2}.&quot;, args));
    }

    int startPage = this.StartPage;
    this.startPage = value;

    if (startPage != this.startPage)
    {
     this.InvalidateLayout();
     this.OnStartPageChanged(EventArgs.Empty);
    }
   }
  }

  [Bindable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  public override string Text
  {
   get
   {
    return base.Text;
   }
   set
   {
    base.Text = value;
   }
  }

  [DefaultValue(false)]
  public bool UseAntiAlias
  {
   get
   {
    return this.antiAlias;
   }
   set
   {
    this.antiAlias = value;
   }
  }

  [EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
  private Size VirtualSize
  {
   get
   {
    return this.virtualSize;
   }
   set
   {
    this.SetVirtualSizeNoInvalidate(value);
    base.Invalidate();
   }
  }

  [DefaultValue((double)0.3)]
  public double Zoom
  {
   get
   {
    return this.zoom;
   }
   set
   {
    if (value &lt;= 0.0)
    {
     throw new ArgumentException(&quot;PrintPreviewControlZoomNegative=Zoom must be 0 or greater. Negative values are not permitted.&quot;);
    }

    this.autoZoom = false;
    this.zoom = value;

    this.InvalidateLayout();
   }
  }


  private static ContentAlignment anyRight = ContentAlignment.BottomRight | ContentAlignment.MiddleRight | ContentAlignment.TopRight;
  private static ContentAlignment anyBottom = ContentAlignment.BottomRight | ContentAlignment.BottomCenter | ContentAlignment.BottomLeft;
  private static ContentAlignment anyCenter = ContentAlignment.BottomCenter | ContentAlignment.MiddleCenter | ContentAlignment.TopCenter;
  private static ContentAlignment anyMiddle = ContentAlignment.MiddleRight | ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft;
 

  internal static StringAlignment TranslateAlignment(ContentAlignment align)
  {
   if ((align &amp; anyRight) != ((ContentAlignment)0))
   {
    return StringAlignment.Far;
   }

   if ((align &amp; anyCenter) != ((ContentAlignment)0))
   {
    return StringAlignment.Center;
   }

   return StringAlignment.Near;
  }

  internal static StringAlignment TranslateLineAlignment(ContentAlignment align)
  {
   if ((align &amp; anyBottom) != ((ContentAlignment)0))
   {
    return StringAlignment.Far;
   }

   if ((align &amp; anyMiddle) != ((ContentAlignment)0))
   {
    return StringAlignment.Center;
   }

   return StringAlignment.Near;
  } 


 }
}

&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://anotherdeveloper.blogspot.com/feeds/1438848394212492615/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://anotherdeveloper.blogspot.com/2012/01/how-to-hide-net-printpreviewcontrols.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/1438848394212492615'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/1438848394212492615'/><link rel='alternate' type='text/html' href='http://anotherdeveloper.blogspot.com/2012/01/how-to-hide-net-printpreviewcontrols.html' title='How-to Hide the .NET PrintPreviewControl&#39;s &quot;Generating Previews&quot; Dialog'/><author><name>David</name><uri>http://www.blogger.com/profile/17125105494896617758</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-6Yl_ErF3NNVSrB7IulUTHlDMmmh767BWPlSrmelnGa0OXxbSCs-grZU8h5xEc3YRXc99m0jycHOMcEx57pRp7h0KYYeFLhoBHSiihtzsxJW_hcExOm8xE9N5f3FVwA/s220/57949_1509560393038_1655215657_1271155_1556591_n2.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5763115508258462541.post-1101379919693060132</id><published>2011-04-23T08:33:00.000-07:00</published><updated>2011-04-23T09:13:21.411-07:00</updated><title type='text'>A New Way to Disable a Form&#39;s Controls</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;color: #38761d; font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;&lt;strong&gt;[C# - WinForms]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;I was working on a new project and I came across a re-occurring problem that has always annoyed the crap out of me; disabling all of the controls on a form when an operation is working in the background. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;None of these standard methods really worked for me:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;1) Form.Enabled = false&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;This not only causes all of the controls to be disabled but it also completely disabled the form and, most importantly,&amp;nbsp;the ability to close the form.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;2) Looping each control and disabling it&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;Not too bad but I was using themed controls on my form and some of them looked horrible when disabled. This was not going to work either.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;3) Extending the Form class and disabling mouse and keyboard input when needed.&lt;/b&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;This wouldn&#39;t work because I needed, at the minimum, mouse input to cancel the background operation. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;So, I decided to do something different. I created a custom control based off&amp;nbsp;the Panel control to overlay the portion of the screen that I wanted &quot;disabled&quot; and made it take a quick snapshot of the screen underneath when it rendered.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;I simply dropped the panel on my form,&amp;nbsp;covered the area that I wanted to disable during the background operation, sent the panel to the back of the control hierarchy and set the visibility to false. Then, in my button&#39;s onclick event, I enabled the form&#39;s wait cursor and simply brought the panel to the front and turned the visibility on.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;this.UseWaitCursor = true;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;transparentPanel1.BringToFront();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;transparentPanel1.Visible = true;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;It worked great! I now have an easy way to disable my form without changing the appearance or writing a ton of code to disable and re-enable&amp;nbsp;all of&amp;nbsp;the controls.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Helvetica Neue&amp;quot;, Arial, Helvetica, sans-serif;&quot;&gt;Here is the code for the TransparentPanel&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

public partial class TransparentPanel : Panel
{   

 public TransparentPanel()
 {
  InitializeComponent();
 }

 public TransparentPanel(IContainer container)
 {
  container.Add(this);

  InitializeComponent();
 }

  
 protected override void OnPaintBackground(PaintEventArgs e)
 {
  Bitmap bgCache = new Bitmap(this.Width, this.Height);

  using (Graphics g = Graphics.FromImage(bgCache))
  {
   if (this.Parent == null || this.DesignMode)
   {
    //No parent or the control is in the designer.
    //Just paint the control normally
    g.FillRectangle(SystemBrushes.Control, 0, 0, this.Width, this.Height);
   }
   else
   {
    Point p2 = this.Parent.PointToScreen(this.Location);
    g.CopyFromScreen(p2.X, p2.Y, 0, 0, new Size(this.Width, this.Height));
   }
  }

  e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
  e.Graphics.InterpolationMode = InterpolationMode.High;
  e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

  e.Graphics.DrawImage(bgCache, 0, 0);   
 }
}

&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://anotherdeveloper.blogspot.com/feeds/1101379919693060132/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://anotherdeveloper.blogspot.com/2011/04/c-winforms-disable-forms-with-fake.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/1101379919693060132'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5763115508258462541/posts/default/1101379919693060132'/><link rel='alternate' type='text/html' href='http://anotherdeveloper.blogspot.com/2011/04/c-winforms-disable-forms-with-fake.html' title='A New Way to Disable a Form&#39;s Controls'/><author><name>David</name><uri>http://www.blogger.com/profile/17125105494896617758</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-6Yl_ErF3NNVSrB7IulUTHlDMmmh767BWPlSrmelnGa0OXxbSCs-grZU8h5xEc3YRXc99m0jycHOMcEx57pRp7h0KYYeFLhoBHSiihtzsxJW_hcExOm8xE9N5f3FVwA/s220/57949_1509560393038_1655215657_1271155_1556591_n2.jpg'/></author><thr:total>0</thr:total></entry></feed>