ListView.AfterLabelEdit ListView.AfterLabelEdit ListView.AfterLabelEdit ListView.AfterLabelEdit Event

定义

当用户编辑项的标签时发生。Occurs when the label for an item is edited by the user.

public:
 event System::Windows::Forms::LabelEditEventHandler ^ AfterLabelEdit;
public event System.Windows.Forms.LabelEditEventHandler AfterLabelEdit;
member this.AfterLabelEdit : System.Windows.Forms.LabelEditEventHandler 
Public Custom Event AfterLabelEdit As LabelEditEventHandler 

示例

下面的代码示例演示如何使用AfterLabelEdit事件来限制对字母表中的字符的新编辑的标签。The following code example demonstrates how to use the AfterLabelEdit event to restrict a newly edited label to characters in the alphabet. 该示例使用ASCIIEncoding类来获取新标签的每个字符的 ASCII 字符代码。The example uses the ASCIIEncoding class to obtain the ASCII character code of each character of the new label. 如果该字符表示的数字的 ASCII 代码之间处于,新标签不能应用于项。If the character falls between the ASCII codes that represent numbers, the new label cannot be applied to the item. 此示例要求您已创建ListView控件在窗体和向其添加的项。This example requires that you have created a ListView control on a form and added items to it. 该示例还要求是否已连接AfterLabelEdit示例代码中定义的事件处理程序的事件。The example also requires that you have connected the AfterLabelEdit event to the event handler defined in the example code. 若要使用ASCIIEncoding类,你的文件必须包含System.Text命名空间。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 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.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 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 Is 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

注解

AfterLabelEdit事件发生在用户完成修改项的文本。The AfterLabelEdit event occurs when the user finishes modifying the text for an item. 在用户键入项的新字符串传递给事件,并且事件处理程序可以拒绝更改。The new string that the user types for the item is passed to the event, and the event handler can reject the change. 如果事件处理程序会拒绝更改,如前的用户开始编辑项文本将恢复为文本。If the event handler rejects the change, the text reverts to the text as it was before the user began editing the item.

备注

因为ListView.AfterLabelEdit事件发生之前就会提交,调用标签编辑ListView.Sort此事件的处理程序中的方法将对使用原始值的项进行排序。Because the ListView.AfterLabelEdit event takes place before the label edit is committed, calling the ListView.Sort method in a handler for this event will sort the item using the original value.

为了使AfterLabelEdit事件被引发LabelEdit的属性ListView控件必须设置为trueIn order for the AfterLabelEdit event to be raised, the LabelEdit property of the ListView control must be set to true.

可以创建的事件处理程序BeforeLabelEdit事件之前用户编辑项的文本执行任务。You can create an event handler for the BeforeLabelEdit event to perform tasks before the user edits the text of an item.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅