字母表中的Visual Basic问题 [英] Visual Basic problems in Alphabets
问题描述
我有一个textbox2.text,我声明这是一个字符串,即myletters
我也有一个label36.text我也声明这个字符串即模式
我只在文本框中输入的是仅从a到z的字母,并且有一个标签36的原因是,当我再次放入相同的字母时,它将显示错误消息。
这是我到目前为止的代码。
----------------- -------------------------------------------------- ----------------------
Imports System.Text.RegularExpressions
I have a textbox2.text and I declare this as a string namely myletters
and I also have a label36.text and i also declare this as a string namely patterns
I will only input on the textbox is letters from "a" to "z" only and the reason why there is a label36 is that when i put the same letter again it will show an error message.
This is my code so far.
-----------------------------------------------------------------------------------------
Imports System.Text.RegularExpressions
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim patterns As String
patterns = Label36.Text
Dim myletters As String
myletters = TextBox2.Text
Dim matches As MatchCollection = Regex.Matches(myletters, patterns)
If Regex.IsMatch(patterns, myletters) Then
MsgBox("You already typed this letter.")
Else
Label36.Text = Label36.Text + myletters + ","
End If
End Sub
< br $>
--------------------------- -------------------------------------------------- -----------
到目前为止,我的代码在顶部有什么用处,如果我已经再次输入相同的字母,则显示错误如果我不放相同的字母,请给这个。
示例输出:
套装A:a,b,c,d,e
现在这里是我的问题。
1.如何输入我的第一个字母而不用逗号?
例如:
a
但我的输出是
示例:
a,
2.如何将文本框设置为仅1个字母的最大输入?
3.如何在正确的时间和格式下放置逗号?
示例1:
a
示例2 :
a,b
但我的例子是
示例1:
a,
示例2:
a, b,
4.如何在正确的位置安排字母?
示例1:
a,z
如果我输入x,输出必须是
例2:
a,x,z
谢谢,任何答案都将被接受TY。
SHOUTING已删除 - OriginalGriff [/ edit]
----------------------------------------------------------------------------------------
What use on the top is my code so far that will show error if i already put a same letter again and give this if i dont put the same letters.
Example Output:
Set A: a,b,c,d,e
Now here is my questions.
1.How can i input my first letter without following a comma?
Example:
a
But my output is
Example:
a,
2.How can i set my textbox to the maximum of input of just 1 letter?
3.How can i put commas in right time and format?
Example 1:
a
Example 2:
a,b
But my example is
Example 1:
a,
Example 2:
a,b,
4.How can i arrange the alphabet letters in the right positions?
Example 1:
a,z
If i will input "x" the output must be
Example 2:
a,x,z
Thank you, any answer will be accepted TY.
[edit]SHOUTING removed - OriginalGriff[/edit]
推荐答案
有很多方法可以做到这一点,最简单的可能就是根本不使用正则表达式 - 请记住
There are a number of ways to do this, and the simplest is probably not to use a regex at all - bear in mind that
a,b
在你的标签中作为正则表达式正是如此:a',后跟逗号,后跟a 'b' - 所以如果你的文字包含a,c则不匹配......
相反,考虑设置一个字符串列表,并添加你去的每一个新角色/>
然后,您可以检查列表是否已经使用List.Contains方法的字母,如果没有,则添加它。您可以使用List.Sort对列表进行排序,并使用string.Join为表示创建逗号分隔的字符串。
in your label as a regex looks for exactly that: an 'a', followed by a comma, followed by a 'b' - so if your text contains "a,c" it will not match...
Instead, think about setting up a List of strings, and add each new character to it as you go.
You can then check if the list has a letter already using the List.Contains method and add it if it doesn't. You can sort the list using List.Sort, and create a comma delimited string for presentation using string.Join.
这篇关于字母表中的Visual Basic问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!