简体   繁体   中英

C# / Visual Studio 2017: References to functions got lost after updating / reorganizing the GUI

recently, I've reorganized some elements in my C#-GUI (move some buttons and dropdown-menus to another ribbon / ribbonbar). Somehow this lead to lost of references, such that the 'functions behind' the buttons / dropdown-menus can't be called right now.

My commit before I made this changes is fine (GUI-functions work). However, for the version after I introduced the new GUI-layout, it looks like nested functions does not get called properly. Well, the initial function still gets called after reorganization, ie the 'button_Click()', but the nested functions 'inside' seem partially not to work. Does anyone give me please a hint on either how to avoid such a situation while restructuring the GUI in general or how I can find and fix all my missing refernces on a fast way?

Many thanks in advance.

For your question, I would suggest that you can rebind the event to the moved control to make them can be called again.

Here is a code example you can refer to, which can use the moved control successfully.(I assume that it is a winform app)

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void MoveClick(object sender, EventArgs e)
        {
            MessageBox.Show("Hello!");
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
            Form2 form = (Form2)Application.OpenForms["Form2"];
            foreach (Control item in form.Controls)
            {
                if(item.Name=="btnmove")
                {
                    item.Click += MoveClick;
                }
            }
            
        }
    }

Also, we need to bind the event with Move button in the menu.

在此处输入图像描述

Tested result: 在此处输入图像描述

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