键盘响应代码:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
string strMessage = "textBox_KeyDown Event:" + e.RoutedEvent + " " + "Key:" + e.Key;
if(listBox!=null)
{
listBox.Items.Add(strMessage);
}
}
textBox内容变更消息响应代码:
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
string strMessage = "textBox_TextChanged Event:" + e.RoutedEvent;
if(listBox!=null)
{
listBox.Items.Add(strMessage);
}
}
显示右键菜单代码:
private void listBox_MouseRightButtonDown(object sender, MouseButtonEventArgs e)//右击弹出菜单
{
ContextMenu aMenu = new ContextMenu();
MenuItem deleteMenu = new MenuItem();
deleteMenu.Header = "清空";
deleteMenu.Click += btDel_Click;
aMenu.Items.Add(deleteMenu);
listBox.ContextMenu = aMenu;
}
private void btDel_Click(object sender, RoutedEventArgs e)
{
listBox.Items.Clear();
}
界面的Xmal代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="74*"/>
<ColumnDefinition Width="445*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30*"/>
<RowDefinition Height="263*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="请输入:" TextAlignment="Center" VerticalAlignment="Center" Grid.Row="0" Margin="0,7,0,6.6"/>
<TextBox x:Name="textBox" Grid.RowSpan="1" TextWrapping="Wrap" Text="TextBox" Grid.Column="1" VerticalAlignment="Center" KeyDown="textBox_KeyDown" TextChanged="textBox_TextChanged"/>
<TextBlock x:Name="textBlock2" HorizontalAlignment="Left" TextWrapping="Wrap" Text="事件记录:" TextAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Margin="0,6.4,0,5.6"/>
<ListBox x:Name="listBox" Grid.Row="1" Grid.Column="1" Margin="0,0.4,0.4,-0.2" MouseRightButtonDown="listBox_MouseRightButtonDown"/>
</Grid>