如何查看文本框包含的内容? [英] How can I check what a textbox contains?
问题描述
嘿伙计们
我在Visualbasic中做了一个IPv6 Shortner和Expandtool。
我怎么能检查一个文本框是否包含指定的charachter,如果有,则应该用其他东西替换。
所以我有一个Textbox ,如果文本框中有::,程序应将::替换为0000:0000
我该怎么做?
感谢您的帮助
textBox1.Text = textBox1.Text.Replace(::,0000:0000)在
验证
文本框的事件。
或者如果您希望它在用户输入时发生,则将其放入TextChanged
事件中 - 而不是我的最喜欢的。
我猜你可以有几个这样的我所以我整理了一下如下:
我创建了一个你要用它们替换它们的东西替换它们的字典,并在一个方便的地方填充它(在我的例子中是Form_Load)
< pre lang =vb> Dim transpose As Dictionary( 字符串,字符串)= 新字典( 字符串,字符串)
私有 Sub Form1_Load(sender As 系统m。对象,e As System.EventArgs)句柄 MyBase .Load
transpose.Add( ::, 0000:0000)
转置.Add( ;;, 另一个)
transpose.Add( ; @, andanother)
结束 Sub
然后我为字符串创建了一个扩展方法我放在项目中的Extensions.vb中的所有替换导入System.Runtime.CompilerServices
模块扩展s
< Extension()>公共函数ReplaceAll(ByVal InputValue As String,transpose As Dictionary(Of String,String))As String
Dim s As String = InputValue
For Each kvp As KeyValuePair(Of String,String)In transpose
s = s.Replace(kvp.Key,kvp.Value)
下一个
返回s
结束函数
结束模块然后我可以使用上述验证事件中的方法...
私有 Sub TextBox1_Validating( sender As System。 Object ,e As System.ComponentModel.CancelEventArgs)句柄 TextBox1.Validating
TextBox1.Text = TextBox1.Text.ReplaceAll(transpose)
结束 Sub请注意,您还需要
导入WindowsApplication2.Extensionsin Fo rm1.vb
从文本框中获取值作为字符串。然后,您可以使用任何字符串方法 [< a href =https://msdn.microsoft.com/en-us/library/system.string_methods(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]修改内容,并将其设置回文本框。
Hey guys
Im doing a IPv6 Shortner and Expandtool in Visualbasic.
How can I check if a textbox contains a specified charachter and if it does, it should be replaced by something else.
So I got a Textbox, and if there is "::" in the textbox, the program should replace the "::" with a "0000:0000"
How can I do that?
Thank you for your help
Put something liketextBox1.Text = textBox1.Text.Replace("::", "0000:0000")in the
Validating
event of the textbox.
Or if you want it to happen as the user types then put it in theTextChanged
event - not my favourite.
[EDIT]
I'm guessing you could have several of these so I've tidied this up a bit as follows:
I created a dictionary of things you want to replace with things you want to replace them with, and populated it in a convenient place (in my case Form_Load)
Dim transpose As Dictionary(Of String, String) = New Dictionary(Of String, String) Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load transpose.Add("::", "0000:0000") transpose.Add(";;", "another") transpose.Add(";@", "andanother") End Sub
I then created an extension method for strings to do all of the substitutions which I placed in Extensions.vb in my projectImports System.Runtime.CompilerServices Module Extensions <Extension()> Public Function ReplaceAll(ByVal InputValue As String, transpose As Dictionary(Of String, String)) As String Dim s As String = InputValue For Each kvp As KeyValuePair(Of String, String) In transpose s = s.Replace(kvp.Key, kvp.Value) Next Return s End Function End ModuleI can then use that method in the validating event as above...
Private Sub TextBox1_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating TextBox1.Text = TextBox1.Text.ReplaceAll(transpose) End SubNote you will also need
Imports WindowsApplication2.Extensionsin Form1.vb
Get the value from the text box as a string. You can then use any of the String Methods[^] to modify the content, and set it back into the textbox.
这篇关于如何查看文本框包含的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!