Export (0) Print
Expand All

ListView.OnAfterLabelEdit Method

Raises the AfterLabelEdit event.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

protected virtual void OnAfterLabelEdit (
	LabelEditEventArgs e
)
protected void OnAfterLabelEdit (
	LabelEditEventArgs e
)
protected function OnAfterLabelEdit (
	e : LabelEditEventArgs
)

Parameters

e

A LabelEditEventArgs that contains the event data.

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnAfterLabelEdit method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors When overriding OnAfterLabelEdit in a derived class, be sure to call the base class's OnAfterLabelEdit method so that registered delegates receive the event.

The following code example demonstrates how to use the AfterLabelEdit event to restrict a newly edited label to characters in the alphabet. The example uses the ASCIIEncoding class to obtain the ASCII character code of each character of the new label. If the character falls between the ASCII codes that represent numbers, the new label cannot be applied to the item. This example requires that you have created a ListView control on a form and added items to it. The example also requires that you have connected the AfterLabelEdit event to the event handler defined in the example code. In order to use the ASCIIEncoding class, your file must include the System.Text namespace.

private void MyListView_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e)
{
 
   // Determine if label is changed by checking for null.
   if (e.Label == null)
      return;

   // ASCIIEncoding is used to determine if a number character has been entered.
   ASCIIEncoding AE = new ASCIIEncoding();
   // Convert the new label to a character array.
   char[] temp = e.Label.ToCharArray();

   // Check each character in the new label to determine if it is a number.
   for(int x=0; x < temp.Length; x++)
   {
      // Encode the character from the character array to its ASCII code.
      byte[] bc = AE.GetBytes(temp[x].ToString());
   
      // Determine if the ASCII code is within the valid range of numerical values.
      if(bc[0] > 47 && bc[0] < 58)
      {
         // Cancel the event and return the lable to its original state.
         e.CancelEdit = true;
         // Display a MessageBox alerting the user that numbers are not allowed.
         MessageBox.Show ("The text for the item cannot contain numerical values.");
         // Break out of the loop and exit.
         return;
      }
   }
}

private void MyListView_AfterLabelEdit(Object sender, 
    System.Windows.Forms.LabelEditEventArgs e)
{
    // Determine if label is changed by checking for null.
    if (e.get_Label() == null) {
        return;
    }

    // ASCIIEncoding is used to determine 
    // if a number character has been entered.
    ASCIIEncoding ae = new ASCIIEncoding();

    // Convert the new label to a character array.
    char temp[] = e.get_Label().ToCharArray();

    // Check each character in the new label to determine if it is a number.
    for (int x = 0; x < temp.get_Length(); x++) {
        // Encode the character from the character array to its ASCII code.
        ubyte bc[] = ae.GetBytes(temp.get_Item(x).ToString());

        // Determine if the ASCII code is within the valid range 
        // of numerical values.
        if (Convert.ToInt32(bc.get_Item(0)) > 47 
            && Convert.ToInt32(bc.get_Item(0)) < 58) {
            // Cancel the event and return the lable to its original state.
            e.set_CancelEdit(true);

            // Display a MessageBox alerting the user that 
            // numbers are not allowed.
            MessageBox.Show("The text for the item cannot contain "
                + "numerical values.");

            // Break out of the loop and exit.
            return;
        }
    }
} //MyListView_AfterLabelEdit

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft