如何在 VB.NET 中的每个文本框行后添加文本 [英] How to add text after every textbox line in VB.NET

查看:51
本文介绍了如何在 VB.NET 中的每个文本框行后添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处找,找不到方法.我想找到一种在每个文本框行后添加文本的方法,但我找不到方法.我有一个 textbox1:

I've searched everywhere, I can't find a way. I want to find a way to add text after every textbox line but I can't find a way to do it. I have a textbox1 with:

example1
example2
example3
And so on...

和另一个带有@gmail.com 的 textbox2

and another textbox2 with @gmail.com

我希望将 textbox2 添加到 textbox1 中每一行的末尾,例如:

I want the textbox2 to be added to the end of every line in textbox1 like:

example1@gmail.com
example2@gmail.com
example3@gmail.com
And so on...

有什么办法吗?提前致谢.

Any way to do it? Thanks in advance.

推荐答案

这个方案简洁,去除空行.

This solution is concise, and removes empty lines.

Private Function appendTextToOtherTextLines(textToAppend As String, otherText As String) As String
    Return String.Join(Environment.NewLine, otherText.
                       Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries).
                       Select(Function(s) s & textToAppend))
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox3.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub

这是您的示例工作

如果你有一个空行,它会在结果字符串中被删除

And if you had an empty line, it is removed in the resulting string

当然,你也可以覆盖原来的文本框,但注意不要点击两次按钮!

Of course, you could overwrite the original textbox instead, but careful not to click the button twice!

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub

其他选项是一个事件处理程序,它会在新行末尾按回车键时自动发生.这仅在您主动手动输入行时有用.

Other option is an event handler which will make this happen automatically when pressing enter at the end of a new line. This is only useful if you are actively entering the lines manually.

Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    If e.KeyCode = Keys.Enter Then
        TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2) & TextBox2.Text & Environment.NewLine
        TextBox1.SelectionStart = TextBox1.Text.Length
    End If
End Sub

(这个选项在按回车键时需要一些纪律)

(this option requires some discipline when pressing enter)

这篇关于如何在 VB.NET 中的每个文本框行后添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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