0

I have an application that consists of several forms. There is a MainForm and then other forms open up within this MainForm. One of the child forms that opens up (we'll just call it Form1) contains a user control that has controls inside it, such as a datagrid view. This datagridview control has several events that are triggered, one being a "cellclick" event.

This click event must access a public timer object that is declared in Form1.

Currently just to make it work I used the code:

((Form1)this.Parent.Parent.Parent.Parent.Parent).clickTimer.Start();

However this doesn't seem like the best way I could be accessing this Timer object, and could potentially be a headache for future development of Form1 and its User Controls.

What are alternative ways of accessing my Form1 timer from inside the user control's datagridview click event?

Any advice is appreciated as I'm drawing a blank on this right now.

Thanks

1
  • I suspect the design; I'd strive to make child controls independent of their parents. Commented Aug 1, 2012 at 16:42

3 Answers 3

2

You could have the user control take the Timer instance as constructor argument and store it in a private backing field for future access.

Sign up to request clarification or add additional context in comments.

10 Comments

This can be a problem when using the Forms Designer.
@Erno Not if you leave the default constructor how it is, but add a new one for your own use.
@AlexanderR - how would you use both?
Wouldn't I have to change my Form1 designer to use the overloaded constructor? Currently that is the only place the user control is called.
You have two constructors on the user control, one - the default autogenerated one - that takes no parameters, and the other, which takes a Timer object. Then just call the one that you want in code. Of course, you'll need to handle the timer field not being initialised in the rest of the class.
|
1

Well I figured out more elegant way to access my Form1 object(as opposed to use Parent.Parent.Parent...etc.) with:

Control activeForm = this.FindForm();

So now I have:

((Form1)activeForm).clickTimer.Start();

The above code gives me the form the userControl exists inside(Form1), and since the Forms that the UC exists inside will always have a Timer, this should work even if the user control is placed on a different form (say Form2).

However, @Darin did provide a good alternative, but I chose to use my solution because it provided me the quickest way to fix my issue with least amount of code changes required yet still provides low maintainability for my Forms. Specific to my situation, of course.

1 Comment

PS - thanks for the advice..just needed to get my brain working.
0

You could create a static event in the child form and register to it in the parent form.

Trigger the event on "cellclick" and trigger the timer in the event handler on your parent form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.