• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C# A few questions.

zokudu

Diamond Member
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?
 
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;
 
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
 
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.
 
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:
Back
Top