Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 20:58pm    
It all strongly depends on the type of the control. What is "TextBox"? Which one? Full type name, please.
—SA

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

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


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,

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


.. 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[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 21:03pm    
With WPF, the problem is the easiest. But first, you did not provide a complete solution yet. OP needs to handle even on entering Enter and probably fire another event to notify that it happened. Second, we don't know what OP is using. I suggest we always wait until user answer our questions if we need clarification. In this case, without the clarification on the exact TextBox type, we do need it.
—SA
Afzaal Ahmad Zeeshan 21-Sep-14 3:01am    
The event is to move to the next line, and that I guess is the job of the Enter key. :-) Isn't it? The default behaviour of the TextBox control is to deny the Enter key to move to the next line. You can override that using the attribute in the markup or the C# backend code.
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:
C#
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;
}
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900