ListView.AfterLabelEdit 事件
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当用户编辑项的标签时发生。
public:
event System::Windows::Forms::LabelEditEventHandler ^ AfterLabelEdit;
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 事件将新编辑的标签限制为字母表中的字符。 该示例使用 ASCIIEncoding 类获取新标签的每个字符的 ASCII 字符代码。 如果字符位于表示数字的 ASCII 代码之间,则新标签无法应用于该项。 此示例要求你在窗体上创建了一个 ListView 控件,并为其添加了项。 此示例还要求已将 AfterLabelEdit 事件连接到示例代码中定义的事件处理程序。 若要使用 ASCIIEncoding 类,文件必须包含 System.Text 命名空间。
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当用户修改完项的文本时,将发生 该事件。 用户为项键入的新字符串将传递给 事件,事件处理程序可以拒绝更改。 如果事件处理程序拒绝更改,则文本将恢复为用户开始编辑项之前的文本。
备注
由于事件 ListView.AfterLabelEdit 发生在提交标签编辑之前,因此在此事件的处理程序中调用 ListView.Sort 方法将使用原始值对项进行排序。
若要 AfterLabelEdit 引发 事件, LabelEdit 必须将 控件的 ListView 属性设置为 true
。
可以为事件创建事件处理程序, BeforeLabelEdit 以便在用户编辑项的文本之前执行任务。
有关处理事件的详细信息,请参阅 处理和引发事件。
产品 | 版本 |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9, 10 |