我已经创建了一个文本框1和2来创建一行x,从一个x开始,然后输入然后输入xx,依此类推......
现在我需要文本框3和4来显示相同的内容,但它必须从10 x开始。 这就是我所拥有的:
namespace Vierkant
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click_1(object sender, RoutedEventArgs e)
{
string x = "X";
for (int i = 0; i < 10; i++)
{
if ( i == 0)
{
x = "X";
}
else
{
x += "X";
}
txt_box1.Text += (x) + "\n";
txt_box2.Text += (x) + "\n";
}
for (int j = 10; j > 0; j--)
{
if (j == 10)
{
x = x.Remove(x.Length - 1);
}
else
{
x = x.Remove(x.Length - 1);
}
txt_box3.Text += (x) + "\n";
txt_box4.Text += (x) + "\n";
// txt_box4.Text displays correct but starts from 9 x's?
}
}
}
}
答案 0 :(得分:1)
您正在删除其中一个x,然后再显示它。
if (j == 10)
{
x = x.Remove(x.Length - 1);
}
也许更好的解决方案是:
if (j < 10)
{
x = x.Remove(x.Length - 1);
}
答案 1 :(得分:0)
试试这个:
private void button_Click_1(object sender, RoutedEventArgs e)
{
int i;
string allLines = "";
for (i = 1; i <= 10; i++)
allLines += new string('X', i) + (i < 10 ? "\n" : "");
txt_box1.Text = allLines;
txt_box2.Text = allLines;
allLines = "";
while (--i > 0)
allLines += new string('X', i) + (i > 1 ? "\n" : "");
txt_box3.Text = allLines;
txt_box4.Text = allLines;
}
这种方式更清晰,更有效,你在内存中创建了很多的字符串。