ListView.AfterLabelEdit 事件

当用户编辑项的标签时发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event AfterLabelEdit As LabelEditEventHandler
用法
Dim instance As ListView
Dim handler As LabelEditEventHandler

AddHandler instance.AfterLabelEdit, handler
public event LabelEditEventHandler AfterLabelEdit
public:
event LabelEditEventHandler^ AfterLabelEdit {
    void add (LabelEditEventHandler^ value);
    void remove (LabelEditEventHandler^ value);
}
/** @event */
public void add_AfterLabelEdit (LabelEditEventHandler value)

/** @event */
public void remove_AfterLabelEdit (LabelEditEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

AfterLabelEdit 事件在用户完成对项文本的修改时发生。用户为项键入的新字符串被传递至事件,事件处理程序可以拒绝此更改。如果事件处理程序拒绝更改,则文本恢复为用户开始编辑以前的项文本。

提示

因为 ListView.AfterLabelEdit 事件发生在提交标签编辑前,所以在此事件的处理程序中调用 ListView.Sort 方法时将根据原始值对项进行排序。

要引发 AfterLabelEdit 事件,ListView 控件的 LabelEdit 属性必须设置为 true

您可以为 BeforeLabelEdit 事件创建事件处理程序,在用户编辑项文本之前执行任务。

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例演示如何使用 AfterLabelEdit 事件将新编辑的标签限制为字母字符。此示例使用 ASCIIEncoding 类来获取新标签的每个字符的 ASCII 字符代码。如果该字符处于表示数字的 ASCII 代码之间,则新标签就不能应用于该项。此示例要求已在窗体上创建了一个 ListView 控件并向其添加了项。该示例还要求已将 AfterLabelEdit 事件与此代码示例中定义的事件处理程序建立了联系。若要使用 ASCIIEncoding 类,您的文件必须包含 System.Text 命名空间。

Private Sub MyListView_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.LabelEditEventArgs) Handles listView1.AfterLabelEdit

   ' Determine if label is changed by checking to see if it is equal to Nothing.
   If e.Label = Nothing Then
      Return
   End If
   ' ASCIIEncoding is used to determine if a number character has been entered.
   Dim AE As New ASCIIEncoding()
   ' Convert the new label to a character array.
   Dim temp As Char() = e.Label.ToCharArray()

   ' Check each character in the new label to determine if it is a number.
   Dim x As Integer
   For x = 0 To temp.Length - 1
      ' Encode the character from the character array to its ASCII code.
      Dim bc As Byte() = AE.GetBytes(temp(x).ToString())

      ' Determine if the ASCII code is within the valid range of numerical values.
      If bc(0) > 47 And bc(0) < 58 Then
         ' 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
      End If
   Next x
End Sub
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 0.
      if ( e->Label == nullptr )
               return;

      // ASCIIEncoding is used to determine if a number character has been entered.
      ASCIIEncoding^ AE = gcnew ASCIIEncoding;

      // Convert the new label to a character array.
      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.
         array<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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ListView 类
ListView 成员
System.Windows.Forms 命名空间
OnAfterLabelEdit
BeforeLabelEdit
LabelEditEventHandler 委托
ListView.LabelEdit 属性