将第n个字符从textbox1复制到textbox2; n是按钮的点击次数

时间:2014-07-21 08:53:38

标签: vb.net

我正在使用vb.net开发游戏,因为我需要实现以下算法。    算法:我有两个文本框,text1和text2以及按钮。 text2 为空,text1有一些静态值    当我第一次点击按钮时,第一个字母被复制到text2    当我再次点击时,前两个字母被复制到text2,依此类推。    点击次数将从一个字符复制到text2。

即,

Text1.Text = "sample text"
Text2.text = ""
dim cnt as integer=0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  cnt += 1
  ' here how can i take single characters from text1? and place it in text2 based on cnt
End sub

3 个答案:

答案 0 :(得分:0)

您需要添加一个隐藏字段来计算您的点击次数。

  Text1.Text = "sample text";
    Char[] listLetters = Text1.Text.ToCharArray();
    for (int i = 0; i < hiddenCountClic; i++)
    {
        Text2.text += listLetters[i].ToString();
    }

答案 1 :(得分:0)

试试这个:

Dim cnt as integer=0
Dim text1 As String =Text1.text

If text.Length > cnt Then
    Text2.text = (Text1.Text).Substring(0, cnt)
    cnt++
End If

宣布&#34; cnt&#34;作为静态或全局变量,以便保留该值。

答案 2 :(得分:0)

 dim cliks as Integer  = 0  ' initialize an integer variable
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
 clicks += 1 ' in each click increment the value of the variable  by 1(click count)
 TextBox1.Text = TextBox1.Text.Substring(0, clicks)' take sub string of the text1 from 0 to click count 
 End Sub