Creating Event for Multiple Forms - c#

I have tried a bunch of different things, so obviously I am now stuck... I have created a form, it has a button on it - that when clicked creates a new form. I can click away and create multiple forms this way. What I would like and can not get to work is to have the main form have a second button on it - that when clicked will change all of the background colors on the secondary forms.
Thanks - I am guessing I close, but then again - close doesn't work...

Bascally you do not need event or delegate type of things to solve this issue. In your secondary forms write a public method to change background color. Keep a list of secondary forms and when button is clicked just loop through all your secondary forms and call the color changing methods
Using events
In your parent form do something like this.
private event Action<Color> ChangeColor;
private void CreateAndShowForm()
{
var form2 = new Form2();
ChangeColor += form2.changeColor;
/*do other stuff to show form*/
}
private void button1_Click(object sender, EventArgs e)
{
ChangeColor(Color.Red);
}
In the child forms
public void changeColor(Color obj)
{
/*change background color*/
}

There are a few ways to achieve this, but one way is to keep a collection of all child Forms in the main form and call a custom change background color method on each of them. You can create a ChildFormBase class that they all can inherit from where you can define the method to avoid repeating it in all child forms.
You can also do this with an event that you raise in the MainForm that the child forms can subscribe to.

In .NET, when an event is raised, all the objects listening to it (registered as event listeners) are notified that the event has been raised and execute the respective event handler. Therefore, in your case, each subform should be registered to the specific event of the main form, as an event listener. Each time the main form raises the event, the subforms will be notified that the event has been raised and act accordingly.
You could see this as a guide to the events paradigm in C#.
Hope I helped!

Related

Detect CloseEvent through different forms

PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}

Is there an event that fires after load for a user control?

I'm using User Controls in C# winforms, and I would like some code to be executed after the load event, and after the control has been shown. If no such event exists, is it possible to make one?
You could use one of events from this list. OnPaint would be most likely candidate.
Form Events:
Construtor
Load
Layout
Activated
Paint­
Closing
Closed
Deactivate
Dispose
and for Controls:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
If you can't find one that fits you needs, this article explains how to construct and fire event.
Create a public method in user control
Call the method on Parent win-form Shown Event
This will call the code you want to run once.
You can also use user-control paint event, but this will Expensive call. Because your code will execute every time the control is redrawn.
So it is better to use a flag which you can determine whether to run the code or not
i.e.
private bool _run =true;
private void Control_Paint(object sender, PaintEventArgs e)
{
if(!_run) return;
//call the code you need to run
_run = false;
}

how to access a user define controls iner controls in parent form

I'm new to windows forms programming so my question may sound little strange.
I have created a user define control (countdown timer) now I'm creating n no of it dynamically in a form by Click of a button (Add new timer) its working well and good.
Here is the Creation Code
private void Addnew_Click(object sender, EventArgs e)
{
UserControl1.userControl11 = new UserControl1();
flowLayoutPanel1.Controls.Add(userControl11);
}
My user control has a Reset button that reset all the content inside the user define control.
it is also working, but What I want Allow user to reset all the Created timers using the “Reset All” button on the form.
Okay one way to do this.
Create a List<UserControl1> private member on your form called say _myUserControls
In your Addnew Handler add it to the list.
If you have a remove button, don't forget to remove from _myUserControls as well.
Add a Reset method to your UserControl1, that does what it needs to do.
Then in your Reset all button click handler
foreach(UserControl1 ctrl in _myUserControls)
{
ctrl.Reset();
}
Jobs a good 'un
The answer I referred you to in comments, would be a way of finding all instances of your UserControl1 class, so you wouldn't need an internal list.

Need an event that will fire after activating a child form

I am working with an existing WinForm application and it uses the following code to re-activate a child form that has previously been loaded:
private void Activate(Form frm)
{
frm.WindowState = FormWindowState.Maximized;
this.ActivateMdiChild(frm);
}
The trouble I'm having is that when the form is re-activated, I can not seem to find any event on the form that gets raised naturally by the code above. I'm also having difficulty adding code to manually raise the event on the child form, and I think it's because I'm working with a generic Form object.
I've tried Load, MdiChildActivate, MaximumSizeChanged, Activated and a few other events, and none of them fire upon running the code above. I basically need to update some of the form elements after that code is called above and figured an event would work well.
Does anyone know an event that will be raised on the child form after it has been reactivated?
Have you tried the Enter event of the child form? I don't think it fires if the form is already active though.
Another option would be to cast it as your own type and add your own event, then fire it directly.

Simulating events on UIElement's without inheriting on Windows Phone 7

I have a very simple requirement, i.e. I want to send a synthetic event to a UIElement, in this case, a Grid. What I want is simply that I be able to send a synthetic event to an UIElement.
For example,
StackPanel myPanel;
StackPanel topPanel;
topPanel.MouseLeftButtonUp += new MouseButtonEventHandler(topPanel_MouseLeftButtonUp);
private void topPanel_MouseLeftButtonUp(object sender, MouseEventArgs args) {
// Here I want to send the MouseLeftButtonUp event to myPanel
}
It is possible using RaiseEvent, but it is a protected event and hence I cannot just call it on an instance of any UIElement. So how do I go about sending a synthetic event on existing classes?
P.S: The reason that I cannot create custom inherited classes is that the current code base is too huge and the number of changes that will be required in case I take such an approach are not feasible.
Any help is greatly appreciated.
Regards,
roahn
Instead of raising events, you could just move the relevant code from the event handler to a method. Then, you can just call the method whenever you want to simulate a button click. However, if you want to simulate the button click on an element, you could do this:
//Assuming myPanel_MouseLeftButtonUp is the event handler for myPanel
myPanel_MouseLeftButtonUp(null, null);

Categories

Resources