简体   繁体   中英

Enable button from a parent form

I have a C# Win Form.

Main Menu Form have

  1. Button A
  2. Button B
  3. Button C
  4. Button Setup

Each Button will open a new win form. After clicking Setup Button, It will open a Setup form. This form will use a datagridview to list out all the button in Main Menu except Setup button.

Admin can click the check box to select which button to enable in the Main Menu

Anyone know how to achieve this implementation

part of my code in Setup form

foreach (Button button in ????.Controls.OfType<Button>())
        {
            if (!button.Text.Contains("Setup"))
            {
                int index = dgvCheckbox.Rows.Add();
                dgvCheckbox.Rows[index].Cells["Selected"].Value = 0;
                dgvCheckbox.Rows[index].Cells["Button"].Value = button.Text;
            }
        }

if I use

MainMenu mainMenu = new MainMenu
foreach (Button button in mainMenu.Controls.OfType<Button>())

will have problem because Main Menu is already open

I create CheckBox (instead of menu) before show form so you can fill menu in the same way (this code is on main form):

    private void buttonSetup_Click(object sender, EventArgs e) {
        using (var adminForm = new AdminForm()) {
            foreach (var button in Controls.OfType<Button>().Where(bt => !bt.Text.Contains("Setup"))) {
                adminForm.Controls.Add(new CheckBox {
                    Text = button.Text,
                    Location = button.Location
                });
            }
            adminForm.ShowDialog();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM