Call a button on a C# form through usercontrol

后端 未结 4 2162
广开言路
广开言路 2021-01-23 18:37

I\'ve created a user control with some buttons. When you click a button in the UserControl the BackColor of the button changes:

 private void button1(object send         


        
4条回答
  •  猫巷女王i
    2021-01-23 18:49

    i fixed it like this;

    control:

    public delegate void ColorChangeEventHandler();
    
    public partial class myControl: UserControl
    {
    
    
        public event ColorChangeEventHandler ColorChanged;
    
        private void OnColorChange()
        {
            if(ColorChanged != null)
            {
                ColorChanged.Invoke();
            }
        }
    
        public speelRij()
        {
            InitializeComponent();
        }
    
      private void Button1_Click(object sender, EventArgs e)
        {
            OnColorChange();
            Control ctrl = ((Control)sender);
            switch (ctrl.BackColor.Name)
            {
                case "Crimson": ctrl.BackColor = Color.Blue; break;
                case "Green": ctrl.BackColor = Color.Orange; break;
                case "Orange": ctrl.BackColor = Color.Crimson; break;
                case "Blue": ctrl.BackColor = Color.Green; break;
                default: ctrl.BackColor = Color.Crimson; break;
            }
    

    form:

     public myForm()
        {
            InitializeComponent();
            myControl1.ColorChanged += () => { Button1.Enabled = true;};
        }
    

提交回复
热议问题