jelle woord
jelle woord

Reputation: 193

Call a button on a C# form through usercontrol

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 sender, EventArgs e)
    {
        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;
        }
    }

on my form is another button which is enabled = false; so when the color in the UserControl changes I want the button on my form enabled=true;

Upvotes: 1

Views: 4482

Answers (4)

Nino
Nino

Reputation: 7115

In your UserControl make event handler ColorChanged and fire that event when color changes. In your form add listener and appropriate code when event fires.

So, in your control, make EventHandler, like this

public partial class UserControl1 : UserControl
{
    public EventHandler ColorChanged; 

then, fire event on your button click, like this:

private void button1(object sender, EventArgs e)
{
    ColorChanged?.Invoke(sender, e);
    //rest of your code...
}

in your form, add listener:

userControl.ColorChanged += new EventHandler(UserControl_ColorChanged)

and add method that will be executed and enable button...

private void UserControl_ColorChanged(object sender, EventArgs e)
{
    //enable button here
}

Upvotes: 3

jelle woord
jelle woord

Reputation: 193

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;};
    }

Upvotes: 0

toby
toby

Reputation: 40

You can get a reference to the parent form using this

Form parentFrm = (this.Parent as Form);

You can then access controls on the parent Form, by either making the public or finding the control by its name

 Button aButton = (Button)parentFrm.Controls["btnName"];
 if (aButton != null)
     aButton.Enabled = true;

Upvotes: 1

Bchir Med Amine
Bchir Med Amine

Reputation: 88

you can use Event Aggregator and make the button in user control will publish an event and make you window handle the event

Upvotes: 0

Related Questions