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

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

Adding Items to the ToolBox
This is a pretty basic tip, but I see this getting asked all the time on a lot of forums.

Once you've created sub-classes for all your form controls (you know, MyTextBox, MyComboBox, etc.), then you need to put your new sub-classes into the IDE's ToolBox so that you can easily drag/drop them onto your design surface. Here's what you do:

First, you have to be in a designer window, not viewing code.
Right-click on the ToolBox, choose "Add/Remove Items" (in VS2005, it's "Choose Items").

When the Customize ToolBox dialog comes up (in VS2005, it's the "Choose Toolbox Items" dialog), click on the "Browse" button. Then browse to the .DLL that contains your base classes.

from a solution provided by Bonnie Berent in Message #1049463

Writing DataSets to Text Files
Writing a DataSet out to an XML file is pretty simple, given that the DataSet has a .WriteXML() method. But, how about if you want to write a CSV file?

Here's a link to an article that shows you how to do it:
http://www.dotnetspider.com/kb/Article963.aspx

However, since this link's URL has been changed at least once before from what had originally been posted in the UT message, there's no guarantee that they won't change it again. So, let me also post the code example, just in case:

using System;
using System.Data;
using System.IO;

   public class DataTableHelper
   {
      /// 
      /// Can stream DataTable to Browser, directly, you need to set 
      /// 
      /// Response.Clear();
      /// Response.Buffer= true;
      /// Response.ContentType = "application/vnd.ms-excel";
      /// Response.AddHeader("Content-Disposition", "inline;filename=Clientes.xls");
      /// Response.Charset = "";
      /// this.EnableViewState = false
      /// // ACTUAL CODE 
      /// ProduceCSV(dt, Response.Output, true);
      /// 
      public static void ProduceCSV(DataTable dt, System.IO.TextWriter httpStream, bool WriteHeader)
      {
         if (WriteHeader)
         {
            string[] arr = new String[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
               arr[i] = dt.Columns[i].ColumnName;
               arr[i] = GetWriteableValue(arr[i]);
            }

            httpStream.WriteLine(string.Join(",", arr));
         }

         for (int j = 0; j < dt.Rows.Count; j++)
         {
            string[] dataArr = new String[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
               object o = dt.Rows[j][i];
               dataArr[i] = GetWriteableValue(o);
            }
            httpStream.WriteLine(string.Join(",", dataArr));
         }
      }

      #region CSV Producer
      public static void ProduceCSV(DataTable dt, System.IO.StreamWriter file, bool WriteHeader)
      {
         if (WriteHeader)
         {
            string[] arr = new String[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
               arr[i] = dt.Columns[i].ColumnName;
               arr[i] = GetWriteableValue(arr[i]);
            }

            file.WriteLine(string.Join(",", arr));
         }

         for (int j = 0; j < dt.Rows.Count; j++)
         {
            string[] dataArr = new String[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
               object o = dt.Rows[j][i];
               dataArr[i] = GetWriteableValue(o);
            }
            file.WriteLine(string.Join(",", dataArr));
         }
      }

      public static string GetWriteableValue(object o)
      {
         if (o == null || o == Convert.DBNull)
            return "";
         else if (o.ToString().IndexOf(",") == -1)
            return o.ToString();
         else
            return "\"" + o.ToString() + "\"";

      }
      #endregion
   }

from a solution provided by Kevin McNeish in Message #1074509

Embedding Javascript in HTML
How do you imbed JavaScript, such as this, into your HTML?
 {script language="javascript"
  src="https://secure.company.com/javascript/trustme.js"
  type="text/javascript"}
 {/script}
It's best not to include the script in the header, because the header is hard to talk to.

Instead you should use:

Page.ClientScript.RegisterClientScriptBlock() 
// and 
Page.ClientScript.RegisterClientScriptBlockInclude() //(in 2.0)  
Page.RegisterClientScriptBlock()                     //(in 1.0) 
In 1.0 you can embed the Client Script Include link as a as a string which is not as clean as in 2.0, but it works.

It's best to isolate that code either into a control or class so that it's reusable but you can also do it in code directly.

from a solution provided by Rick Strahl in Message 1096678

How to Detect and/or force HTTPS
To detect HTTPS you can check for Request.ServerVariables("SERVER_PORT") and look for port 443 (or maybe something other than 80). Actually ServerVariables("SERVER_PORT_SECURE") is probably a better choice.

If it's not the secure port Redirect to the same URL with a new port. The easiest way to do that is to use the URI class and change the port.

Rick's Blog post has some related content that describes just this scenario:

http://west-wind.com/weblog/posts/1412.aspx

from a solution provided by Rick Strahl in Message 1096678

Using TreeViews with ContextMenus
Based on which Tree Node is selected, you can easily adjust the ContextMenu with code similar to this, using the MouseDown event:
private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
   TreeNode treenode = this.treeView1.GetNodeAt(e.X, e.Y);
   if (treenode != null)
   {
      if (e.Button == MouseButtons.Right)
      {
         this.PrepareContextMenu(treenode);
      }
   }
}
In addition to creating the ContextMenu items, the PrepareContextMenu() method can also do things like enable/disable the context menu items if you wish.

In 2.0, the ContextMenuStrip has an Opening event which can also be used to prepare the contextmenu before opening.

from a solution provided by Einar Kvandahl in Message #1101230

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, December 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, 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