[英]Pressing Enter Key will Add the Selected Item From ListBox to RichTextBox
與此主題相關的內容: 在RichTextBox中鍵入單詞時將出現隱藏列表框
我正在編寫代碼編輯器,我只想知道如何使用enterkey將列表框中的項目添加到文本框中。
繼續我的字符串:
public String[] ab = { "abstract" };
public String[] am = { "AmbientProperties", "AmbientValueAttribute" };
樣品:
在richtextbox(rtb)中,我鍵入Ab,然后hiddenlistbox將使用此代碼在其上顯示“抽象”文本(已經這樣做):
if (token == "letterA" || token.StartsWith("Ab") || token.StartsWith("ab"))
{
int length = line.Length - (index - start);
string commentText = rtb.Text.Substring(index, length);
rtb.SelectionStart = index;
rtb.SelectionLength = length;
lb.Visible = true;
KeyWord keywordsHint = new KeyWord();
foreach (string str in keywordsHint.ab)
{
lb.Items.Add(str);
}
break;
}
然后在按Enterkey之后,我想將列表框中的摘要添加到richtextbox。
RichTextBox聲明為rtb,ListBox聲明為lb
我該怎么辦? 謝謝 。
在按鍵事件中按下某些控件時,某些控件無法識別某些按鍵。 例如,ListBox無法識別按下的鍵是否為Enter鍵。
請參閱以下鏈接中的備注部分 - http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx
您的問題的解決方案之一可以是http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx
為列表框實現PreviewKeyDown事件,以便列表框識別您的操作。
以下是示例代碼段 -
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Do your task here :)
}
}
private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
e.IsInputKey = true;
break;
}
}
您無法直接在列表框中鍵入文本,因此我使用textBox創建了一個示例:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
this.richTextBox1.AppendText((sender as TextBox).Text);
e.Handled = true;
}
}
如果你的意思是comboBox,你可以很容易地調整它,替換上面的行:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
this.richTextBox1.AppendText((sender as ComboBox).Text);
e.Handled = true;
}
}
將選定的列表框條目復制到rtf框:
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
foreach (string s in listBox1.SelectedItems)
{
this.richTextBox1.AppendText(s + Environment.NewLine);
}
e.Handled = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.