Click here to Skip to main content
14,661,175 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 1

Put something like
textBox1.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 the TextChanged 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 project
Imports 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 Module
I 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 Sub
Note you will also need
Imports WindowsApplication2.Extensions
in Form1.vb
   
v2
Comments
YouCanHateMeNow 26-Feb-15 9:33am
   
First of all, thank you:)

But it is possible, that you cant Replace nothing with something? (I know it sounds a little strange)

but if the user types nothing in a textbox, then the programm shoul
CHill60 26-Feb-15 9:50am
   
Try something like
If TextBox1.Text.Trim() = String.Empty Then
TextBox1.Text = "You have to enter something!"
e.Cancel = True
End If
in the validating event before trying the transpose
YouCanHateMeNow 26-Feb-15 10:06am
   
Thank you
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100