ListView.OnAfterLabelEdit 方法

引发 AfterLabelEdit 事件。

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

语法

声明
Protected Overridable Sub OnAfterLabelEdit ( _
    e As LabelEditEventArgs _
)
用法
Dim e As LabelEditEventArgs

Me.OnAfterLabelEdit(e)
protected virtual void OnAfterLabelEdit (
    LabelEditEventArgs e
)
protected:
virtual void OnAfterLabelEdit (
    LabelEditEventArgs^ e
)
protected void OnAfterLabelEdit (
    LabelEditEventArgs e
)
protected function OnAfterLabelEdit (
    e : LabelEditEventArgs
)

参数

备注

引发事件时会通过委托调用事件处理程序。有关更多信息,请参见 引发事件

OnAfterLabelEdit 方法还允许派生类对事件进行处理而不必附加委托。这是在派生类中处理事件的首选技术。

给继承者的说明 在派生类中重写 OnAfterLabelEdit 时,一定要调用基类的 OnAfterLabelEdit 方法,以便已注册的委托对事件进行接收。

示例

下面的代码示例演示如何使用 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 命名空间
AfterLabelEdit
LabelEditEventArgs 类