如何添加下拉菜单,选择,在富文本字段中添加该文本

时间:2015-06-10 02:06:29

标签: visual-studio visual-studio-2013

我怎样才能做到这一点: 当我从下拉菜单中选择选项时,它会将文本添加到richtextbox中,例如:

            Drop Down Menu/ComboBox >            |Are Goood|

            richtextbox:
                        ___________________________________________________
                        | (output after selecting option from dropdownmenu |
                        |                                                  |
                        |    "Waffles Are Goood"                           |
                        | (Adds Entire Text, "Waffles Are Good")           |
                        ___________________________________________________

1 个答案:

答案 0 :(得分:2)

您使用ComboBox的SelectedIndexChanged事件(下拉菜单)。 然后尝试在事件函数上添加此代码。

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    richTextBox1.Text = main_text + " " + comboBox1.Text;
}

VB
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    RichTextBox1.Text = main_text + " " + ComboBox1.Text
End Sub

您需要创建一个字符串变量来存储来自richTextbox的文本

string main_text; -- C#
Dim main_text As String -- VB

在richtextbox上添加KeyPress事件

C#
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    main_text = richTextBox1.Text;
}

VB
Private Sub RichTextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
        main_text = RichTextBox1.Text
End Sub