简体   繁体   中英

VB.net Custom Textbox

i am using a custom textbox control which doesn't have Text.Split() function in this box i enter a string in the follwing format : "35 To 99" , now here is my code i know its wrong, my programming skills are limited

    Dim v1 As Int32
    Dim v2 As Int32
    Dim rule As New String("{0} To {1}", v1, v2) = TextBox1.Text
    MsgBox(v1 & " " & v2)

in other words how would you get the nubers in this string "35 To 99" assign each one to a variable without Text.Split()

Split is a method of the String class, not the TextBox class. As such, it doesn't matter where you get the string from, whether it be from a text box, a custom control, a file, or anywhere else, you can use the String.Split method to split it. For instance:

Dim v1 As Int32
Dim v2 As Int32
Dim rule As String = TextBox1.Text
Dim parts() As String = rule.Split(New String() {" To "}, StringSplitOptions.None)
v1 = Integer.Parse(parts(0))
v2 = Integer.Parse(parts(1))
MessageBox.Show(v1 & " " & v2)

Or, more tersely:

' ...
Dim parts() As String = TextBox1.Text.Split(New String() {" To "}, StringSplitOptions.None)
' ...

To make the split case insensitive, just force the case one way or the other on the whole string before you split it, for instance:

Dim parts() As String = TextBox1.Text.ToLower().Split(New String() {" to "}, StringSplitOptions.None)

or

Dim parts() As String = TextBox1.Text.ToUpper().Split(New String() {" TO "}, StringSplitOptions.None)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM