I have a string[] arr. I want to make it like if I click the button once arr[0] goes to the textbox and If I click button again arr[1] goes to textbox.
我有一个字符串[] arr。我希望如果我按下按钮一次arr [0]进入文本框,如果我再次点击按钮,arr [1]进入文本框。
I know it will be done using some counter and incrementing it on each click but im just confused with syntax. Help will be appreciated.
我知道它将使用一些计数器并在每次点击时递增它,但我只是混淆了语法。帮助将不胜感激。
3 个解决方案
#1
Here is a sample :
这是一个示例:
int _Counter = 0;
string[] arr = new string[4] {"1", "2", "3", "4"};
private void buttonClick(Object sender, EventArgs e)
{
textbox.Text = arr[_Counter];
_Counter++;
if (_Counter == arr.Length) {_Counter = 0;}
}
If you're using this in ASP.NET application you should store _Counter in ViewState, Session or Cookie.
如果您在ASP.NET应用程序中使用它,则应将_Counter存储在ViewState,Session或Cookie中。
#2
if(Session["Counter"] == null)
Session["Counter"] = 0;
string[] arr = new string[4] {"A", "B", "C", "D"};
private void button1_Click(Object sender, EventArgs e)
{
textbox1.Text = arr[int.parse(Session["Counter"])];
Session["Counter"]=int.parse(Session["Counter"])+1;
if (int.parse(Session["Counter"]) == arr.Length)
{
Session["Counter"]= 0;
}
}
#3
There's a one-liner for everything. ;)
一切都是单行的。 ;)
int _counter = 0;
string[] _values = {"1", "2", "3", "4"};
private void buttonClick(Object sender, EventArgs e)> {
TheLittleTextbox.Text = _values[_counter++ % _values.Length];
}
(Note: As the counter is not reset, it will overflow after 2 billion clicks... However, as you would have worn out a big pile of mice (clicking frantically day and night for about 20 years) to get there, I consider it safe enough...)
(注意:由于计数器没有重置,它会在20亿次点击后溢出......但是,因为你会磨掉一大堆老鼠(昼夜疯狂点击大约20年)到达那里,我认为它够安全......)
#1
Here is a sample :
这是一个示例:
int _Counter = 0;
string[] arr = new string[4] {"1", "2", "3", "4"};
private void buttonClick(Object sender, EventArgs e)
{
textbox.Text = arr[_Counter];
_Counter++;
if (_Counter == arr.Length) {_Counter = 0;}
}
If you're using this in ASP.NET application you should store _Counter in ViewState, Session or Cookie.
如果您在ASP.NET应用程序中使用它,则应将_Counter存储在ViewState,Session或Cookie中。
#2
if(Session["Counter"] == null)
Session["Counter"] = 0;
string[] arr = new string[4] {"A", "B", "C", "D"};
private void button1_Click(Object sender, EventArgs e)
{
textbox1.Text = arr[int.parse(Session["Counter"])];
Session["Counter"]=int.parse(Session["Counter"])+1;
if (int.parse(Session["Counter"]) == arr.Length)
{
Session["Counter"]= 0;
}
}
#3
There's a one-liner for everything. ;)
一切都是单行的。 ;)
int _counter = 0;
string[] _values = {"1", "2", "3", "4"};
private void buttonClick(Object sender, EventArgs e)> {
TheLittleTextbox.Text = _values[_counter++ % _values.Length];
}
(Note: As the counter is not reset, it will overflow after 2 billion clicks... However, as you would have worn out a big pile of mice (clicking frantically day and night for about 20 years) to get there, I consider it safe enough...)
(注意:由于计数器没有重置,它会在20亿次点击后溢出......但是,因为你会磨掉一大堆老鼠(昼夜疯狂点击大约20年)到达那里,我认为它够安全......)