多行文本转到下一行,但最后一行是空的,然后转到下一行 [英] Multi line text go to next line but last st line is empty then go to next line

查看:87
本文介绍了多行文本转到下一行,但最后一行是空的,然后转到下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是

i使用多行文本框以及如何设置输入时按Enter然后转到下一行但如果Textbox最后一行文本为空则转到C#中的Next控制焦点窗户申请表格



我的尝试:



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:

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);
                    }
                }
            }

推荐答案

不要使用 SendKeys 。使用关键事件模拟​​进行UI开发通常被视为滥用;我同意这一点。你的例子只是一个证明这一点。



在你的情况下,只需使用 nextControl.Focus(),因为这是你真正想要实现的目标。这里, nextControl 可以是对表单或其他控件的控件成员的引用,即一些固定控件,但它也可以自动找到下一个控件,下一个一个在子控件的Tab键顺序中:

Control.GetNextControl方法(控制,布尔)(System.Windows.Forms) [ ^ ]。



-SA
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


这可以通过uBound函数和string.split函数轻松完成..

希望这个帮助。示例代码在这里..



'全局变量..

dim c as integer = 0



'事件......



如果e.KeyCode = Keys.Enter那么



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))

如果ltext.Trim.Length = 0然后

c + = 1

否则

c = 0

结束如果



如果c = 2那么

textbox2.Focus()



c = 0

结束如果







结束如果
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


这篇关于多行文本转到下一行,但最后一行是空的,然后转到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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