我如何强制VB.net,一个ENTER创建一个新行,而不是关闭对话框?

时间:2021-04-29 13:55:22

I've built a dialogbox with a RichTextBox and I'd like to have the following behaviour:

我已经使用RichTextBox构建了一个对话框,我希望有以下行为:

I've got the focus (cursor) in the RichTextBox. When the ENTER key is pressed, then there should be a new line in the rich edit control created. The ENTER should NOT close the dialog box [as it does now :-( ].

我在RichTextBox中得到了焦点(光标)。按下ENTER键后,创建的富编辑控件中应该有一个新行。 ENTER不应该关闭对话框[就像现在这样:-(])。

Any Idea ?

任何的想法 ?

2 个解决方案

#1


If the form's AcceptButton property is assigned, referencing a button on the form, this will intercept the enter key presses. Make sure that the form does not have any AcceptButton assigned, and the text box should receive the enter key presses and behave as expected.

如果指定了表单的AcceptButton属性,引用表单上的按钮,则将截取回车键按下。确保表单没有分配任何AcceptButton,并且文本框应该按下回车键并按预期运行。

Update: If you want to have the behaviour of the AcceptButton and have the RichTextBox receiving the enter key presses when focused you can achieve this in two different ways. One is to set the KeyPreview property of the form to true, and adding the following code to the KeyPress event handler of the form:

更新:如果您想拥有AcceptButton的行为并让RichTextBox在聚焦时按下回车键,则可以通过两种不同的方式实现此目的。一种是将表单的KeyPreview属性设置为true,并将以下代码添加到表单的KeyPress事件处理程序:

private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter && this.ActiveControl != theRichTextBox)
    {
        this.DialogResult = DialogResult.OK;
    }
}

The other approach is to have the AcceptButton property assigned, pointing out a button (that in turn has its DialogResult property set to OK). Then you can add event handlers for the Enter and Leave events for the RichTextBox control that will temporarily un-assign the AcceptButton property of the form:

另一种方法是分配AcceptButton属性,指出一个按钮(然后将其DialogResult属性设置为OK)。然后,您可以为RichTextBox控件的Enter和Leave事件添加事件处理程序,该控件将临时取消分配表单的AcceptButton属性:

private void RichTextBox_Enter(object sender, EventArgs e)
{
    this.AcceptButton = null;
}

private void RichTextBox_Leave(object sender, EventArgs e)
{
    this.AcceptButton = btnAccept;
}

#2


You mean RichTextBox right? You need to set the AcceptsReturn property true.

你的意思是RichTextBox对吗?您需要将AcceptsReturn属性设置为true。

#1


If the form's AcceptButton property is assigned, referencing a button on the form, this will intercept the enter key presses. Make sure that the form does not have any AcceptButton assigned, and the text box should receive the enter key presses and behave as expected.

如果指定了表单的AcceptButton属性,引用表单上的按钮,则将截取回车键按下。确保表单没有分配任何AcceptButton,并且文本框应该按下回车键并按预期运行。

Update: If you want to have the behaviour of the AcceptButton and have the RichTextBox receiving the enter key presses when focused you can achieve this in two different ways. One is to set the KeyPreview property of the form to true, and adding the following code to the KeyPress event handler of the form:

更新:如果您想拥有AcceptButton的行为并让RichTextBox在聚焦时按下回车键,则可以通过两种不同的方式实现此目的。一种是将表单的KeyPreview属性设置为true,并将以下代码添加到表单的KeyPress事件处理程序:

private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter && this.ActiveControl != theRichTextBox)
    {
        this.DialogResult = DialogResult.OK;
    }
}

The other approach is to have the AcceptButton property assigned, pointing out a button (that in turn has its DialogResult property set to OK). Then you can add event handlers for the Enter and Leave events for the RichTextBox control that will temporarily un-assign the AcceptButton property of the form:

另一种方法是分配AcceptButton属性,指出一个按钮(然后将其DialogResult属性设置为OK)。然后,您可以为RichTextBox控件的Enter和Leave事件添加事件处理程序,该控件将临时取消分配表单的AcceptButton属性:

private void RichTextBox_Enter(object sender, EventArgs e)
{
    this.AcceptButton = null;
}

private void RichTextBox_Leave(object sender, EventArgs e)
{
    this.AcceptButton = btnAccept;
}

#2


You mean RichTextBox right? You need to set the AcceptsReturn property true.

你的意思是RichTextBox对吗?您需要将AcceptsReturn属性设置为true。