BLOG.CSHARPHELPER.COM

Make a PictureBox that acts like a button in C#

This program uses several images to make a PictureBox behave like a button. These images give you complete control over how the "button" looks as it is pressed and released.

The button should have these behaviors:

  • When the mouse is not over the button and the button is not pressed, the button should display the up image (left image).
  • When the mouse presses down on it, the button should display the down image (middle image).
  • If the mouse is pressed over the button and then leaves the button, the button should display the up image.
  • If the pressed mouse re-enters the button, it should display the down image.
  • When the mouse moves over the button while not pressed, the button should display the highlight image (right image).
  • If the user releases the mouse while over the button, the button should raise a Click event.
  • If the user releases the mouse while not over the button, the button should not raise any event.

First, at design time, I set the PictureBox's Image property to the button up image. I also set its BackgroundColor to Transparent.

The button displays the following images, which are stored in application properties:

  • ButtonUp - The up image.
  • ButtonDown - The down image.
  • ButtonMouseOver - The highlight image.
  • ButtonMask - A mask that is black for pixels that are non-transparent in the ButtonUp and ButtonDown images.

At desiogn time, I stored the up, down, mouse over, and mask images in application properties called ButtonUp, ButtonDown, ButtonMouseOver, and ButtonMask.

The following code executes when the user presses the mouse down over the PictureBox.

// Keep track of whether the button is pressed.
private bool ClickMeButtonIsPressed = false;

// Display the button down image.
private void picClickMe_MouseDown(object sender, MouseEventArgs e)
{
    // See if the mouse is over the masked area.
    if (MouseIsOverButton(e.Location))
    {
        ClickMeButtonIsPressed = true;
        picClickMe.Image = Properties.Resources.ButtonDown;
    }
}

The ClickMeButtonIsPressed variable keeps track of whether the button is pressed. (Initially it is not pressed.)

When the mouse is pressed down over the button, the MouseDown event handler executes. It calls the MouseIsOverButton method described shortly to see if the mouse is over a non-transparent part of the button. (For example, if the button is a circle, the PictureBox will receive this event even if the user clicks inside the PictureBox's rectangular area but outside of the circle.)

If the mouse is over the button's non-transparent area, the code sets ClickMeButtonIsPressed to true and displays the down button image.

When the user releases the mouse, the following code executes.

// Display the button up image.
private void picClickMe_MouseUp(object sender, MouseEventArgs e)
{
    ClickMeButtonIsPressed = false;
    picClickMe.Image = Properties.Resources.ButtonUp;
}

This code simply sets ClickMeButtonIsPressed to false and displays the up image. The code doesn't actually need to do anything to raise any kind of click event. Instead the program can simply catch the PictureBox's MouseClick event as shown in the following code.

// The button has been clicked.
private void picClickMe_MouseClick(object sender, MouseEventArgs e)
{
    // See if the mouse is over the masked area.
    if (MouseIsOverButton(e.Location))
    {
        MessageBox.Show("Clicked");
    }
}

This event occurs when the user presses the mouse down over the control and then releases the mouse while over the control. The only thing the code needs to add is a call to MouseIsOverButton to see whether the mouse is over a non-transparent part of the control. The following code shows the MouseIsOverButton method.

// Return true if the mouse is over the button's masked area.
private bool MouseIsOverButton(Point location)
{
    // Make sure the location is over the image.
    if (location.X < 0) return false;
    if (location.Y < 0) return false;
    if (location.X >= Properties.Resources.ButtonMask.Width) return false;
    if (location.Y >= Properties.Resources.ButtonMask.Height) return false;

    // See if the mask pixel at this position is black.
    Color color =
        Properties.Resources.ButtonMask.GetPixel(
            location.X, location.Y);
    return ((color.A == 255) &&
            (color.R == 0) &&
            (color.G == 0) &&
            (color.B == 0));
}

This method first determines whether the mouse is over the control and, if it is not, it returns false.

If the mouse is over the control, the method examines the image stored in the ButtonMask property. It gets the pixel at the mouse's position and compares it to black. If the pixel is black, then the method returns true to indicate the mouse is over the button. If the pixel is not black, the method returns false.

The final piece of the program is the following MouseMove event handler.

// If the button is pressed, display the appropriate image.
private void picClickMe_MouseMove(object sender, MouseEventArgs e)
{
    // The picture the button should have.
    Image desired_picture = Properties.Resources.ButtonUp;

    // See if the mouse is over the button's masked area.
    if (MouseIsOverButton(e.Location))
    {
        // The mouse is over the masked area.
        // See if the mouse is pressed.
        if (ClickMeButtonIsPressed)
            desired_picture = Properties.Resources.ButtonDown;
        else
            desired_picture = Properties.Resources.ButtonMouseOver;
    }
    else
    {
        // The mouse is not over the masked area.
        // The button should be in the up position.
        desired_picture = Properties.Resources.ButtonUp;
    }

    // See if we need to change the button image.
    if (picClickMe.Image != desired_picture)
        picClickMe.Image = desired_picture;
}

The variable desired_picture keeps track of the picture that the button should display. Initially the method assumes the button should display the up image.

If the mouse is over the button, then code checks ClickMeButtonIsPressed to see if the button is currently pressed. If it is, then the program should display the ButtonDown image.

If the mouse is over the button but ClickMeButtonIsPressed indicates the mouse is not pressed, then the program should display the ButtonMouseOver image.

If the mouse is not over the button, the program should display the ButtonUp image.

After deciding which image it should display, the program compares that image to the one currently displayed. If the desired image is not displayed, the code displays it.

That's all there is to this example. It's fairly simple but produces a nice customized effect.

Note that you can create much more elaborate customizations if you use WPF/XAML/Silverlight/whatever it's called these days. In those programs you can customize all of the buttons in an application consistently (although not exactly easily).

(Note that this example only handles the button-like behavior and doesn't mess with other behaviors of true buttons such as handling focus when the user tabs onto the button.)

   

See if the internet is available in C#

There are many ways you can test your network to see if the internet is available. Some may think the network is connected if your local network is available but the internet is not. Even if you avoid that sort of problem, there's always the chance that the network will go down (or come back up) right after you test it so the program can never be completely sure the internet is there. Even if the network really is there, the site you want to visit may be down.

The best method for testing the internet is to simply attempt whatever task you need to perform and see if it works.

However, if your network connection is usually reasonably stable, it may be useful to know if it's worth even trying to use the internet. The following IsInternetConnected method uses ping to see if it can reach the Google server in a reasonably efficient manner.

private bool IsInternetConnected(int timeout)
{
    try
    {
        Ping ping = new Ping();
        String host = "google.com";
        PingReply reply = ping.Send(host, timeout);
        return (reply.Status == IPStatus.Success);
    }
    catch
    {
        return false;
    }
}

The method sends a ping request to the host google.com. (You can use some other server if you like. For example, if your program will be accessing a company server, you may as well use that so you can tell if that server is available.)

The PingReply object's Status property indicates whether the server responded to the ping request within the timeout period (which is in milliseconds).

   

Use reflection to list a class's events in C#

This example uses the following code to add some simple events to the Form1 class.

// Make some events.
public delegate int MyPublicDelegate(string index);
public event MyPublicDelegate MyPublicEvent;

private delegate int MyPrivateDelegate(string index);
private event MyPrivateDelegate MyPrivateEvent;

public virtual event MyPublicDelegate MyVirtualEvent;

When the program's form loads, the following code displays information about the class's events.

// List the events.
// Use the class you want to study instead of Form1.
EventInfo[] event_infos = typeof(Form1).GetEvents(
    BindingFlags.FlattenHierarchy |
    BindingFlags.Instance |
    BindingFlags.NonPublic |
    BindingFlags.Public |
    BindingFlags.Static);
foreach (EventInfo info in event_infos)
{
    string properties = "";
    if (info.EventHandlerType.IsNotPublic) properties += " private";
    if (info.EventHandlerType.IsPublic) properties += " public";
    if (info.EventHandlerType.IsSealed) properties += " sealed";

    if (properties.Length > 0) properties = properties.Substring(1);
    ListViewMakeRow(lvwEvents,
        info.Name,
        info.EventHandlerType.Attributes.ToString(),
        properties);
}

The code gets the Form1 type and calls its GetEvents method to get information about the class's events. See the example Use reflection to list a class's properties in C# for information about the BindingFlags parameters.

The code then loops through the EventInfo objects that GetEvents returns. The code checks a series of EventInfo properties to get information about the field's accessibility and builds a string describing the values.

The code finishes by calling the ListViewMakeRow method to display the information in the form's ListView control. The EventInfo object's Name property gives the method's name. The EventHandlerType.Attributes property returns information about the event's attributes.

Download the example program to see how the ListViewMakeRow method works and for other details.

   

Book Review: Python for Kids: A Playful Introduction to Programming

[5 stars] A great book, but is it the one for you?
Python for Kids: A Playful Introduction to Programming
By Jason R. Briggs
344 pages
No Starch Press (December 19, 2012)
ISBN-13: 978-1593274078
This book is exactly what it says it is: a kid-friendly introduction to programming with Python. It explains concepts in simple terms and doesn't assume you understand anything about programming. It doesn't even assume you know anything about some useful mathematical concepts such as angles and the modulus operator. I think even kids could use this book to learn to program.

The one place where I think the book may have problems is with motivation. Kids (or anyone for that matter) want to produce something fun and interesting quickly. Unfortunately to learn to program you need to learn the syntax for a lot of operations--performing calculations, repeating code, building classes, and so forth. It's important and the book does a great job of explaining it in simple terms but it's not exactly fun, despite the kid-friendly jokes sprinkled throughout the text. The book finishes by building two games that are pretty fun, but it's a long way from page 1 to those games.

In contrast, the book Super Scratch Programming Adventure!: Learn to Program By Making Cool Games is shorter, has colorful pages, and explains how to use the Scratch programming language to write games. Scratch uses drag-and-drop programming and that book dives quickly into graphics and the other elements you need to write simple games.

"Python for Kids" provides a better introduction to the real world of programming (it's taught at many colleges and universities) but "Super Scratch Programming Adventure!" is more engaging. You'll have to decide which book is right for you. Perhaps both?

Even though this book is kid-friendly, it still provides good coverage of the Python programming language so there's no reason why it wouldn't make a great introduction for adults, too!

Use reflection to list a class's methods in C#

This example uses the following code to add some simple methods to the Form1 class.

// Add some methods.
private void MyPrivateMethod(string arg0, string arg2) { }
public void MyPublicMethod(float float0, double double0) { }
private void MyOverloadedMethod() { }
private void MyOverloadedMethod(int arg0) { }
protected void MyProtectedMethod() { }

When the program's form loads, the following code displays information about the class's methods.

// List the methods.
// Use the class you want to study instead of Form1.
MethodInfo[] method_infos = typeof(Form1).GetMethods(
    BindingFlags.FlattenHierarchy |
    BindingFlags.Instance |
    BindingFlags.NonPublic |
    BindingFlags.Public |
    BindingFlags.Static);
foreach (MethodInfo info in method_infos)
{
    // Make a list of properties.
    string properties = "";
    if (info.IsConstructor) properties += " ctor";
    if (info.IsAbstract) properties += " abstract";
    if (info.IsPrivate) properties += " private";
    if (info.IsPublic) properties += " public";
    if (info.IsFamily) properties += " protected";
    if (info.IsFamilyAndAssembly) properties += " protected AND internal";
    if (info.IsFamilyOrAssembly) properties += " protected OR internal";
    if (info.IsFinal) properties += " sealed";
    if (info.IsGenericMethod) properties += " generic";
    if (info.IsStatic) properties += " static";
    if (info.IsVirtual) properties += " virtual";

    if (properties.Length > 0) properties = properties.Substring(1);
    ListViewMakeRow(lvwMethods,
        info.Name,
        info.ToString(),
        properties);
}

The code gets the Form1 type and calls its GetMethods method to get information about the class's methods. See the example Use reflection to list a class's properties in C# for information about the BindingFlags parameters.

The code then loops through the MethodInfo objects that GetMethods returns. The code checks a series of MethodInfo properties to get information about the field's accessibility and builds a string describing the values.

The code finishes by calling the ListViewMakeRow method to display the information in the form's ListView control. The MethodInfo object's Name property gives the method's name. The MethodInfo object's ToString method returns the method with its signature as in "Void MyOverloadedMethod(Int32)."

Download the example program to see how the ListViewMakeRow method works and for other details.

   

Use reflection to list the fields provided by the SystemInformation class in C#

The example Use reflection to list a class's fields in C# shows how to list the fields defined by a class. This example uses the techniques described in that example to list the few fields defined by the SystemInformation class.

When the program's form loads, the following code displays information about the class's properties.

// List the fields.
object property_value;
FieldInfo[] field_infos = typeof(SystemInformation).GetFields(
    BindingFlags.FlattenHierarchy |
    BindingFlags.Instance |
    BindingFlags.NonPublic |
    BindingFlags.Public |
    BindingFlags.Static);
foreach (FieldInfo info in field_infos)
{
    string name = info.Name;
    string attributes = info.FieldType.Attributes.ToString();

    string visibility = "";
    if (info.IsAssembly) visibility += " assembly";
    if (info.IsFamily) visibility += " family";
    if (info.IsFamilyAndAssembly) visibility += " family AND assembly";
    if (info.IsFamilyOrAssembly) visibility += " family OR assembly";
    if (info.IsInitOnly) visibility += " init";
    if (info.IsLiteral) visibility += " literal";
    if (info.IsPrivate) visibility += " private";
    if (info.IsPublic) visibility += " public";
    if (info.IsStatic) visibility += " static";
    if (visibility.Length > 0) visibility = visibility.Substring(1);

    string value = "";

    // See if it's an array.
    if (!info.FieldType.IsArray)
    {
        // It's not an array.
        property_value = info.GetValue(this);
        if (property_value == null)
            value = "";
        else
            value = property_value.ToString();
    }
    else
    {
        // It is an array.
        name += "[]";
        // If it's an array of integers, get the values.
        if (info.FieldType.Name == "Int32[]")
        {
            Int32[] values = (Int32[])info.GetValue(this);
            if (values == null)
            {
                value = "";
            }
            else
            {
                foreach (Int32 val in values)
                    value += ", " + val.ToString();
                if (value.Length > 0) value = value.Substring(2);
                value = "[" + value + "]";
            }
        }
        else
        {
            value = "";
        }
    }

    ListViewMakeRow(lvwProperties, name,
        info.FieldType.ToString(),
        attributes, visibility, value);
}

The code gets the SystemInformation type and calls its GetFields method to get information about the class's fields. See the example Use reflection to list a class's properties in C# for information about the BindingFlags parameters.

The code then loops through the FieldInfo objects that GetFields returns. The code uses the FieldInfo object's Name property to get the field's name. It uses the object's FieldType.Attributes property to get information about the field's attributes.

Next the code checks a series of FieldInfo properties to get information about the field's accessibility and build a string describing the values.

The code then uses methods similar to those used by the earlier example to display the field's value. This example also shows how to show the values in an integer array. If the field's type name is Int32[] and the code can get its value, then the program loops through the field's value (which is an array) and adds its entries to a string.

The code finishes by calling the ListViewMakeRow method to display the information in the form's ListView control. Download the example program to see how that works.

   

Use reflection to list a class's fields in C#

Even though fields and properties are very similar when you interact with an item in code, to C# they are different. One side effect of that is that the GetProperties method provided by reflection lists only properties, not fields. Fortunately the GetFields method works in a similar manner, although many of the details are different.

This example uses the following code to add some fields to the Form1 class.

// Add some fields.
private int MyPrivateField = 1;
public int MyPublicField = 2;
public static int MyPublicStaticField = 3;
public static int MyProtectedField = 4;
public const int MyConstField = 5;
public int[] MyArray = { 1, 2, 3, 4, 5 };
public int[] MyEmptyArray = { };
public int[] MyNullArray;

When the program's form loads, the following code displays information about the class's fields.

// List the fields.
// Use the class you want to study instead of Form1.
object property_value;
FieldInfo[] field_infos = typeof(Form1).GetFields(
    BindingFlags.FlattenHierarchy |
    BindingFlags.Instance |
    BindingFlags.NonPublic |
    BindingFlags.Public |
    BindingFlags.Static);
foreach (FieldInfo info in field_infos)
{
    string name = info.Name;
    string attributes = info.FieldType.Attributes.ToString();

    string visibility = "";
    if (info.IsAssembly) visibility += " assembly";
    if (info.IsFamily) visibility += " family";
    if (info.IsFamilyAndAssembly) visibility += " family AND assembly";
    if (info.IsFamilyOrAssembly) visibility += " family OR assembly";
    if (info.IsInitOnly) visibility += " init";
    if (info.IsLiteral) visibility += " literal";
    if (info.IsPrivate) visibility += " private";
    if (info.IsPublic) visibility += " public";
    if (info.IsStatic) visibility += " static";
    if (visibility.Length > 0) visibility = visibility.Substring(1);

    string value = "";

    // See if it's an array.
    if (!info.FieldType.IsArray)
    {
        // It's not an array.
        property_value = info.GetValue(this);
        if (property_value == null)
            value = "<null>";
        else
            value = property_value.ToString();
    }
    else
    {
        // It is an array.
        name += "[]";
        // If it's an array of integers, get the values.
        if (info.FieldType.Name == "Int32[]")
        {
            Int32[] values = (Int32[])info.GetValue(this);
            if (values == null)
            {
                value = "<null>";
            }
            else
            {
                foreach (Int32 val in values)
                    value += ", " + val.ToString();
                if (value.Length > 0) value = value.Substring(2);
                value = "[" + value + "]";
            }
        }
        else
        {
            value = "<array>";
        }
    }

    ListViewMakeRow(lvwProperties, name,
        info.FieldType.ToString(),
        attributes, visibility, value);
}

The code gets the Form1 type and calls its GetFields method to get information about the class's fields. See the example Use reflection to list a class's properties in C# for information about the BindingFlags parameters.

The code then loops through the FieldInfo objects that GetFields returns. The code uses the FieldInfo object's Name property to get the field's name. It uses the object's FieldType.Attributes property to get information about the field's attributes.

Next the code checks a series of FieldInfo properties to get information about the field's accessibility and build a string describing the values.

The code then uses methods similar to those used by the earlier example to display the field's value. This example also shows how to show the values in an integer array. If the field's type name is Int32[] and the code can get its value, then the program loops through the field's value (which is an array) and adds its entries to a string.

The code finishes by calling the ListViewMakeRow method to display the information in the form's ListView control. Download the example program to see how that works.

   

Use reflection to list the properties provided by the SystemInformation class in C#

The SystemInformation class is chock full of useful properties that give information about system parameters. These include such values as the thickness of a text caret, the size of menu buttons, the default font, and the name of the computer. The example Use reflection to list a class's properties in C# explains how to list a class's properties. This example uses the reflection techniques explained in the previous post to discover the properties provided by SystemInformation and their values.

When the program's form loads, the following code displays information about the class's properties.

// List the SystemInformation class's properties.
object property_value;
PropertyInfo[] property_infos =
        typeof(SystemInformation).GetProperties(
    BindingFlags.FlattenHierarchy |
    BindingFlags.Instance |
    BindingFlags.NonPublic |
    BindingFlags.Public |
    BindingFlags.Static);
foreach (PropertyInfo info in property_infos)
{
    string name = info.Name;
    string value = "";

    // See if it's an array.
    if (!info.PropertyType.IsArray)
    {
        // It's not an array.
        if (info.CanRead) property_value = info.GetValue(this, null);
        else property_value = "---";

        if (property_value == null)
            value = "";
        else
            value = property_value.ToString();
    }
    else
    {
        // It is an array.
        name += "[]";
        value = "";
    }

    ListViewMakeRow(lvwProperties, name, value);
}

The code gets the SystemInformation class's type and calls its GetProperties method to get information about the class's properties. See the example Use reflection to list a class's properties in C# for information about the BindingFlags parameters.

The code then loops through the PropertyInfo objects that GetProperties returns. The code gets the properties' names and values, and calls the ListViewMakeRow method to display the information in the form's ListView control. Download the example program to see how the ListViewMakeRow method works and for other details.

   

Congratulations to winners of the book drawing

Congratulations to the following winners of the drawing for copies of Essential Algorithms: A Practical Approach to Computer Algorithms. The lucky winners are:

  • Aaron Waters
  • Cristian Forchino
  • Eric Howard
  • Geoff Hirst
  • Harold Short
  • J. Nanthan
  • Ken Ramme
  • Oscar Vargas
  • Richard Moss
  • Tony Ropson

(If your name is on this list and you didn't give me your postal address when you entered the drawing, send it to me ASAP. If I don't receive your address in the next few days, I may not send you a copy.)

Use reflection to list a class's properties in C#

This post begins a short series on using reflection. The tools that make C# work (such as the .NET Framework and the compiler that turns C# into IL code) use techniques that require them to learn a lot about the classes (and other things such as structs and enums) that go into a program. To make that possible, the whole system instruments classes so they know a lot about themselves. That lets you use reflection to query a class's type to learn about it.

Reflection lets you discover such things as the properties, fields, methods, and events that the class provides. It also lets you learn other information about those items such as their data type, accessibility (public, private), and attributes. It lets you learn about your own classes and even classes that are pre-defined in the .NET Framework or in other libraries. It even lets you examine base classes. For example, a Windows Forms program usually has a Form1 class that inherits from the System.Windows.Form class. Reflection lets you see any properties, methods, and events that you added to the Form1 class, in addition to properties, methods, and events inherited from the Form class.

Often you don't need to use reflection. If you're writing the code, you know what you put into it. Even when you're using a class that you didn't write, such as a .NET Framework class, you can often use the documentation and IntelliSense to figure out how to use it. Sometimes, however, reflection can be useful for learning about undocumented features. I have used it occasionally to see what's inside libraries that I've been working with that didn't come with good documentation.

This example shows how to list a class's properties. The example includes a lot of code dealing with displaying values in a ListView control. The following discussion omits that code and only shows the code that deals with reflection. Download the example program to see all the rest of the details.

To make using reflection easier, start with the following using statement.

using System.Reflection;

This example uses reflection to examine the Form1 class. To make it a bit easier to see the effects of different kinds of property declarations, the program uses the following code to add several properties to the class.

// Add some properties.
private int _MyPrivateProperty;
private int MyPrivateProperty
{
    get { return _MyPrivateProperty; }
    set { _MyPrivateProperty = value; }
}
public int MyPublicProperty
{
    get { return 2; }
    set { }
}
public static int MyPublicStaticProperty
{
    get { return 3; }
}
protected int MyProtectedProperty
{
    get { return 4; }
}
public virtual int MyPublicVirtualProperty
{
    set { }
}

All of the properties' names begin with My so they all appear together in the program's display.

This code defines:

  • A private property with a typical backing variable.
  • A public property that always returns the value 2.
  • A public read-only static property.
  • A protected read-only property.
  • A public virtual property.

When the form loads, the following code executes to display information about the Form1 class's properties including those defined by the previous code. (The ListViewMakeRow method just displays values in the program's ListView control. It doesn't really have anything to do with reflection so you can ignore it for now.)

// List the properties.
// Use the class you want to study instead of Form1.
object property_value;
PropertyInfo[] property_infos = typeof(Form1).GetProperties(
    BindingFlags.FlattenHierarchy |
    BindingFlags.Instance |
    BindingFlags.NonPublic |
    BindingFlags.Public |
    BindingFlags.Static);
foreach (PropertyInfo info in property_infos)
{
    string name = info.Name;
    string attributes = info.PropertyType.Attributes.ToString();
    if (info.CanRead) attributes += " get";
    if (info.CanWrite) attributes += " set";

    string value = "";

    // See if it's an array.
    if (!info.PropertyType.IsArray)
    {
        // It's not an array.
        if (info.CanRead) property_value = info.GetValue(this, null);
        else property_value = "---";

        if (property_value == null)
            value = "<null>";
        else
            value = property_value.ToString();
    }
    else
    {
        // It is an array.
        name += "[]";
        value = "<array>";
    }

    ListViewMakeRow(lvwProperties, name,
        info.PropertyType.ToString(),
        attributes, value);
}

The code starts by using typeof(Form1) to get a System.Type object representing the Form1 class. It calls that object's GetProperties method to get information about the properties defiend by the class. It passes GetProperties values to tell what information to return. The BindingFlag values that this code uses are:

  • FlattenHierarchy - Return information about properties that are inherited from parent classes
  • Instance - Return information about instance (non-static) properties
  • NonPublic - Return information about non-public properties
  • Public - Return information about public properties
  • Static - Return information about static properties

The code loops through the PropertyInfo objects returned by GetProperties. It uses the following PropertyInfo properties:

  • Name - Gives the property's name
  • PropertyType.Attributes - Gives information about the property's attributes
  • CanRead - True if the property is readable
  • CanWrite - True if the property is writable

Next the code tries to get the property's value. This can be somewhat tricky depending on the property's data type and whether it is an array.

If the PropertyType.IsArray property indicates the property is not an array, the code uses the CanRead to see if it can read the value. If the program can read the value, it uses the PropertyInfo object's GetValue method to get the value for the current form object (this). If the program cannot read the value, it displays the value ---. If the program got a property value object and that object is not null, the code calls its ToString method to convert it into a string and displays the result.

If the IsArray method indicates that the property is an array, the code adds brackets around the property's name and displays its value as <array>.

Finally the call to ListViewMakeRow displays the property's information in the program's ListView control. Download the example program to see how that works.

   

Calendar

September 2013
SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
2930

Subscribe


Blog Software
Blog Software