locked
The buttons don't work on my second form RRS feed

  • Question

  • 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 for your help! George.
    Monday, August 10, 2020 9:07 PM

Answers

  • I finally got it to work by enabling the parent form. Thanks!
    Sunday, August 16, 2020 10:37 PM

All replies

  • Hi George, 
    >> I copy/pasted one of the functioning buttons onto the second form.
    What specific operation is this? And have you subscribed to the event?
    You can subscribe to a button's click event in order to make your application do something useful when you clicks the button.
    Here is a code example you can refer to.

    private void button1_Click(object sender, EventArgs e)
    {
        using (Form form = new Form())
        {
            form.Text = "Second Form";
    
            Button button = new Button();
            form.Controls.Add(button);
            button.Text = "button2";
            button.Click +=new EventHandler( myButton_Click);
            form.ShowDialog();
        }
            
    }
    void myButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show("hello");
              
    }

    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Tuesday, August 11, 2020 2:10 AM
  • When I made the buttons I copied one, then clicked paste in a new spot on the form to create a new button.
    Tuesday, August 11, 2020 5:38 AM
  • Hi  George, 
    I viewed that you create a second form programmatically, so did you copy the button to the first form or second form?
    And the button can't be copied and pasted in the second form.
    So could you explain in detail? If pssible, Please provide some code or screenshots to reproduce the situation.
    I suggest you add button control in the second form by using Control.add method. And you can also use button's location property to set the position of button.
    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.


    Tuesday, August 11, 2020 9:54 AM
  • I copied it into the second form. I made new buttons and event handlers in the second form the same way I did in the first one, but they're not enabled--they don't highlight when I hover.

    Form1.Designer.cs

    DungeonCreation openedForm = null;

    Form1.cs

             

    {
            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.cs

      {
            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)
            {

            }
        }

    Thursday, August 13, 2020 8:04 PM
  • Hi George O'Gorman,
    First, you should check whether the enable attribute of the control is true.
    You can also set it by code:

    button.Enabled = true;

    If you copy the button from the form1 to form2, the button's text should be same. 
    Based on your code, I viewed there are no button with same text. So if you change the button's text in the form2? Such as "DCButton" and "GenerateDungeonButton"
    And what is "RandomCreation" in you code?
    To avoid mistakes, I suggest you drag the button form Toolbox to form2 and use it directly.
    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Friday, August 14, 2020 9:00 AM
  • I finally got it to work by enabling the parent form. Thanks!
    Sunday, August 16, 2020 10:37 PM
  • Hi George O'Gorman,
    I am glad you have got your solution. We appreciated you shared us your solution. And we also hope you can mark it as an answer. By marking a post as Answered, you help others find the answer faster.
    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Monday, August 17, 2020 1:09 AM