Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's .NET Tips
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Summary
Great tips for .NET developers
Description

Sort FileInfo and DirectoryInfo objects
What do you do if you need to sort Files in a directory by, say, the date? Since FileInfo and DirectoryInfo objects do not implement the IComparable interface, they can not be used with the Array.Sort() method. Here's a link to an article that shows you how to create a class to handle this situation.
http://vb2themax.com/ShowContent.aspx?ID=38f32e71-ab8e-449d-9557-8417bd31c350

from a solution provided by Craig Boyd in Message #1020770

Determining DataBindings at Runtime
Suppose you have controls that can be databound to two different DataSets, depending on processing needs. How can you tell, at runtime, which DataSet the control is bound to? Since there are two different DataBinding syntaxes, there are actually two different comparisons you need to make:
DataSet ds = new DataSet();
if (this.txtTest.DataBindings.Count > 0)
{
   if (this.txtTest.DataBindings["Text"].DataSource is DataTable)
      ds = ((DataTable)this.txtTest.DataBindings["Text"].DataSource).DataSet;
   else
      if (this.txtTest.DataBindings["Text"].DataSource is DataSet)
      ds = (DataSet)this.txtTest.DataBindings["Text"].DataSource;
}

if (ds.Equals(this.MyFirstDataSet))
   MessageBox.Show("Number One");
if (ds.Equals(this.MySecondDataSet))
   MessageBox.Show("Number Two");
We could take this a step further and have a method call that returns a boolean value.
public bool BoundToDataSet(DataSet dsTest)
{
   DataSet ds = new DataSet();
   if (this.txtTest.DataBindings.Count > 0)
   {
      if (this.txtTest.DataBindings["Text"].DataSource is DataTable)
         ds = ((DataTable)this.txtTest.DataBindings["Text"].DataSource).DataSet;
      else
         if (this.txtTest.DataBindings["Text"].DataSource is DataSet)
         ds = (DataSet)this.txtTest.DataBindings["Text"].DataSource;
   }

   return ds.Equals(dsTest);
}

from a solution provided by Çetin Basöz in Message #1051904

Persistent Web Page ToolTips
How do you make persistent tooltips on a web page? By "persistent" I mean that the tooltip will remain visible as long as the mouse is positioned over the control. The answer is to use JavaScript. To see it in action, copy/paste this block of code, save it as an .html file and then browse it in your Browser.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <title>Tool Tip</title>
      <style> .MyToolTip { 
         border-right: black 1px solid; border-top: black 1px solid; 
         border-left: black 1px solid; border-bottom: black 1px solid;
         font-weight: normal; font-size: 10pt; font-style: normal; font-family: Tahoma;
         margin: 3px; vertical-align: baseline; text-transform: none;  
         color: dimgray; direction: ltr; line-height: normal;   
         letter-spacing: normal; position: absolute; background-color: papayawhip; 
         text-align: left; text-decoration: none; }
   </style>
      <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
      <meta name="ProgId" content="VisualStudio.HTML">
      <meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
      <script id="clientEventHandlersJS" language="javascript">
<!--

function Button1_onmouseover() {
   window.MyTip.style.display="block";
   window.MyTip.style.left = window.Button1.style.left + window.Button1.style.width;
   window.MyTip.style.top = window.Button1.style.top;
}

function HideToolTip() {
   window.MyTip.style.display="none";
}
//-->
      </script>
   </head>
   <body onload="return HideToolTip();">
      <INPUT id="Button1" type="button" value="Button" name="Button1" 
         onmouseover="return Button1_onmouseover();"
         onmouseout="return HideToolTip();">
      <div name="MyTip" class="MyToolTip" id="MyTip" style="LEFT: 3px; TOP: 48px">This is 
         my tooltip
         <br>
         This is the next line</div>
   </body>
</html>

from a solution provided by Dawa Tsering in Message #1024032

Starting a Windows Form application
When you create a new Windows Form Application in the Visual Studio IDE, it automatically sets up the new Form so that it is the entry point when the application starts, by including the static Main method. This isn't anything that needs to be in a Form, and can in fact be removed from that Form and placed in any class.

1. Create a new class in your project (name it MainEntryPoint.cs or whatever you want)
2. Put the following code in that file

using System;
using System.Windows.Forms;

namespace YourNamespaceForYourProject
{
   /// <summary>
   /// Summary description for Main.
   /// </summary>
   public class MainEntryPoint
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main() 
      {
         // I usually put a try...catch here just in case an exception makes it all the way down here 
         // (better safe than sorry <s>)
         try
         {
            // some code here before you start the form
            MessageBox.Show("good morning", "good morning");

            // start the form here
            Application.Run(new Form1());
         }
         catch(Exception e)
         {
            MessageBox.Show(e.ToString(), "Exception caught in Main()!");
         }
      }
   }
}

from a solution provided by Einar Kvandahl in Message #1023200

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers