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

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

Default Values for Properties
In the Property Sheet, if you change the value of a Property, you can then right-click that Property and choose "Reset" to set it back to its Default Value. What code do you need in your own controls to get that to work? You need code in two places: in the Constructor to set the value to begin with, and a DefaultValue attribute for the Property itself:

public class MyTextBox : TextBox
{
    private string m_MyProperty;

    public MyTextBox
    {
        this.m_MyProperty = "";
        
        // As an additional bonus, I'm showing you two ways to do color
        this.BackColor = System.Drawing.Color.FromArgb(90, 100, 240);
        this.ForeColor = System.Drawing.Color.Firebrick; ;
    }

    [DefaultValue("")]
    public string MyProperty
    {
        get {return this.m_MyProperty;}
        set {this.m_MyProperty = value;}
    }
    [DefaultValue(typeof(System.Drawing.Color), "90,100,240")]
    public override System.Drawing.Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = value;
        }
    }
    [DefaultValue(typeof(System.Drawing.Color), "Firebrick")]
    public override System.Drawing.Color ForeColor
    {
        get
        {
            return base.ForeColor;
        }
        set
        {
            base.ForeColor = value;
        }
    }
}

Setting some of these properties doesn't always work as you would expect them to ... one that comes to mind is "Text" ... you may or may not have problems setting a default for that one, depending on which control you're sub-classing.

Also, some properties aren't virtual and can't be overridden. For those properties, you can use "new" instead of "override".

from a solution provided by Bonnie Berent in Messages #1196770 and #1197504

Copy Rows From One Table to Another
If you want to create a new DataTable with data from an existing DataTable, you can always use the DataTable.Copy() method. But, what if you want to copy that data (or even a subset of that data) to another existing DataTable? Here's an easy trick:
DataRow[] rows;

// To copy all the rows
rows = MyOldTable.Select();
MyDataSet.Merge(rows);

// To copy a subset
string FilterSubset = "code = '4'";
rows = MyOldTable.Select(FilterSubset);
MyDataSet.Merge(rows);

Unfortunately, there is one drawback to this approach ... this signature of the .Merge() method is only supported by DataSet (in other words, you can't use the DataTable.Merge() method).

But, if you're using Visual Studio 2005 ... then no worries mate! There's the new .ToTable() method to the rescue!

DataTable dtCopy = MyOriginalTable.DefaultView.ToTable();
MyNewTable.Merge(dtCopy);

from a solution provided by Mike Cole in Message #1178569

Bound ListBox/CheckBoxList Selected Property
Consider the following code, which works as is:

html src:

<asp:CheckBoxList ID="cblUsers" runat="server" >
  <asp:ListItem>Item 1</asp:ListItem><asp:ListItem>Item 2</asp:ListItem>
  <asp:ListItem>Item 3</asp:ListItem><asp:ListItem>Item 4</asp:ListItem>
</asp:CheckBoxList>
code behind:
        With cblUsers
            For x = 0 To .Items.Count - 1
                If .Items(x).Selected Then
                    Response.Write(.Items(x).Text & "<BR>")
                End If
            Next
        End With

BUT, if you add a DataSource to the List, the .Selected property no longer correctly represents the Selected items ... UNLESS you set the AppendDataBoundItems property to true:
cblUsers.AppendDataBoundItems = true

from a solution provided by Perry Forman in Message #1128475

Dynamic Color Changes in a WinForm Grid
VS 2005's new DataGridView provides an easy way to dynamically change colors by handling the CellFormating event.

Handlers for this event receive an argument of the DataGridViewCellFormattingEventArgs type. This object contains properties that let you determine the value of the cell being formatted along with its location in the DataGridView control. This object also contains a CellStyle property that is initialized to the value of the InheritedStyle property of the cell being formatted. You can modify the cell style properties to specify style information appropriate to the cell value and location.

As always, to paraphrase a common phrase, "a code snippet is worth a thousand words":

private void MyDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.RowIndex == 1)
    {
        e.CellStyle.BackColor = Color.Green;
    }
}

from a solution provided by Alexandre Palma in Message #1166923

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