How to start timer in Visual Studio 2010 Form Application - c#

this is my first time posting, but I was wondering how i can get a timer to increment once i start my program. I have the timer and the stop button, and i can get it to start if i have a start button to press, but i want it to start once the application is opened as there are multiple timers one after another in the game i'm making. thanks everyone. i tried searching this, but couldn't find anything. I'm still pretty new to app programming so it's not something i have done before and don't even know what to try for it.

Welcome,
I would suggest to locate the FormLoad eventhandler (either by double clicking the form or by looking in the properties window (look for the lightening icon) and search for the Load eventhandler.
Or you could try to use the FormShown eventhandler, which will be triggered when the form is actually shown instead of loaded. Depending on what you are trying to achieve, this might be a better option.
Either way, you will need to put
Timer.Start()
inside such an event handler.
Hope it helps.

In Form's Load event (doubleclick on form) you can use
timer1.Start();

You can start a timer by simply calling Timer.Start()
When to call this depends on the trigger. Typically you write this in an event handler, of the "button click event" like you mention, or the app's "app start event" such as the Main method, but this depends on what kind of app (WinForms?) you are writing.

You can start timer on starting the program either by starting the timer in the following way:
public Form1()
{
InitializeComponent();
time.Start();
time.Tick += time_Tick;
}
or by initiating the timer in Form Load Event Handler

Related

C# adding infinity loop in button click event and break it with another button click event

I tried to make a while (work){....}
inside button event called start
to do some stuff like label changing in the same form with random values
and I have another button called stop I want to make the work=false; so the while should be break
the problem is when I clicked start button and the form froze and did nothing
how I can do that like stay in the while loop and access all other events
As long as the loop runs, no events (like your other button's click) can be processed. This results in freezing the UI.
Better use a Timer instead of an infinite loop. The timer will not freeze the UI but call a Tick event at defined intervals and allow other events to be processed between two ticks. It is then easy for another button to stop this timer.
Since you have not mentioned any UI technology (is it WinForms, WPF, WebForms, MAUI, Xamarin, ...?) and not shown any code, it is difficult to give you example code.

Slow reaction of MouseClick event for NotifyIcon

I noticed strange behaviour of left-click event for NotifyIcon.
I have a code like this:
private void notifyIcon2_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Console.WriteLine("Hello!");
}
}
The problem is that upon clicking notifyicon in tray string "Hello" is not shown immediately, it takes about 0.5 seconds (half of a second) to react. That is why I can not add some sort of variable-counter for each click of the icon - it just reacts too slow to catch all clicks and increment my variable.
Is there any solution to the problem? I tried MouseClick, MouseDown, MouseUp and Click events, and all of them have such a slow reaction.
Thank you!
I think it is related to this little comment they make here (I know this is not this NotifyIcon).
Note that the LeftClickCommand fires after a short delay (as opposite to the DoubleClickCommand that fires immediately). This is because there is a time span between a first click and a second click for the OS to consider the mouse action a double-click. The NotifyIcon is smart enough to wait this period in order to make sure the LeftClickCommand is only fired if the user does not click a second time within that period.
I tried it and this delay is present on the Form itself as well. This is just how this event works.
Implementing a handler for the DoubleClick event was not a solution in my case where I wanted only the single click to open the NotifyIcon's popup.
I found the NoLeftClickDelay property in the code completion that makes things to work as wanted.
TaskbarIcon tbIcon = (TaskbarIcon)FindResource("MyNotifyIcon");
tbIcon.NoLeftClickDelay = true;

Stop action when button is released

In my WP8 app that controls Lego Mindstorms I have a Button with UIElement.Hold Event that triggers method runMotor() When I release the Button motor keeps on going but I would like it to stop. Method for stopping is stopMotor(), I've already tried to assign it to KeyUp Event but it doesn't work. Any solutions?
You can try to call stopMotor() in ManupulationCompleted event. Note that ManipulationCompleted event will get invoked after any gesture manipulation including Tap, Double Tap, Hold, and other gesture. Take that into account. If application scenario is still simple, checking if motor already running before calling stopMotor in ManipulationCompleted event handler maybe enough :
private void MyButton_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
if(isMotorRunning) stopMotor();
}

How to separately handling MouseClick and MouseDoubleClick event on ChartControl

I'm implementing a module which needs to handle both MouseClick and MouseDoubleClick on a ChartControl of DevExpress. The version that I'm using is v12.2.
When I double click on that chart, both events are fired. I'd like (and I think that it must be) it just fires one event, in this case, MouseDoubleClick.
So, anyone know how to fix that problem?
What's I've tried:
Handle MouseClick or Click event and see MouseEventArgs#Clicks property. But it's always 1.
What's I'm using:
Declare a boolean variable to tell if MouseDoubleClick is fired. On MouseClick handling, just waiting for a moment, then do the codes if that variable does not turn on. I think this is a bad implementation.
You need a time machine to see the difference between the two. Inevitably a double-click starts with a single click, you always see the first click first.
You can get a time machine that sees the future by using a timer that delays the past. Set its interval to SystemInformation.DoubleClickTime + 16 and start it in the Click event, stop it in the DoubleClick event. If the Tick event fires then it was a single click.
That works, but do note that the delayed response to a single click is fairly annoying. Best not to annoy your user with a user interface like that.

What events are triggered when ShowDialog(ParentForm) is called in C#

Simple question. I have a MainForm and a settingsForm. The settings form is initialized once and then shown every time the user clicks a button. I need it to do something when this button is clicked.
m_inputSettings.ShowDialog(this); //edit settings selected, so show that form
This is the MainForm calling the settings form, which it does fine. But I need the SettingsForm to do something every time this happens. Presently, I cant figure out if this call actually triggers any events I can set handlers for. Does it trigger an event? If not, is there another way I can tell my SettingsForm to do something every time this call happens?
Note: Any code in the mainform after that line doesn't get executed until the SettingsForm returns, but that is intentional.
Thanks.
Edit: One of the things I want my form to do it select a specific control when this happens, but it seems that that is impossible until after the form is done loading everything.
You can override the OnVisibleChanged method in your settings form. Make sure to call base.OnVisibleChanged though as to not screw up any potential observers of the event (and anything else the base class may do inside of that method.)
FormShown event - raised only once when form is displayed first time.
OnPaint / OnActivate - every time form is activated, but these events raised even when you switch with other application, which probably you don't want to do.
If you are changing form visbility, then you can use OnVisibleChanged
If you are minimizing the form, you can use OnSizeChanged / OnLocationChanged event.
If none suits you, make a public property and set false when form is closed / hidded, and set true before showing it. OnActivate, use this property to do your task.
Maybe use VisibleChanged event.
Override OnShown() event in your form this will raise only once after the form is opened
The disadvantage of OnVisibleChanged is it will also get raised when the form is closed.
On Paint , On Activate and OnEnter will raise before form is shown to the user.

Categories

Resources