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

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

New Line in a TextBox
There are several methods one can use to display multiple lines in a TextBox. If you recall, a new line is is simply a carriage return (ascii 13) and a line feed (ascii 10). Your choices are:
// The \r\n escape codes correspond to carriage return and line feed.
txtResult.Text = "Line1\r\nLine 2"
-or-
// The NewLine is good for portability between winforms and webforms.
txtResult.Text = "Line1"+System.Environment.NewLine+"Line 2"
-or-
// The @ escape character let's you write it exactly as it would display, similar to VFP's text/endtext.
TxtResult.Text = 
@"Line 1
Line2
";

from solutions provided by Einar Kvandahl and Çetin Basöz in Message #1030414 and Message #1030422

Be Sure a DataSet Has Changes
I see this question a lot. The data entered into a form's textboxes doesn't always get saved. Why not? Typically it happens because the changes to the row in question haven't been committed yet, .NET is still considering them "proposed" changes. I use a simple method in all my forms that I call before I attempt to save any of my DataSets. The method loops through all the rows looking for proposed changes and does an EndEdit() when it finds them:
protected virtual void ForceChangedRows(DataSet ds)
{
   if (ds == null)
      return;

   for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
   {
      for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
      {
         if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
         {
            ds.Tables[nTable].Rows[nRow].EndEdit();
         }
      }
   }
}
Now some of you reading this may say, well, why don't you just use the BindingContext[MyTable].EndCurrentEdit() method? I've found that this doesn't always work, although under which circumstances it fails I haven't been able to determine. So, I stick with tried and true methodology.

from a solution provided by Bonnie Berent in Message #1047186

WebForm Layout and Style Sheets
Three Questions (with answers) about WebForm design:

1. I gather that FlowLayout is preferred to GridLayout, especially for downlevel browsers. If so, it seems necessary to use tables to get a good looking layout for a data entry form. Is this true?

Not really, as long as you have a really good style sheet that you can assign various page elements to.

However, a lot of people still use tables for this kind of thing, especially if they're not taking full advantage of CSS. But CSS is not a replacement for tables. If you look at nearly any Web site online, you will find tables (or in some newer cases DIV tags) are the mainstay for layout control. CSS can be applied against tables to provide common layout features.

One could also use a mixture of the two using which ever approach is easier/cleaner.

2. A client has provided me with a fairly sophisticated CSS file which they'd like to see used as much as possible. I've included the .css in the project, but its styles don't seem to appear in the .style property sheet dropdowns for various controls/elements. Is VS capable of integrating the styles in this manner? Or, do I have to hand-code style info into the raw HTML?

Drop the style sheet on the design form - it will add a reference to it in the HTML. Use CssClass to set the CSS class that you want applied to that element. There isn't any drop-down for this (at least that I've seen), you just have to type in the class name.

3. In general, are CSSs well-supported in ASP.NET? Any tips or "best practices" on their use?

CSS is an HTML thing, so it just works. Learning CSS early on is one of the most productive things you can do for HTML design. Thankfully VS.NET uses styles almost exclusively, so the HTML it generates nudges you in that direction anyway.

ASP.NET has some issues with styles not displaying in User Controls, which is a bit of a problem in visualization, but there really isn't a good way around this in VS2003.

Bottom line: keep the HTML as clean and simple as possible, and use CSS styles wherever possible.

from solutions provided by Paul Mrozowski and Rick Strahl in Message #1021097 and Message #1021100

Detecting When a Form Closes
If a user clicks on the "X" in the upper right of a form to close it, how can we detect this in order to prompt the user for saving data, or to ask if they really want to exit the form or the application? Use the Closing event .... it provides a mechanism for cancelling the close if necessary.
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  e.Cancel = (MessageBox.Show("Some question", "Message",
    MessageBoxButtons.OKCancel,  MessageBoxIcon.Question )).ToString()
    != "OK";
}

from a solution provided by Hilmar Zonneveld in Message #1043118

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