C# A few questions.

zokudu

Diamond Member
Nov 11, 2009
4,364
1
81
I am doing a project for a C# class I am in and I am having a few issues getting dynamically created buttons to execute a click on them. It is supposed to be a Skyrim perk calculator just as an FYI.

As it stands now this is one of my classes for Archery: http://pastebin.com/Cqifycq7

I create a button for each perk in the tree place them and draw lines connecting them.

Now taking the code for the Overdraw perk.
Code:
            Button btnOverdraw = new Button();
            btnOverdraw.Location = new Point(582, 442);
            btnOverdraw.Text = "Overdraw " + perks[1, 1] + "/5";
            btnOverdraw.Size = new System.Drawing.Size(124, 23);
            this.Controls.Add(btnOverdraw);

Now I add in
Code:
submitButton.Click += new EventHandler(submitButton_Click);
and it says:

Code:
            Button btnOverdraw = new Button();
            btnOverdraw.Location = new Point(582, 442);
            btnOverdraw.Text = "Overdraw " + perks[1, 1] + "/5";
            btnOverdraw.Size = new System.Drawing.Size(124, 23);
            this.Controls.Add(btnOverdraw);
            btnOverdraw.Click += new EventHandler(btnOverdraw_Click);

then I add in the actual event for when the button is clicked:
Code:
private void btnOverdraw_Click(object sender, EventArgs e)
{
    if (perks[1, 1] < 5)
    {
        perks[1, 1]++;
    }
    //blah blah blah
}

But the code inside the overdraw event never fires when i click the button. I cannot figure out what i am doing wrong here. Any help?
 

LokutusofBorg

Golden Member
Mar 20, 2001
1,065
0
76
We would need to see a more complete code sample, as the way you've described you're doing it should work fine.
 

zokudu

Diamond Member
Nov 11, 2009
4,364
1
81
Hmm sorry I think this is entirely my fault. The Dynamic click event is working fine. I seem to be misusing the array.

I have the array set as:
Code:
int[,] perks = new int[18, 15];

With the idea that there are 18 skills and the max number of perks per skill is 15. I cannot seem to figure out how to adjust the integer at [1,1] to show the actual number of points you have in that perk. At least saying:

Code:
perk[1, 1]++;
Does not seem to work and neither does:
Code:
            if (perks[1, 1] == 0)
            {
                perks[1, 1] = 1;
                return;
            }
            else if (perks[1, 1] == 1)
            {
                perks1, 1] = 2;
                return;
            }
            else if (perks[1, 1] == 2)
            {
                perks[1, 1] = 3;
                return;
            }
            else if (perks[1, 1] == 3)
            {
                perks[1, 1] = 4;
                return;
            }
            else if (perks[1, 1] == 4)
            {
                perks[1, 1] = 5;
                return;
            }
            else if (perks[1, 1] == 5)
                return;
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
Are you updating btnOverdraw.Text? It's not magically linked to your array.
 

zokudu

Diamond Member
Nov 11, 2009
4,364
1
81
Are you updating btnOverdraw.Text? It's not magically linked to your array.

I hadn't gotten that far. I was attempting to refresh the button text by clicking on Archery again to no avail. I am getting an error when trying to rerun the button text within the btnOverdraw_Click event.

Code:
        private void btnOverdraw_Click(object sender, EventArgs e)
        {
            if (perks[0, 0] < 5)
            {
                perks[0, 0]++;
                btnOverdraw.Text = "Overdraw " + perks[0, 0] + "/5";
            }
            else
                return;
        }

Code:
The name 'btnOverdraw' does not exist in the current context
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
All your btn* variables should be class scope, not function scope, that will fix that.

And try putting this.Controls.Clear() at the top of your archery function.
 

zokudu

Diamond Member
Nov 11, 2009
4,364
1
81
All your btn* variables should be class scope, not function scope, that will fix that.

And try putting this.Controls.Clear() at the top of your archery function.

That did it! Perfect. It was working properly I just hadn't cleared the old buttons because I was only working on this one skill so far so I didn't think about clearing it. I added all of the buttons into a panel as well so i can clear the panel controls without clearing the skill selection controls.

Also I'm sorry to sound stupid but when you say my btn variables need to be class scope how do I fix that?
Figured it out. Thanks a ton for the help. I'll post again in here if something else comes up.
 
Last edited: