我有3个按钮。有prev,next和add。我有一个包含6行的文本文件。因此,作为表单加载,它仅显示3行升序文本,当我单击下一个按钮时,将显示其他3行。但我不知道如何让它出现。这是我的下一个按钮代码
private void next_Click(object sender, EventArgs e)
{
string[] baca;
baca = System.IO.File.ReadAllLines(@path.Text);
nama.Text = baca[3];
npm.Text = baca[4];
alamat.Text = baca[5];
}
我想让它显示另一个下一行只有1个下一个按钮。
您需要将当前行存储在变量中,以便在单击按钮时可以使用此变量作为参考。
另一件事,而不是设置固定索引设置var作为下面的代码。 Ps:如果您使用web plataform存储隐藏字段中的当前项目。
private int i = 0; // or in hidden field
private void next_Click(object sender, EventArgs e)
{
string[] baca;
baca = System.IO.File.ReadAllLines(@path.Text);
nama.Text = baca[i];
npm.Text = baca[i+1];
alamat.Text = baca[i+2];
}
@Plutonix已经说明了答案,我引用了“读取数据一次并使用表单级别var来索引要显示的内容”。
int firstIndex = 0;
var baca = System.IO.File.ReadAllLines(@path.Text).ToList();
private void next_Click(object sender, EventArgs e)
{
firstIndex++;
nama.Text = baca[firstIndex];
npm.Text = baca[firstIndex + 1];
alamat.Text = baca[firstIndex + 2];
}
但是,如果由于某种原因您不想要表单级别变量,这将起作用:
private void next_Click(object sender, EventArgs e)
{
var baca = System.IO.File.ReadAllLines(@path.Text).ToList();
int firstIndex = 1 + baca.FindIndex(nama.Text);
nama.Text = baca[firstIndex];
npm.Text = baca[firstIndex + 1];
alamat.Text = baca[firstIndex + 2];
}
请记住,这不是最好的方法,如果有重复,则不起作用。