监听KeyUp
事件,Btn_Sender
为发送按钮
/// <summary>
/// 监听换行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
//Shift+Enter换行
if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.Enter){}
//发送
else if (e.KeyCode == Keys.Enter)
{
//取消回车符
var str = txt_Sender.Text;
if (str.EndsWith(Environment.NewLine))
{
txt_Sender.Text = str.Substring(0, str.Length - Environment.NewLine.Length);
Btn_Sender_Click(null, null);
}
}
}
上述的if()...else if()...
便于理解,等同于如下
//Shift+Enter换行
//发送
if (!((e.Modifiers == Keys.Shift && e.KeyCode == Keys.Enter) || e.KeyCode != Keys.Enter))
{
//取消回车符
var str = txt_Sender.Text;
if (str.EndsWith(Environment.NewLine))
{
txt_Sender.Text = str.Substring(0, str.Length - Environment.NewLine.Length);
Btn_Sender_Click(null, null);
}
}