我根据这个代码添加字符串

时间:2017-08-25 21:40:05

标签: vb6

text1.text =“梅威瑟vs麦格雷戈:洛杉矶新闻发布会” text2.text返回空格数。

我的自定义值= =“爱”

现在点击每个按钮,我想将该自定义字符串随机添加到空间的每个位置。

所以先点击 text1.text =“梅威瑟vs爱麦格雷戈:洛杉矶新闻发布会”

第二次点击 text1.text =“Mayweather vs McGregor :;爱洛杉矶新闻发布会”

依赖于代码,它会检测到空间,然后每次点击只添加一次。

Code:

Dim Count As Integer
    Dim i As Integer
    For i = 1 To Len(Text1.Text)
        If Mid(Text1.Text, i, 1) = " " Then Count = Count + 1
        Text2.Text = Count
    Next

1 个答案:

答案 0 :(得分:0)

这是一个小型测试项目:

Option Explicit
' 1 form with:
'    2 textbox controls: name=Text1 and name=Text2
'    1 command button  : name=Command1

Private mstrText As String

Private Sub Command1_Click()
  Static intCount As Integer 'declare as static to remember value of intCount on next click
  Dim intLoop As Integer
  Dim intSpace As Integer
  intCount = intCount + 1
  'find correct space
  intLoop = 0
  intSpace = 0
  Do While intLoop < intCount
    intSpace = InStr(intSpace + 1, mstrText, " ")
    intLoop = intLoop + 1
  Loop
  Text1.Text = Left$(mstrText, intSpace) & "love " & Mid$(mstrText, intSpace + 1)
  Caption = CStr(intSpace)
End Sub

Private Sub Form_Load()
  mstrText = "Mayweather vs McGregor: Los Angeles Press Conference"
  Text1.Text = mstrText
End Sub

Private Sub Text1_Change()
  'show number of spaces
  Dim intSpace As Integer
  intSpace = Len(Text1.Text) - Len(Replace(Text1.Text, " ", ""))
  Text2.Text = CStr(intSpace)
End Sub

这是你的意思吗?