Click here to Skip to main content
16,018,347 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
my Question is
i use a multi line text box and how to set when typing i press enter then go to next line but if Textbox last line text is empty then go to Next control focus in C# windows Form Application

What I have tried:

C#
if ((!e.Control & e.KeyData == Keys.Return) | (!this.Multiline & (e.KeyCode == Keys.Down | e.KeyCode == Keys.Up)) | e.KeyData == (System.Windows.Forms.Keys.Return | System.Windows.Forms.Keys.Shift))
                {
                    string[] Mylines = this.Lines;

                    if (Mylines[Mylines .Length - 1 ] == string.Empty & this .Multiline == true )
                    {
                        SendKeys.Send("{Tab}");
                    }
                    else
                    {
                    
                    if (this.FindForm() != null)
                    {
                        e.SuppressKeyPress = true;
                        e.Handled = true;
                        this.FindForm().SelectNextControl(this, !(e.Shift || e.KeyCode == Keys.Up), true, true, true);
                        base.OnKeyDown(e);
                    }
                }
            }
Posted
v2

Don't use SendKeys. Using key event simulation for UI development is usually considered as abuse; and I agree with that. Your example is just one to justify that.

In your case, just use nextControl.Focus(), because this is what you really want to achieve. Here, nextControl could be a reference to a control member of your form or other control, that is, some fixed control, but it also can find the next control automatically, the next one in the tab order of child controls:
Control.GetNextControl Method (Control, Boolean) (System.Windows.Forms)[^].

—SA
 
Share this answer
 
this is done easily with uBound function and string.split function..
hope this help. Sample code is here..

'global variable..
dim c as integer=0

'event...

If e.KeyCode = Keys.Enter Then

Dim strin() As String = txt_pres_add.Text.Split(Environment.NewLine)

Dim lastline = txt_pres_add.Text.Split(Environment.NewLine)

Dim ltext As String
ltext = lastline(UBound(strin))
If ltext.Trim.Length = 0 Then
c += 1
Else
c = 0
End If

If c = 2 Then
textbox2.Focus()

c = 0
End If



End If
 
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