I've tried to use event from an userControl to a form, but when I'm creating it in a form constructor I've an issue. I don't know where is a fail. There is my code.
UserControl
public GameField()
{
InitializeComponent();
button.Click += Button_Clicked;
}
public event EventHandler ButtonClicked;
private void Button_Clicked(object sender, EventArgs e)
{
if (this.ButtonClicked != null) this.ButtonClicked(sender, e);
}
Form
GameField gameField = new GameField(); //Instance of the derived class UserControl
public Form1()
{
InitializeComponent();
gameField.ButtonClicked += new EventHandler(this.btn_Click);
}
private void btn_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
There is an issue
enter image description here
I think you wanted to subscribe to Button_Clicked instead of ButtonClicked in GameField.
button.Click += Button_Clicked;
Edit
I see, I have a hunch that you have two instances of GameField. The one that you added through the forms designer, probably named gameField1 and the one that you added in code to your form called gameField.
If you open Form1.Designer.cs can you see a gameField1 in there (or whatever name you gave when you added it through the designer)?
Can you try the following:
gameField1.ButtonClicked += new EventHandler(btn_Clicked); // name can be other than gameField1, gameField1 is just the automatically generated name by VS
Related
I was reading similar questions about overloading eventhandlers by not using eventhandlers but delegates or by calling other functions from within the eventhandler. But I really can't see how I can bind the delegate to the custom control like I am binding ButtonClick in the code below. I have a form with let's say 10 custom controls. Each custom control has 5 buttons. The way I am passing the key presses from each button of each custom control is:
This is in my custom control's cs file (GlobalDebugMonitorControl.cs)
namespace GlobalDebugMonitor
{
public partial class GlobalDebugMonitorControl : UserControl
{
public GlobalDebugMonitorControl()
{
InitializeComponent();
}
public event EventHandler ButtonClick;
private void MultiControl_Click(object sender, EventArgs e)
{
if (this.ButtonClick != null)
this.ButtonClick(sender, e);//**How Do I put here both sender and this**
}
}
}
Then all the buttons in the custom control.designer.cs have something like this:
this.openFileBTN.Click += new System.EventHandler(this.MultiControl_Click);
this.editFilePathBTN.Click += new System.EventHandler(this.MultiControl_Click);
this.delControlBTN.Click += new System.EventHandler(this.MultiControl_Click);
this.addControlBTN.Click += new System.EventHandler(this.MultiControl_Click);
this.editCompanyNameBTN.Click += new System.EventHandler(this.MultiControl_Click);
And then in my form1
namespace GlobalDebugMonitor
{
public partial class Form1 : Form
{
protected void UserControl_ButtonClick(object sender, EventArgs e)
{
Button tempButton = (Button)sender;
GlobalDebugMonitorControl tempParentControl = (GlobalDebugMonitorControl)((tempButton.Parent).Parent).Parent;
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (string item in tempGlobalPaths)
{
GlobalDebugMonitorControl tempGDMcontrol = new GlobalDebugMonitorControl();
tempGDMcontrol.Name = item.Split(',')[0];
tempGDMcontrol.companyNameLBL.Text = item.Split(',')[0];
tempGDMcontrol.globalPathTXT.Text = item.Split(',')[1];
tempGDMcontrol.ButtonClick += new EventHandler(UserControl_ButtonClick);
flowLayoutPanel1.Controls.Add(tempGDMcontrol);
}
}
}
}
As you can see I create a tempButton by the sender to do some things based on which button was pressed of the 5 and by sender.parent.parent (the custom control is inside a table that is inside a flowlayout that is inside another panel etc)I finally reach the custom control that tells me which of the 10 custom controls had it's button pressed.
So the question is, is there a way to pass both the sender(button that was pressed) and the great grandfather (the custom control that owns the sender button)? I mean it works but this way I need to now how many "generations" I need to go up.
Thank you for reading me.
You could introduce your own type of EventArgs
public class CustomEventArgs : EventArgs
{
public GlobalDebugMonitorControl Control { get; set; }
public CustomEventArgs(GlobalDebugMonitorControl control)
{
this.Control = control;
}
}
and then change eventHandler to use it:
public event EventHandler<CustomEventArgs> ButtonClick;
so the calling code would be:
this.ButtonClick(sender, new CustomEventArgs(this));
and of course the implementer of the event:
protected void UserControl_ButtonClick(object sender, CustomEventArgs e)
{
Button tempButton = (Button)sender;
GlobalDebugMonitorControl tempParentControl = e.Control;
}
I have a userControl and I've a button there, I'd like to call event when I'm clicking on the button in my main form from userControl. I do this:
UserControl
public UserControlerConstructor()
{
_button.Click += new EventHandler(OnButtonClicked);
}
public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
public event ButtonClickedEventHandler OnUserControlButtonClicked;
private void OnButtonClicked(object sender, EventArgs e)
{
// Delegate the event to the caller
if (OnUserControlButtonClicked != null)
OnUserControlButtonClicked(this, e);
}
Form
public Form1()
{
userControlInstance.OnUserControlButtonClicked += new EventHandler(OnUCButtonClicked);
}
private void OnUCButtonClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
It doesn't work because when I click in the form do nothing in the form code, but it does in userControl code. But I'd like to do in form code. I don't know how to call event from userControl to the form.
Well now I don't know if you're explicity want to use the delegate, no? If not, why don't you just do:
public Form1()
{
userControlInstance._button.Click += OnUCButtonClicked;
}
private void OnUCButtonClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
up to now your code does not compile. You are using the wrong event handler type. It should show the following compiler error:
EventHandler cannot be converted to ButtonClickedEventHandler
Do the following steps:
1) put the declaration of the delegate outside of the class UserControlerConstructor:
public delegate void ButtonClickedEventHandler(object sender, EventArgs e);
public partial class UserControlerConstructor: UserControl
{
1) then change the type of the handler when registering the event in Form:
public Form1()
{
userControlInstance.OnUserControlButtonClicked += new ButtonClickedEventHandler(OnUCButtonClicked);
}
This way it should work
I've got a problem with subscribing from a form to an event in an user control.
MainForm-Code:
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
UserControl menuView = new mnlib.mnlibControl();
newWindow(menuView);
}
public void newWindow(UserControl control)
{
this.mainPanel.Controls.Clear();
this.mainPanel.Controls.Add(control);
}
mnlibControl.OnLearnClick += new EventHandler(ButtonClick); //Error in this line
protected void ButtonClick(object sender, EventArgs e)
{
//handling..
}
}
UserControl-Code:
public partial class mnlibControl : UserControl
{
public mnlibControl()
{
InitializeComponent();
}
private void btn_beenden_Click(object sender, EventArgs e)
{
Application.Exit();
}
public event EventHandler LearnClick;
private void btn_lernen_Click(object sender, EventArgs e)
{
if (this.LearnClick != null)
this.LearnClick(this, e);
}
}
Now, visual studio marks the "mnlibControl.OnLearnClick ..." line as wrong. "mnlibControl" would not be found, maybe a missing using directive etc. .
All this code and both forms are located in the same project file.
I tried around and googled like hell but just can't find a solution for my problem.
In the UserControl form there is a button - when it's clicket it shall trigger the newWindow method in the mainForm and open up another window.
My source for this solution of my problem is: How do I make an Event in the Usercontrol and Have it Handeled in the Main Form?
There is no OnLearnClick in your component. You need to subscribe to LearnClick. You also need to subscribe in function block. You also should use concrete type (mnlib.mnlibControl), not UserControl:
public mainForm()
{
InitializeComponent();
mnlib.mnlibControl menuView = new mnlib.mnlibControl();
menuView.LearnClick += new EventHandler(ButtonClick);
newWindow(menuView);
}
Your code mnlibControl.OnLearnClick += new EventHandler(ButtonClick); must be within any of functional block (i.e. method, property, ...).
You have to place this line inside an actual method:
mnlibControl.LearnClick += new EventHandler(ButtonClick);
Like this:
public mainForm()
{
InitializeComponent();
UserControl menuView = new mnlib.mnlibControl();
newWindow(menuView);
mnlibControl.OnLearnClick += new EventHandler(ButtonClick);
}
I create a event and handle it on the Form
User Control Code:
public event EventHandler ButtonClick;
private void button1_Click(object sender, EventArgs e)
{
if (this.ButtonClick != null)
this.ButtonClick(sender, e);
}
Form Code:
private Usercontrol1 sampleUserControl = new Usercontrol1();
public Form1()
{
InitializeComponent();
sampleUserControl.ButtonClick += new EventHandler(this.UserControl_ButtonClick);
}
private void UserControl_ButtonClick(object sender, EventArgs e)
{
//sample event stuff
this.Close();
Form2 F2 = new Form2();
F2.Show();
}
but the event does not firing. What must be the problem?
private Usercontrol1 sampleUserControl = new Usercontrol1();
It isn't terribly obvious exactly what button you clicked, but it won't be the button on that user control. You never added it to the form's Controls collection so it is not visible. Fix:
public Form1()
{
InitializeComponent();
this.Controls.Add(sampleUserControl);
sampleUserControl.BringToFront();
sampleUserControl.ButtonClick += new EventHandler(this.UserControl_ButtonClick);
}
With some odds that you now have two of those user controls on your form. One that you previously dropped on the form, possibly with the name "Usercontrol11". And the one you added in code. Either use the designer or write the code, doing it both ways causes this kind of trouble.
i've taken your code and compiled it and the event fired right off, so my answer is that the ButtonClick event that you created and fired in the method button1_Click isn't called because the method is not connected to the event of the button clicked for some reason.
please check that the method is called and that the event ButtonClick have been registered on the moment the method button1_Click was called. if the method was not called, you haven't registered the button1_Click method. otherwise, you might have registered to some other item
public Form1()
{
InitializeComponent();
this.Usercontrol11.ButtonClickEvent += new EventHandler(UserControl_ButtonClick);
}
private void UserControl_ButtonClick(object sender, EventArgs e)
{
//sample event stuff
this.Close();
Form2 F2 = new Form2();
F2.Show();
}
Usercontrol1 is the name of usercontrol. So when I add the usercontrol1 to form i gives me the name Usercontrol11
Im making a userControl named [File_Manager] and i was wondering if i can add a button to this custom control that i can set its job later after adding this custom control to another form .. something like
File_Manager fManager = new File_Manager();
fManager.SetFreeButtonJob( MessageBox.Show("Hello") ); // something like this.
then whenever user press that button .. the messageBox shows up.
So.. Is it possible to do that?
thanks in advance.
Sure you can. Just attach the buttons click handler to the action you pass in.
fManager.SetFreeButtonJob(() => MessageBox.Show("Hello"));
private void SetFreeButtonJob(Action action)
{
button1.Click += (s,e) => action();
}
Just note that passing in the Action breaks the encapsulation of user control though. You should probably do something like SetFreeButtonJob(Jobs.SayHello); and put the knowledge of what to do inside the control.
Create a custom event for your UserControl and fire it when your Button is clicked. You can then attach an event handler to the custom event in your Form. Or you can just raise the UserControl's Click Event when your Button is clicked.
public delegate void CustomClickEventHandler(object sender, EventArgs e);
public partial class buttonTest : UserControl
{
public event CustomClickEventHandler CustomClick;
public buttonTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CustomClick(sender, e);
}
}
and in your Form
public Form1()
{
InitializeComponent();
buttonTest1.CustomClick +=new CustomClickEventHandler(userControl1_ButtonClick);
}
private void userControl1_ButtonClick(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
Or as my second option try.
private void button2_Click(object sender, EventArgs e)
{
OnClick(e);
}
and in your Form subscribe to the UserControl's Click event.
buttonTest1.Click +=new EventHandler(buttonTest1_Click);
private void buttonTest1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Again");
}