Usercontrol handle in Form not Firing - c#

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

Related

Detect when a child form is closed

I have he following Form:
An Initialize Function that is called when the Form1 is created.
A button that opens another Form (Form2)
What I need is to call Initialize() not only when Form1 is created, but whenever Form2 is closed, since Form2 might have modified some stuff that makes Initialize need to be called again.
How can I detect when the form2 is closed?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Initialize();
}
void Initialize()
{
// Read a config file and initialize some stuff
}
// Clicking this button will open a Form2
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2().Show();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
// some stuff that Form2 does which involves modifying the config file
}
}
You just need to add an event handler for the FormClosing event, this handler could be in your first form class and here you can call every internal method of that class
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.FormClosing += childClosing;
form2.Show();
}
private void childClosing(object sender, FormClosingEventArgs e)
{
Initialize();
....
}
In addition to Steve's excellent answer, you may consider displaying Form2 as a modal dialog. This would mean code execution in Form1 STOPS until Form2 is dismissed. This may or may not work well with your application, we have no idea what it does.
At any rate, you'd use ShowDialog() instead of Show() like this:
// Clicking this button will open a Form2
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2().ShowDialog(); // <-- code STOPS here until form2 is closed
Initialize(); // form2 was closed, update everything
}

Field System.MulticastDelegate._invocationCount is not available c#

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

Switch tab with form control button on a different form

I have created a form with 5 static tabs and then created 5 separate forms that load in to them at Main form load. The first of these tabs has buttons on the form that I want to link to and show the corresponding tab when clicked.
The problem I'm having is that the buttons not being on the main form but a separate form don't have the options to switch to the corresponding tab on the main form.
I have tried the tabControl1.SelectedTab = tabPage2 stumped me for a couple of days this one any help much appreciated
As I understand your question, your problem is "How to communicate the button click on the sub form to the main form".
I'd suggest to declare events in your sub forms and catch these events in your main form. So a simplified example of a sub form could look like this:
public class SubForm : Form
{
// this is the event to register with from MainForm
public event EventHandler ButtonClicked;
// thread safe event invocator (standard pattern)
protected virtual void OnButtonClicked()
{
EventHandler handler = ButtonClicked;
if (handler != null) handler(this, EventArgs.Empty);
}
// your button handler
private void button1_Clicked(object sender, Eventargs e)
{
OnButtonClicked();
}
}
And in your main form you could do the following:
public class MainForm : Form
{
private SubForm _subForm1 = new SubForm();
private SubForm _subForm2 = new SubForm();
public MainForm()
{
InitializeComponents();
// more initializatino
_subForm1.ButtonClicked += subForm1_ButtonClicked;
_subForm2.ButtonClicked += subForm2_ButtonClicked;
}
private void subForm1_ButtonClicked(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
}
private void subForm2_ButtonClicked(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage2;
}
And you can also think about using only one someSubForm_ButtonClicked handler and infer which tab to select via the sender argument, which will be the SubForm that raised the event.

how to set a custom control button's job after adding it to some form?

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

Sending button event to object host class

I have a windows form called FormMain in ClientForms project. Now this(FormMain) form opens another form called FormScheduler in Scheduling project.
Now, I want to send a message back to FormMain when a button_click method event is triggered in FormScheduler.
My solution creates a circular dependency. Is there another way, like using delegates ?
Use events.
In your form FormScheduer add a button and the following code:
public event EventHandler ButtonClicked;
private void button1_Click(object sender, EventArgs e) {
if (ButtonClicked != null) {
ButtonClicked(this, EventArgs.Empty);
}
}
In your FormMain, instantiate and display your FormScheduer form like this:
var form = new FormScheduer();
// Listen for the ButtonClicked event...
form.ButtonClicked += form__ButtonClicked;
form.Show();
Your form_ButtonClicked method on FormMain will be called when the button on FormScheduler is clicked:
void form__ButtonClicked(object sender, EventArgs e) {
Console.WriteLine("clicked");
}

Categories