A label in form is displaying the count of timer. Now i want to stop,start and reset it using form 2. How can i do this.plz help
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
If the timer object resides in Form1, then create a public property for it:
Then you can access this timer by having a reference to Form 1 in Form 2. You can do this by passing it in to the constructor, or having a set property on Form2. Once you have a reference to Form1, you can simply call methods on the timer:
You can always create a singleton out of Form1 if you cannot pass a reference of it to Form2.
Declare your singleton:
Initialize your singleton if it isnt already, and return it:
For best practices, make your Form1 constructor private. This of course will not work if Form1 does not have a default constructor (parameterless).
Then in Form2:
Do this
On Form load
Access form and it's controls from another form
Forms are just classes, and the timer on Form 2 is an object inside that class.
You can change the
Modifiers
property of your timer to public, and then instantiate Form 2 inside Form 1, call theShow()
method of Form 2, and then access your timer object which is now public.So you have a project with 2 forms like so:
Create a button in Form 1 like so:
Place a timer object on Form 2 and change the access modifier like so:
Then put the following code under your button in form one:
Now you can launch form 2 and access all of the properties on the timer on form 2 from form 1.
Does this help?