单击按钮后如何在for循环中打印序列号 [英] How do I print serial numbers in for loop after button click

查看:107
本文介绍了单击按钮后如何在for循环中打印序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我有一个按钮,在Messagebox中显示i的值。我希望第一次显示值为1,然后退出循环,

然后再次点击按钮它应显示为2

并退出循环

然后再次点击按钮它应该显示为3

并退出循环

然后再次点击按钮它应显示为4

并退出循环

 私人  void  Button_Click( object  sender,RoutedEventArgs e)
{
c = 1 ;
for (i = 1 ; i < ; = 26 ; i ++)
{
// < span class =code-comment> b = i;
c = i;
MessageBox.Show( i的值是 + i);
}



如何更改此代码才能获得...

解决方案

< blockquote>你不需要循环,只需将变量移到方法之外:

  private  < span class =code-keyword> int  counter =  1 ; 
private void Button_Click( object sender,RoutedEventArgs e)
{
MessageBox.Show( counter的值是 + counter ++);
}


hi .
i have a button which displays the value of "i" in Messagebox. i want to display the value as 1 for 1sttime, and exit the loop,
then for button click again it should display as 2
and exit the loop
then for button click again it should display as 3
and exit the loop
then for button click again it should display as 4
and exit the loop

private void Button_Click(object sender, RoutedEventArgs e)
       {
           c = 1;
           for (i = 1; i <= 26; i++)
           {
               //  b = i;
               c = i;
               MessageBox.Show(" value of i is " + i);
           }


how should i change this code to get that..

解决方案

You don't want a loop, just move the variable outside the method:

private int counter = 1;
private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("The value of counter is " + counter++);
}


这篇关于单击按钮后如何在for循环中打印序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆