在Windows窗体上创建一个全局计数器来计数按钮按
这似乎是一个非常简单的问题,但对于我的生活(我对编码相当陌生)我找不到答案。在Windows窗体上创建一个全局计数器来计数按钮按
因此,我有一个列表框,命名为listbox1,我通过按几个按钮中的一个按钮(每个按钮有一组“值”添加到列表中)1,但我希望每个元素该列表将被递增。 E.G:
- 对象ž
- 对象f,
- 体W
等等等等等等。但到目前为止,我所管理的所有内容都是指望每个单独的按钮,这意味着只有同一个按钮的计数才会增加,而不是全部。 EG:
- 坚定的长靴
- 怒焰靴
- 怒焰靴
- 坚定的长靴
图片显示我在列表框中得到: 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++;
}
长时间编辑的道歉。
我不知道我的理解,但也许你想要的计数作为字段:
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++;
}
};
嗨,谢谢你的回应。 但是,我似乎无法管理这适合我的代码。我还假设通过“也许你希望计数作为一个领域”你提出一个变量来存储计数? 这似乎是你的目标。 – user2069077 2013-02-13 18:17:13
在你的例子中,每个方法中的“i”变量是一个不同的变量(加上,如果有几个类的实例,每个方法将访问和更新与其他实例中相应方法相同的变量,因为变量是静态的)。在我的例子中,这两个方法将访问和更新相同的变量,因为它是类中的一个实例字段。如果该类有多个实例,则该字段对于每个实例都是不同的。我再次不确定这是你想要的,但你对这个问题的描述对我来说似乎还不清楚。 – user1610015 2013-02-13 19:01:52
已经更新了我的第一篇文章,更多的信息,正是我所尝试的。 – user2069077 2013-02-13 20:01:04
这不是一个C++的问题。我重新认为适合我 - 如果我犯了错误,请随时更改。 – JBentley 2013-02-13 16:38:27
谢谢你,不知道要放什么标签。 – user2069077 2013-02-13 18:16:33