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

Bonnie Berent's Tips
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Summary
Great tips for .NET developers selected from the community by Bonnie Berent.
Description

Stopping a Windows Service before it starts
I created a Windows Service that reads values from a config file at startup. If these values are not valid, I don't want the service to start at all. How can I achieve this?

Quite simple actually ... in the OnStart method, simply throw an exception.

from a solution provided by Alexandre Palma in Message #1187639

DataRow Iterating
You can't modify a collection with an iterator like foreach. The following will give you the error "collection was modified":
// wrong way to do it
foreach(DataRow loRow in DataTable.Rows)
{
    if (loRow["MyField"] = string.Empty)
    {
        loRow.RejectChanges();
    }
}
Instead, use for() in reverse order.
// right way to do it
for(int i = myTable.Rows.Count-1; i >= 0; i--)
{
    if ((string)myTable.Rows[i]["MyField"] == String.Empty)
    {
        myTable.Rows[i].RejectChanges();
    }
}
Likewise for deleting a DataRow from the collection ... it must be done in reverse order.

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

Controlling the Submit Button on a WebForm
If you have multiple buttons on a WebForm, and you'd like to control which one acts as the "Submit" button (responds to the User pressing the "Enter" key), here's all you have to do: add an "onkeydown" attribute, as follows:
TextBox1.Attributes.Add("onkeydown", 
	"if(event.which || event.keyCode)
		{if ((event.which == 13) || (event.keyCode == 13)) 
			{document.getElementById('"+Button1.UniqueID+"').click();return false;}} 
	else {return true}; ");

from a solution provided by Mike Cole in Message #1203026

Hooking Into the Form's Events, from Controls on the Form
Following is one methodology one could use to accomplish hooking into Form events from a Control.

Let's use an example to illustrate this ... we'd like our Control to hook into the Form's Load and Closing events. Let's use the ListView as an example. First, we'll sub-class the ListView Control. As you can see we override the OnParentChanged event in the ListView sub-class.

We utilize the TopLevelControl (not the Parent). If the TopLevelControl is null then we hookup event handlers backwards through the control hierarchy as each control is parented. When we finally get to a TopLevelControl for a Form type we hookup the load/close event handlers.

public class MyListView : System.Windows.Forms.ListView
{
   private Form _parentForm = null;

   public MyListView()
   {
   }

   protected override void OnParentChanged(EventArgs e)
   {
      base.OnParentChanged(e);
			
      if (this._parentForm != null)
         return;

      if (this.DesignMode == false)
      {
         if (this.TopLevelControl != null && this.TopLevelControl is Form)
         {
            this._parentForm = (Form)this.TopLevelControl;
            this.EstablishParentEvents();
         }
         else
         {
            Control LastParent = this;

            while(LastParent != null)
            {
               LastParent.ParentChanged += new EventHandler(LastParent_ParentChanged);
               LastParent = LastParent.Parent;
            }
         }
      }
   }

   private void EstablishParentEvents()
   {
      this._parentForm.Closing += new CancelEventHandler(MyListView_Closing);
      this._parentForm.Load += new EventHandler(MyListView_Load);
   }

   private void MyListView_Closing(object sender, CancelEventArgs e)
   {
      MessageBox.Show("The parent form is closing!");

      if (this._parentForm != null)
      {
         this._parentForm.Closing -= new CancelEventHandler(MyListView_Closing);
         this._parentForm.Load -= new EventHandler(MyListView_Load);
      }
   }

   private void MyListView_Load(object sender, EventArgs e)
   {
      MessageBox.Show("The parent form is loading!");
   }

   private void LastParent_ParentChanged(object sender, EventArgs e)
   {
      Control Source = (Control)sender;

      if (Source.TopLevelControl != null && Source.TopLevelControl is Form)
      {
         this._parentForm = (Form)Source.TopLevelControl;
         this.EstablishParentEvents();
      }
   }
}

from a solution provided by Neil Tonkin in Message #1084683

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, 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, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers