简体   繁体   English

C#,在按下按钮时循环通过TextBlock,因此关闭

[英]C#, Cycle through TextBlock on button press, So close

So I am working in C#, bit of a beginner, I have searched around but cannot find anything. 所以我在C#中工作,只是一个初学者,我到处搜索但找不到任何东西。

I have a TextBlock on my screen and a Button, each time the button is pressed I want the new text to be the content of the TextBlock, this is what I have so far however it only shows the last line, thanks. 我在屏幕上有一个TextBlock,还有一个Button,每次按下按钮时,我都希望新文本成为TextBlock的内容,这是我到目前为止的内容,但是只显示最后一行,谢谢。

 string[] arr = new string[4]; 
        arr[0] = "Hello and Welcome";               
        arr[1] = "To the new app";             
        arr[2] = "enjoy your stay";             
        arr[3] = "press next to continue";          

        for (int i = 0; i < arr.Length; i++)
        {
            string s = arr[i];
            tbArray.Text = s;
        }

You are not storing each string, but only the last one. 您不会存储每个字符串,而只存储最后一个字符串。

Add a little + before your equals like so: 在等于之前添加一点+ ,如下所示:

string[] arr = new string[4]; 
arr[0] = "Hello and Welcome";               
arr[1] = "To the new app";             
arr[2] = "enjoy your stay";             
arr[3] = "press next to continue";          

for (int i = 0; i < arr.Length; i++)
{
    string s = arr[i];
    tbArray.Text += s;
}

It would be also nice to add a space or a new line as they will be squashed together this way. 添加空格或换行也很不错,因为这样可以将它们挤压在一起。

If I understand correctly, you would like to display the next element from your array every time your button is pressed? 如果我理解正确,那么您想在每次按下按钮时显示数组中的下一个元素吗? You should store the current index of your array in a variable in your class and each time your button is pressed increment its value. 您应该将数组的当前索引存储在类中的变量中,并且每次按下按钮时,其值都会增加。 Obviously, make sure you check that your index is not out of bounds. 显然,请确保检查索引没有超出范围。

public class Foo
{
    private int index = 0;

    private string[] arr = new [] { "Hello and Welcome",
                                    "To the new app",
                                    "enjoy your stay",
                                    "press next to continue" };     

    private void ButtonCallback(...)
    { 
         tbArray.Text = arr[index++]; 
    }
}

I immgine you always see "press next to continue" string. 我想,您总是会看到"press next to continue"字符串。 as this is the last string assigned to a text box. 因为这是分配给文本框的最后一个字符串。 You have to aggergate all strings into the single one and final string assign to a text box. 您必须将所有字符串加粗为单个字符串,最后一个字符串分配给文本框。

Simple example: 简单的例子:

StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < arr.Length; i++)
{
  sb.Append(arr[i]); 
}

tbArray.Text = sb.ToString();

if you want to display new Text from array each time you press a button 如果您想在每次按下按钮时显示数组中的新文本

Global "i" variable and your array "arr" 全局“ i”变量和数组“ arr”

/* global variable */
var i = 0;
/* your array - e.g. javascript array */
var arr = ['Hello and Welcome', 
            'To the new app', 
            'enjoy your stay', 
            'press next to continue'];

Call this function 调用此功能

 function displayText() {
        if(i >= arr.length) {
            i=0;
        }
        yourTextBlock.Text = arr[i];
        i++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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