雙向互動介面:ComboBox 選項 → Button 添加 → RichTextBox 顯示 → 另一 Button 刪除任意行。
// 添加按鈕點擊
private void btnAdd_Click(object sender, EventArgs e) {
if (comboBox1.SelectedItem != null) {
richTextBox1.AppendText(comboBox1.SelectedItem.ToString() + Environment.NewLine);
}
}
// 刪除按鈕點擊:刪除游標所在行
private void btnDelete_Click(object sender, EventArgs e) {
int lineIndex = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
if (lineIndex >= 0 && lineIndex < richTextBox1.Lines.Length) {
var lines = richTextBox1.Lines.ToList();
lines.RemoveAt(lineIndex);
richTextBox1.Lines = lines.ToArray();
}
}
ComboBox.SelectedItem:獲取當前選中項目物件。RichTextBox.AppendText:高效追加文字不觸發完整重繪。GetLineFromCharIndex + Lines 集合操作:精確定位並移除指定行。MessageBox.Show)。ContextMenuStrip 實現右鍵選單刪除,更符合直覺操作。