2009-08-30 6 views
-2

증가 및 감소 카운터를 만들고 싶습니다. X와 Y라는 두 개의 버튼이 있습니다. 먼저 X를 누른 다음 Y 키를 누르면 증가합니다. 먼저 Y를 누른 다음 X 카운터를 누르면 감소합니다.은 C# 카운터를 구현해야합니다.

저는 C#에 익숙하지 않습니다. 그래서 아무도 나를 도와주세요 ?? 다음 :(

당신은 카운터를 추적 할 변수를 갖고 싶어 것
+4

이 숙제인가? 그렇지 않다면 "증가"버튼과 "감소"버튼을 사용하는 것이 훨씬 간단하지 않습니까? 그렇다면 지금까지 무엇을 시도 했습니까? – VoteyDisciple

+1

그리고 그것은 ASP.NET, WPF 또는 WinForms 버튼입니까? –

+1

웹 또는 데스크톱 응용 프로그램을 원하십니까? 전체 코드를 찾고 있습니까, 아니면 다른 리소스 (응용 프로그램을 만드는 방법, 논리를 수행하는 방법, 결과 표시 방법)에 대한 포인터를 원하십니까? 우리가 유추 할 수 있도록 알고있는 프로그래밍 언어가 있습니까? – olle

답변

1

.

int counter = 0; 

은 웹 응용 프로그램은 당신이 어디에 같은 세션 상태로 어떤이를 저장해야하는 경우. 귀하의 증가 카운터 단추 :

counter++; 

와 감소 카운터 버튼의

이 작업을 수행 :

counter--; 
3

2 개의 변수가 필요한 것처럼 들립니다. 카운터와 마지막으로 누른 버튼입니다. WinForms 응용 프로그램이라고 가정합니다. 필자가이 글을 쓰고있을 때 지정하지 않았기 때문입니다.

class MyForm : Form 
{ 
    // From the designer code: 
    Button btnX; 
    Button btnY; 

    void InitializeComponent() 
    { 
     ... 
     btnX.Clicked += btnX_Clicked; 
     btnY.Clicked += btnY_Clicked; 
     ... 
    } 

    Button btnLastPressed = null; 
    int counter = 0; 

    void btnX_Clicked(object source, EventArgs e) 
    { 
     if (btnLastPressed == btnY) 
     { 
      // button Y was pressed first, so decrement the counter 
      --counter; 
      // reset the state for the next button press 
      btnLastPressed = null; 
     } 
     else 
     { 
      btnLastPressed = btnX; 
     } 
    } 

    void btnY_Clicked(object source, EventArgs e) 
    { 
     if (btnLastPressed == btnX) 
     { 
      // button X was pressed first, so increment the counter 
      ++counter; 
      // reset the state for the next button press 
      btnLastPressed = null; 
     } 
     else 
     { 
      btnLastPressed = btnY; 
     } 
    } 
} 
0

은 다른 웹 사이트에이 발견 :

public partial class Form1 : Form 
{ 
     //create a global private integer 
     private int number; 

     public Form1() 
     { 
      InitializeComponent(); 
      //Intialize the variable to 0 
      number = 0; 
      //Probably a good idea to intialize the label to 0 as well 
      numberLabel.Text = number.ToString(); 
     } 
     private void Xbutton_Click(object sender, EventArgs e) 
     { 
      //On a X Button click increment the number 
      number++; 
      //Update the label. Convert the number to a string 
      numberLabel.Text = number.ToString(); 
     } 
     private void Ybutton_Click(object sender, EventArgs e) 
     { 
      //If number is less than or equal to 0 pop up a message box 
      if (number <= 0) 
      { 
       MessageBox.Show("Cannot decrement anymore. Value will be 
               negative"); 
      } 
      else 
      { 
       //decrement the number 
       number--; 
       numberLabel.Text = number.ToString(); 
      } 
     } 
    } 
관련 문제