2013-02-13 70 views
0

這似乎是一個非常簡單的問題,但對於我的生活(我對編碼相當陌生)我找不到答案。在Windows窗體上創建一個全局計數器來計數按鈕按

因此,我有一個列表框,命名爲listbox1,我通過按幾個按鈕中的一個按鈕(每個按鈕有一組「值」添加到列表中)1,但我希望每個元素該列表將被遞增。 E.G:

  1. 對象ž
  2. 對象f,
  3. 體W

等等等等等等。但到目前爲止,我所管理的所有內容都是指望每個單獨的按鈕,這意味着只有同一個按鈕的計數纔會增加,而不是全部。 EG:

  1. 堅定的長靴
  2. 怒焰靴
  3. 怒焰靴
  4. 堅定的長靴

圖片顯示我在列表框中得到: Picture of the programme

所以按右邊的按鈕將一個條目添加到listb中牛/

private: System::Void btn_steadfast_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     static int i = 1; 
     this->listBox1->Items->Add(i + ". Steadfast Boots "); 
     i++; 

private: System::Void btn_ragefire_Click(System::Object^ sender, System::EventArgs^ e) { 
     static int i = 0; 
     this->listBox1->Items->Add(i + ". Ragefire Boots "); 
     i++; 
    } 

我相信我需要一個全局計數器,每個按鈕指的是按下時,只是不知道該怎麼去做。

任何幫助將非常感激。

問候傑米


額外的資訊


這是我試過的代碼(註釋掉是我試圖把在,同時也刪除過時的信息,如使用「i」,並嘗試將「Form1」更改爲BDLGlacors以表示表格名稱,因爲這是程序中的第二種形式,因此無效):

#pragma endregion 
private: System::Void BDLGlacors_Load(System::Object^ sender, System::EventArgs^ e) { 
     } 
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { 

      MessageBox::Show("Return to Menu?"); 
      BDLGlacors::Close(); 
     } 
private: System::Void listView1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { 
     } 
/*public ref class Form1 : public System::Windows::Forms::Form{ 
private: 
    int buttonPressCount; 

public: 
    Form1() 
    { 
     buttonPressCount = 0; 

    }*/ 
private: System::Void btn_steadfast_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     static int i = 1; 
     this->listBox1->Items->Add(/*buttonPressCount*/ i + ". Steadfast Boots "); 
     //buttonPressCount++; 
     i++;    
    } 

private: System::Void btn_ragefire_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     static int i = 1; 
     this->listBox1->Items->Add(/*buttonPressCount*/i + ". Ragefire Boots "); 
     //buttonPressCount++; 
     i++; 
    } 

長時間編輯的道歉。

+0

這不是一個C++的問題。我重新認爲適合我 - 如果我犯了錯誤,請隨時更改。 – JBentley 2013-02-13 16:38:27

+0

謝謝你,不知道要放什麼標籤。 – user2069077 2013-02-13 18:16:33

回答

0

我不知道我的理解,但也許你想要的計數作爲字段:

public ref class Form1 : public System::Windows::Forms::Form 
{ 
private: 
    int buttonPressCount; 

public: 
    Form1() 
    { 
     buttonPressCount = 0; 
     // ... 
    } 

private: System::Void btn_steadfast_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     this->listBox1->Items->Add(buttonPressCount + ". Steadfast Boots "); 
     buttonPressCount++; 

private: System::Void btn_ragefire_Click(System::Object^ sender, System::EventArgs^ e) { 
     this->listBox1->Items->Add(buttonPressCount + ". Ragefire Boots "); 
     buttonPressCount++; 
    } 
}; 
+0

嗨,謝謝你的迴應。 但是,我似乎無法管理這適合我的代碼。我還假設通過「也許你希望計數作爲一個領域」你提出一個變量來存儲計數? 這似乎是你的目標。 – user2069077 2013-02-13 18:17:13

+0

在你的例子中,每個方法中的「i」變量是一個不同的變量(加上,如果有幾個類的實例,每個方法將訪問和更新與其他實例中相應方法相同的變量,因爲變量是靜態的)。在我的例子中,這兩個方法將訪問和更新相同的變量,因爲它是類中的一個實例字段。如果該類有多個實例,則該字段對於每個實例都是不同的。我再次不確定這是你想要的,但你對這個問題的描述對我來說似乎還不清楚。 – user1610015 2013-02-13 19:01:52

+0

已經更新了我的第一篇文章,更多的信息,正是我所嘗試的。 – user2069077 2013-02-13 20:01:04