Unable to reset time duration [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
On this code I am starting and stopping random sampling based on a fixed sampling time.
When I clicked stop sampling, timers stops correctly.
But when I start back again, the times do not start correctly.
could you please help to check what is wrong here?
public partial class MainWindow : Window
{
public float samplingTime = 10f;
double currentDateTime = 0.0;
DateTime dtEnd;
public bool running;
public MainWindow()
{
InitializeComponent();
timer1 start.code()
void timer_Tick(object sender, EventArgs e)
{
dtEnd = DateTime.Now.AddSeconds(samplingTime);
someCode();
}
}
private void startSample_Click(object sender, RoutedEventArgs e)
{
timer2 start code()
void windowtimer_Tick(object sender, EventArgs e)
{
currentDateTime = (dtEnd - DateTime.Now).TotalSeconds;
if (currentDateTime < 0.1 && running == true)
{
code();
}
}
}
private void stopSample_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
windowTimer.Stop();
}

You are adding the Tick handler multiple times:
windowTimer.Tick += windowtimer_Tick;
is executed every time you start, but you don't unhook it. Probably you should set the handler elsewhere (in the designer maybe).
Or you can add:
windowTimer.Tick -= windowtimer_Tick;
to the Stop handle

Related

All the if statements running in c#? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have some code below which is a URL Parser and the if statement doesn't work instead of executing the code when it is true it executes all the code in all the if statements.
public static class var
{ //Global variables
public static string stre = System.Uri.UnescapeDataString(Environment.CommandLine);
public static string[] str = stre.Remove(stre.Length - 1).Split(new string[] { "cmd:" }, StringSplitOptions.None);
}
public CMD()
{ //I suppose this class runs first so all my is statements are here
AClsMsgBox.Show("Parsing...", "CMD Parser", 1000); //msgbox
if (var.stre.Length > 5) { InitializeComponent();} //This executes even if I type "cmd:" which should trigger the statement below. (Always executes) This should run when typing "cmd:echo a command"
else if (var.stre.Length == 4) { Process.Start("cmd.exe"); } //This should run when typing "cmd:"(Never execute)
else { AClsMsgBox.Show("This is a URL Parser use it by typing 'cmd:<command>' in the URL adress bar field of a browsers.","CMD Parser",60000); } //Always executes
}
private void Form1_Load(object sender, EventArgs e)
{
string stra = var.str[1].Replace("&", "&&");
label1.Text += stra;
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("cmd.exe", " /k " + var.str[1]);
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void label1_Click(object sender, EventArgs e) { }
}
This is C# GUI so I am running InitializeComponent() only if I need to display the rest of buttons and stuff. (I think it should stop the GUI)
And there are no errors! But appear after building the code.
On running debugger:
System.IndexOutOfRangeException
HResult=0x80131508
Message=Index was outside the bounds of the array.
Source=CMD Parser GUI
StackTrace:
at CMD_Parser_GUI.CMD.Form1_Load(Object sender, EventArgs e) in D:\bat\CMD Parser GUI\Form1.cs:line 61
line 61 is "string stra = var.str[1].Replace("&", "&&");"

Adding a clock to the UI using BackgroundWorker C# [duplicate]

This question already has answers here:
How to report progress from within a class to a BackgroundWorker?
(4 answers)
Regularly report progress of a BackgroundWorker
(2 answers)
Run a Digital Clock on your WinForm
(1 answer)
Clock on Windows Form without polling?
(1 answer)
Digital clock not displaying
(1 answer)
Closed 2 years ago.
I have a WinForms app which I'd like to have a clock in the bottom left. So far I managed to display the time when it launched, but I can't make it update itself. I tried to use a BackgroundWorker but since it is on a different thread it errors out so it can't change anything.
I hope there's a somewhat easy fix to my problem
Thanks in advance!
Edit 1 would be adding some of the code
DateTime output;
Random rnd = new Random();
private void GetTime_DoWork(object sender, DoWorkEventArgs e)
{
GetTime.ReportProgress(rnd.Next(1, 62));
}
private void GetTime_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
output = DateTime.Now;
TimeLabel.Text = output.Hour + ":" + output.Minute + ":" + output.Second;
DateLabel.Text = output.ToString("yyyy-MM-dd, dddd");
}
"output" is a DateTime type variable where I'd like to store the values of the current time
The random number generator is only used to change the progress
TimeLabel only outputs time
DateLabel outputs in 2020-01-01 Wednesday format
Edit 2: Using Timer element worked, Thread.Sleep(1000); isn't used since it would halt everything else on the form
Adding new code:
public Form1()
{
InitializeComponent();
TimeUpdate.Start();
}
//...
private void TimeUpdate_Tick(object sender, EventArgs e)
{
output = DateTime.Now;
TimeLabel.Text = output.ToString("HH:mm:ss");
DateLabel.Text = output.ToString("yyyy-MM-dd, dddd");
}
//TimeUpdate is the name of the Timer
Still think you should use the standard Timer control.
If you want to keep that BackgroundWorker, though, then you need to make it loop forever and pause for one second between updates:
private void Form1_Load(object sender, EventArgs e)
{
GetTime.RunWorkerAsync();
}
private void GetTime_DoWork(object sender, DoWorkEventArgs e)
{
while(true)
{
GetTime.ReportProgress(rnd.Next(1, 62));
System.Threading.Thread.Sleep(1000);
}
}
private void GetTime_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
output = DateTime.Now;
TimeLabel.Text = output.ToString("h:mm:ss tt");
DateLabel.Text = output.ToString("yyyy-MM-dd, dddd");
}
Since it is a background thread, it will automatically be killed when the form is closed.

Determine Which Control In My WPF Has Focus Via Script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to determine which control has focus in C# ?
Example :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox); /*Here there my textBox has focus.*/
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, /*I mean here : the focus remains where it is already, on the textBox.*/);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
myControl.Focus();
}
In the NotCorrect() method :
textBlock.Background means : I want the same brush as the one already existing (see the first line of the Click event), I do not want to change anything.
(I prefer to write textBlock.Background rather than rewrite Brushes.White.)
Now I ask if there is also a way to say : I want the focus to remain on the control on which it is already, I do not want to change anything.
Form.ActiveControl is you should search for.
I found out a kind of solution using null :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox);
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, null);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
if (myControl != null)
myControl.Focus();
}
Another simpler solution I found is to use this :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox);
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, this);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
myControl.Focus();
}

Visual effect on a mouse click in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want that if i click on an image it gives a visual effect like whirl effect or glow effect or anything else on specific part around the point where i clicked with the mouse. for example if anyone has used the picture password of UC browser of windows phone exactly the same i want.
I have not tried anything because i have no knowledge of animation and graphics hence i haven't tried anything.
public void start()
{
messagebox.show("i haven't tried anything yet no knowledge of animation");
}
This code is nothing but i wrote it because i was not able to post the question.
In Winforms you could write code like this:
int AnimationCounter = 0;
Point AnimationCenter = Point.Empty;
Timer AnimationTimer = new Timer();
private void pictureBox1 _MouseClick(object sender, MouseEventArgs e)
{
AnimationCenter = e.Location;
AnimationTimer.Interval = 20;
AnimationTimer.Start();
}
void AnimationTimer_Tick(object sender, EventArgs e)
{
if (AnimationCounter > 15)
{
AnimationTimer.Stop();
AnimationCounter = 0;
pictureBox1.Invalidate();
}
else
{
AnimationCounter += 1;
pictureBox1.Invalidate();
}
}
private void pictureBox1 _Paint(object sender, PaintEventArgs e)
{
if (AnimationCounter > 0)
{
int ac = AnimationCounter / 2;
e.Graphics.DrawEllipse(Pens.Orange, AnimationCenter.X - ac,
AnimationCenter.Y - ac, ac * 2, ac * 2);
}
}
Don't forget to hook up the Paint and MouseClick event and also the AnimationTimer_Tick event.!
The result will draw a growing circle at the spot you click on which will disappear after ca. 10 * 20 ms..
Update: The first version suffered from repeatedly hooking up the Tick event. This one is better tested ;-)

Why doesn't my second form show up? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I made a timer, but when I press a button nothing happens.
My code is:
private void button3_Click(object sender, EventArgs e)
{
Instellingen form = new Instellingen();
form.Show();
}
Instellingen.cs code:
public partial class Instellingen : Form
{
public Instellingen()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("text", "text");
}
}
}
The button3 event doesn't even fire (confirmed by adding a breakpoint, it doesn't get there)
Check if the button's event realy connected to the method button3_Click.
Put a breakpoint in the first line of button3_Click and check if the code get there.
Make sure your button's click event is button3_Click. Probably your button's click event is empty.

Categories

Resources