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

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

How to Determine if Connected to the Internet (redux)
In the June column of .NET Tips, my predecessor, Cathi, had a Tip on how to determine if your computer is connected to the Internet. As usual, with .NET there's always more than one way to skin a cat. Here's a different method to determine the same thing.
public void TestInternetConnection()
{
   InternetConnectionState flags = 0; 
   
   bool bInternet = InternetGetConnectedState(ref flags,0); 
   if (bInternet == false) 
   { 
      //Not Connected 
      if (flags == 0) 
      { 
         MessageBox.Show("Do something to tell the user to install some internet connection");
      } 
   }
   else
   {
      MessageBox.Show("You should be connected to the internet");
   }

}

// Assumes you're using System.Runtime.InteropServices
[DllImport("wininet.dll",CharSet=CharSet.Auto)] 
static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved); 
[Flags] 
   enum InternetConnectionState: int 
{ 
   INTERNET_CONNECTION_MODEM = 0x1, 
   INTERNET_CONNECTION_LAN = 0x2, 
   INTERNET_CONNECTION_PROXY = 0x4, 
   INTERNET_RAS_INSTALLED = 0x10, 
   INTERNET_CONNECTION_OFFLINE = 0x20, 
   INTERNET_CONNECTION_CONFIGURED = 0x40 
} 

from a solution provided by Einar Kvandahl in Message #1025188

Insert Rows in a DataTable
There's more than one way to add Rows to a DataTable, but most people use the .Add() method which sticks the Row at the end of the table.
DataRow row = MyTable.NewRow();
row["MyColumn"] = Whatever";
MyTable.Rows.Add(row);
But, what do you do if you want to insert this row someplace other than at the bottom? Use the .InsertAt() method instead of the .Add().
// Inserts as the first row.
MyTable.Rows.InsertAt(row, 0);

from a solution provided by Jim Livermore in Message #1037931

Set Focus to a Control on a Web Form
To set focus on a Web Form to a control instead of the address bar, all that is needed is a little bit of Javascript.
<body onload="document.getElementById('txtCompany').focus()">
Let's take this one step further, by creating a property, call it FocusedControl, on our base Web Form class and overriding the OnPreRender event to handle it.
protected override void OnPreRender(EventArgs e)
{
   if (this.FocusedControl != null) 
      this.RegisterStartupScript("FocusedControl",
         "<script language='javascript'>\r\ndocument.getElementById('" + 
         this.FocusedControl.ID + "').focus();\r\n</script>\r\n");

   base.OnPreRender (e);
} 

from a solution provided by Rick Strahl in Message #1020611

Changing UserControl's Size
Once you've created a UserControl and dropped it on a Form, it's size is determined by the Control's .Size property as it was when the Control was originally dropped on the Form. If you decide to add to the UserControl later, thereby changing it's size, that new size will be reflected in any new place that you drop it, but the old instances of it will remain at the old size. That's because the .Size property was already set in the Form's InitializeComponent() method.

This can be good or bad, depending on the behavior you want to see. It's good because you may have other controls on the form and if the UserControl were to automatically change it's size, it would throw off the layout of other controls on your Form. However, sometimes you may want this behavior. You can add a property to your UserControl to determine if the user of your control wants it to auto-resize or not and then handle it in the UserControl's Load event.
private void BBControl_Load(object sender, System.EventArgs e)
{
   if (this.m_AllowResize)
      this.Size = new System.Drawing.Size(376, 184);
}

from a solution provided by Bonnie Berent in Message #1038813

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