有什么方法可以按字符显示字符串字符?就像在旧RPG中一样
我试过这个:
string text1 ="this is a text";
for (int i = 0; i < text1.Length; i++)
{
textBox1.Text = "" + text1[i];
}
但它只替换文本框中的最后一个字符。
答案 0 :(得分:1)
试试这个:
public partial class Form1 : Form
{
int count = 0;
string text1 = "this is a scrolling text";
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
textBox1.ReadOnly = true;
SetTimer(500);
}
private void SetTimer(int milliseconds)
{
timer1.Tick+=new EventHandler(timer1_Tick);
timer1.Interval = milliseconds;
timer1.Start();
}
private void timer1_Tick(Object o, EventArgs e)
{
if (count < text1.Length)
{
textBox1.Text += text1[count];
count++;
}
else
{
timer1.Stop();
button1.Enabled = true;
textBox1.ReadOnly = false;
}
}
}
输出:
答案 1 :(得分:0)
我不确定这对你有什么帮助。您可能想要添加为什么这样做。您对代码的问题是您没有附加到字符串。请尝试使用此行:
textBox1.Text += text1[i];
您需要在开始之前清除textBox1.Text(将其设置为“”)。
更好的方法可能是打印出十六进制值,以便很容易看到非打印字符。你可以这样做:
string text1 ="this is a text";
var sb = new StringBuilder();
for (int i = 0; i < text1.Length; i++) {
sb.AppendFormat("{0:X} ", text1[i]);
}
textBox1.Text = sb.ToString();
阅读Jeroen Vannevel让我觉得你正试图创造一种“打字机”效果。在这种情况下尝试类似:
string text1 ="this is a text";
textBox1.Text = "";
for (int i = 0; i < text1.Length; i++) {
textBox1.Text += text1[i];
Thread.Sleep(250); // 1/4 sec delay
}
此代码是根据给定的代码编写的。有一个隐含的期望,这是从主UI线程运行。这意味着当您坐在具有睡眠延迟的循环中时,您的UI将无响应(因为您正在占用主线程)。您可以通过在Dispatcher对象(应用程序)上使用Invoke方法并在不同的线程上运行代码来解决此问题。在这种情况下,您应该只调用主UI(textBox1)拥有的部分。
答案 2 :(得分:0)
以下代码应该这样做:
public Form1()
{
InitializeComponent();
TypeText("this is a text");
}
private void TypeText(string text)
{
textBox1.Clear(); // Make sure the textbox is empty
Thread thread = new Thread(delegate() // Create a new thread which fills the textbox periodically
{
button1.BeginInvoke((MethodInvoker)delegate { button1.Enabled = false; }); // Disables the button
for (int i = 0; i < text.Length; i++)
{
int temp = i; // Cache variable because without this, an 'ArgumentOutOfRange' Exception will be thrown
textBox1.BeginInvoke((MethodInvoker)delegate // Invoke to main thread
{
textBox1.Text += text[temp]; // Fill with next char
});
if (text[temp] != ' ') // This makes sure the user doesn't have to wait the double of the time when there is an empty space for the new character
Thread.Sleep(500); // This will stop the seperate thread for 500ms. Won't block the main thread
}
button1.BeginInvoke((MethodInvoker)delegate { button1.Enabled = true; }); // Reenables the button
});
thread.Start(); // Start the new thread and continue the main thread
}
答案 3 :(得分:-1)
textBox1.Text = textBox1.Text + text1[i]
答案 4 :(得分:-1)
对我而言,似乎你想要将字符串分解为其字符,有几种方法可以做到这一点:
1 - String.GetChar()方法: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.getchar%28v=vs.110%29.aspx
2 - String.Chars属性: http://msdn.microsoft.com/en-us/library/system.string.chars%28v=vs.110%29.aspx
TextBox1.Text += MyString.Chars[i];
然后,如果你想要一个接一个地延迟或类似地显示它们,你可以像其他人建议的那样使用计时器。