Resolved The buttons don't work on my second form

Joined
Aug 10, 2020
Messages
19
Programming Experience
1-3
Hello. I built an app in which buttons on a form cause a second form to be programmatically created. The buttons on the first form work fine, and create the second form, but the buttons on the second form seem to be disabled. I copy/pasted one of the functioning buttons onto the second form, and it doesn't work. Thanks in advance for any help! George.
 
Is this WinForms, WPF, WebForms, ASP.NET MVC, or something else? We can't tell since you posted to the VS.NET General Discussion forum.

Also it would help us dramatically if you show us your code. Please post your code in code tags. Do not use screenshots.
 
Last edited:
Is this WinForms, WPF, WebForms, ASP.NET MVC, or something else? We can't tell since you posted to the VS.NET General Discussion forum.

Also it would help us dramatically if you show us your code. Please post your code in code tags. Do not use screenshots.
It's a Windows Forms App (NET Framework)

Form1.Designer.cs
DungeonCreation openedForm = null;

Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DM
{
    public partial class MainScreen : Form
    {
        public MainScreen()
        {
            InitializeComponent();
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void DCButton_Click(object sender, EventArgs e)
        {
            if (openedForm == null)
            {
                //There is no Form, so create and open it
                openedForm = new DungeonCreation();
                openedForm.Show();
            }
            else
            {
                //there is a form. So close and get rid of the reference
                openedForm.Close();
                openedForm = null;
            }
        }

        private void PlayButton_Click(object sender, EventArgs e)
        {

        }
    }
}

Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DM
{
    public partial class DungeonCreation : Form
    {
        public DungeonCreation()
        {
            InitializeComponent();
        }

        RandomCreation openedForm2 = null;

        private void GenerateDungeonButton_Click(object sender, EventArgs e)
        {
            GenerateDungeonButton.Text = "hi";
            if (openedForm2 == null)
            {
                //There is no Form, so create and open it
                openedForm2 = new RandomCreation();
                openedForm2.Show();
            }
            else
            {
                //there is a form. So close and get rid of the reference
                openedForm2.Close();
                openedForm2 = null;
            }
        }

        private void GenerateNPCButton_Click(object sender, EventArgs e)
        {

        }

        private void GenerateWeaponButton_Click(object sender, EventArgs e)
        {

        }

        private void GenerateArmourButton_Click(object sender, EventArgs e)
        {

        }

        private void GenerateMagicButton_Click(object sender, EventArgs e)
        {

        }
    }
}
 
Last edited by a moderator:
Firstly, please don't post unformatted code snippets. They are just too hard to read.

Secondly, don't just post all your code. Post only the code that is relevant. Sometimes it can be hard to work out exactly what is and isn't relevant but it's pretty much a guarantee that namespace imports and empty event handlers are not.
 
As for your question, do they actually seem to be disabled or are they actually enabled but no code is executed when you click them? Have you actually attached event handlers to them? If you just copy event handler code from one form to another then the answer is "no". You have to actually register the method as an event handler. You can double-click a control to create a handler for the default event, which is Click for a Button. If you want finer-grained control of event handlers, open the Properties window and click the Events button on the toolbar. You can then double-click any event to create a handler or you can select an existing method from the drop-down.
 
Last edited:
I'll make the obligatory comment that C# is not like VB where event handlers are autowired by naming convention. In C#, you have to actually register for events. The compiler won't insert code to do the event registration when when it sees something that looks like an event handler.
 
I'll make the obligatory comment that C# is not like VB where event handlers are autowired by naming convention. In C#, you have to actually register for events. The compiler won't insert code to do the event registration when when it sees something that looks like an event handler.
That was a really long time ago before VB moved to .Net - or did you mean VBA?
 
Pre VB.NET as I recall.
 
I can't find "events" on any of the properties. I double-clicked on the second set of buttons to create event handlers individually--I didn't just copy/past the ehs. The second set seems like they're disabled. They don't highlight when I hover over them like the functional buttons do.
 
Events is not a property, it is one of the views of Properties window: Properties Window - Visual Studio
1597347780647.png
 
Is that the one that has "build events"?

I don't a properties button like that.

Wait I found it.

It shows the clickevent in the click properties of each of these non-functioning buttons.
 
And what are method names listed there? How do they compare to the method names you have in post #3?
 
Seems to work fine for me...
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;

namespace WinForms
{
    class ChildForm : Form
    {
        public ChildForm()
        {
            var showMessage = new Button()
            {
                Text = "Show Message",
                AutoSize = true,
            };
            showMessage.Click += (o, e) => MessageBox.Show("Hi!");
            Controls.Add(showMessage);
        }
    }

    class MainForm : Form
    {
        MainForm()
        {
            ChildForm childForm = null;

            var showChild = new Button()
            {
                Text = "Show Child",
                AutoSize = true,
            };
            showChild.Click += (o, e) =>
            {
                if (childForm == null)
                {
                    showChild.Text = "Hide Child";
                    childForm = new ChildForm();
                    childForm.Show();
                }
                else
                {
                    showChild.Text = "Show Child";
                    childForm.Close();
                    childForm = null;
                }
            };
            Controls.Add(showChild);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}
 
The second set seems like they're disabled. They don't highlight when I hover over them like the functional buttons do.
What are their Enabled properties set to? Are they on the form directly or on a Panel or the like? What is the Enabled property of that parent set to? Check at design time and at run time.
 
Back
Top Bottom