c#中的多行文本框 [英] multiline textbox in c#

查看:1356
本文介绍了c#中的多行文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有

i有多行文本框,我想要在文本框中写入的用户移动到下一行时发生火灾事件,在另一个表达式中我希望火灾事件时指针指针转到新行

关于

hello all
i have multi line text box and i want fire event when the user who write in the text box move to next line ,in another expression i want fire event when the indicator of the pointer move to the new line
regards

推荐答案

我想你想在TextBox控件中添加一个功能,以便移动到下一行用户按下回车键,对吗?



TextBox的默认行为是拒绝用户按Enter键转到下一行。也就是说,当用户按下回车键时,即使TextBox是多行(多行的足够高度),新行请求也会被拒绝。您可以通过向对象添加属性或C#属性来克服此行为。



AcceptsReturn 对象的属性决定了TextBox是否会接受用户移动到下一行的请求,或不。这取决于价值。如果将其设置为 true

,TextBox将允许用户现在移动到下一行。您不需要处理事件,TextBox会为您处理它。



希望你能使用WPF,你可以在Markup中做到这一点,比如



I think you want to add a feature to the TextBox control to move to the next line when the user presses enter key, right?

The default behaviour of the TextBox is to deny the user to go to the next line by pressing Enter key. That is when the user presses the enter key, even if the TextBox is multiline (enough height for multiple lines) the new line request would be denied. You can overcome this behaviour by adding an attribute or a C# property to the object.

AcceptsReturn attribute of the object determines whether the TextBox would accept the user's request to move to the next line, or not. It depends on the value. If you set it to true
the TextBox would allow the user now to move to the next line. You don't need to handle the event, TextBox would handle it for you.

Hopefully you'll be using WPF, you can do that in the Markup like

<textbox name="myTextBox" acceptsreturn="True" />





以上行包含和属性 AcceptsReturn 或禁用文本框以允许用户使用回车键移动到下一行。



AC#解决方案就像下面的代码一样,





The above line includes and attribute, AcceptsReturn which enables or disables the textbox to allow the user to move to the next line using the enter key.

A C# solution to this would be like the following code,

// myTextBox is the name of the TextBox control
// calling the AcceptsReturn property
// and setting its value to true.
myTextBox.AcceptsReturn = true;





..这将允许用户按下回车键后转到下一行。数据将保存在TextBox中,您可以使用 myTextBox.Text 属性获取该数据。它将是多行的(如果用户已添加多行数据)。



有关详细信息: http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.acceptsreturn (v = VS.95).ASPX [ ^ ]



.. this will now allow the user to go to the next line once he presses enter key. The data will be saved in the TextBox and you can get that data using myTextBox.Text property. It will be multiline (if user has added multiline data).

For more on this: http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.acceptsreturn(v=VS.95).ASPX[^]


假设这是Windows窗体:



1.正如Afzaal在他的解决方案中所建议的:在设计时将文本框的接受返回设置为真实或代码内。



2为TextBox写一个'TextChanged EventHandler,如下所示:
Assuming this is Windows Forms:

1. as Afzaal suggests in his solution: set 'AcceptsReturn of the TextBox to 'true at design-time, or in-code.

2. write a 'TextChanged EventHandler for the TextBox as shown:
private int currentLines;

private void textBox1_TextChanged(object sender, EventArgs e)
{
    int nLines = textBox1.Lines.Length;

    if (nLines != currentLines)
    {
        // do what you need to do
        numberOfLinesChanged(nLines, currentLines);
    }
}

private void numberOfLinesChanged(int newNumberOfLines, int oldNumberOfLines)
{
    // whatever ?

    // update current number of Lines ?
    currentLines = newNumberOfLines;
}

请记住,如果您将TextBox的WordWrap属性设置为'true:对用户显示的自动换行就好像有新行一样不会影响TextBox.Lines属性的值。

Keep in mind that if you have the WordWrap property of the TextBox set to 'true: that a word-wrap that appears to the user as if there is new line will not affect the value of the TextBox.Lines property.


这篇关于c#中的多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆