C# 反复计算时间后重新打开表单
我有一个问题与一些代码在这里,它的东西就像你有一个表单,最大化和控制屏幕,当你登录你有时间使用计算机,就在时间停止或结束后的时间计算的形式将再次最大化 问题是我不知道如何捕捉计时器是否停止或完成计数 谢谢你抽出时间C# 反复计算时间后重新打开表单,c#,winforms,timer,C#,Winforms,Timer,我有一个问题与一些代码在这里,它的东西就像你有一个表单,最大化和控制屏幕,当你登录你有时间使用计算机,就在时间停止或结束后的时间计算的形式将再次最大化 问题是我不知道如何捕捉计时器是否停止或完成计数 谢谢你抽出时间 private void Monitoring_Load(object sender, EventArgs e) { try { fs.Maximize(this, this, e); } catch (Exc
private void Monitoring_Load(object sender, EventArgs e)
{
try
{
fs.Maximize(this, this, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Errors");
}
}
private void btn_login_Click(object sender, EventArgs e)
{
fs.Restore(this);
T.Show();
if (T.IsDisposed == true)
{
fs.Maximize(this, this, e);
}
else
{ fs.Restore(this); }
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Errors");
}
}
/////////////////////////////////////
public void Maximize(Form targetForm, object sender, EventArgs e)
{
//////////////////////////////////
ct.hidetaskbar();
cu.DisableTaskmanager();
cu.DisableUserSwitching();
cu.DisableWorkstationLock();
ck.KeyboardHook(this, e);
////////////////////////////////////
targetForm.WindowState = FormWindowState.Maximized;
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.TopMost = true;
targetForm.ShowInTaskbar = false;
targetForm.ControlBox = false;
FullScreen.SetWinFullScreen(targetForm.Handle);
}
////////////////////////////////////////
public void Restore(Form targetForm)
{
////////////////////////////////////
cu.EnableTaskManager();
cu.EnableUserSwitching();
cu.EnableWorkstationLock();
ct.showtaskbar();
ck.ReleaseKeyboardHook();
///////////////////////////////////
targetForm.ControlBox = false;
targetForm.FormBorderStyle = FormBorderStyle.FixedSingle;
targetForm.WindowState = FormWindowState.Minimized;
targetForm.ShowInTaskbar = true;
targetForm.Visible = true;
targetForm.Hide();
}
///////////////////////////////////
public partial class Timer : Form
{
int h;
int m = 15;
int s;
public Timer()
{
InitializeComponent();
Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(workingArea.Right - Size.Width,
workingArea.Bottom - Size.Height);
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (h > 0)
{
if (m > 0)
{
if (s > 0)
{
s--;
}
else
{
s = 59;
m--;
}
}
else
{
m = 59;
h--;
}
}
else
if (h == 0)
{
if (m > 0)
{
if (s > 0)
{
s--;
}
else
{
s = 59;
m--;
}
}
else
{
s--;
}
this.lb_h.Text = h.ToString();
this.lb_m.Text = m.ToString();
this.lb_s.Text = s.ToString();
}
if (h == 0 && m == 0 && s == 0)
{
timer1.Stop();
this.Close();
}
}
private void btn_end_Click(object sender, EventArgs e)
{
timer1.Stop();
this.Close();
}
}
如果您在表单中需要它,可以使用工具栏中的控件,如下所示:
class Timer : Form {
var myTimer = new System.Windows.Forms.Timer();
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) {
// If you want handle interval once call Stop method, if not remove this line
myTimer.Stop();
// Call waht you want
}
public void Run() {
myTimer.Tick += TimerEventProcessor;
// Sets the timer interval to 5 seconds.
myTimer.Interval = 5000;
myTimer.Start();
}
}
您可以使用
计时器
组件,在表单中设置结束时间,并在勾选
事件中检查日期时间。现在
大于或等于结束时间,则给定时间结束
在下面的示例中,我假设您有一个PasswordForm
,它在密码正确时返回DialogResult.OK
。我在开始时显示密码表,时间结束时:
DateTime endTime;
private void MainForm_Load(object sender, EventArgs e)
{
ShowPasswordForm();
}
private void ShowPasswordForm()
{
timer1.Stop();
var f = new PasswordForm();
var passed = false;
while (!passed)
{
if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
passed = true;
}
var startTime = DateTime.Now;
endTime = startTime.AddMinutes(1); //I used 1 minute for test, add the time you need.
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
var diff = endTime.Subtract(DateTime.Now);
this.Text = string.Format("Remaining: {0} d {1:D2}:{2:D2}:{3:D2}",
diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
if (DateTime.Now >= endTime)
{
timer1.Stop();
this.Text = "Remaining: 0 d 00:00:00";
MessageBox.Show("Time is over.");
ShowPasswordForm();
}
}
你不使用a有什么原因吗?哦,我确实使用它,在计时器窗体中计时器窗体是另一个窗体显示倒数计时器给用户我想捕捉的动作已关闭,或者如果它已停止,则再次显示最大化窗体,但不知道如何以及在何处使用计时器来计算计时器何时用完以解决问题,把问题分成几个小部分。例如,假设我想要一个倒计时计时器,当给定的时间结束时,显示一个消息框。删除与小问题无关的部分,如WndProc和检查密码。你的问题太忙,无法回答。最好是编辑答案,然后发布一个简单的问题:)@Nitro.de谢谢,我会找的。为什么每1秒都打一次,然后检查溢出时间限制?我认为最好将计时器设置为15分钟,并在事件调用后立即停止。@Klaudiuszbryjamus这只是一个例子。你说得对,OP可以根据他的要求使用更好的间歇时间。我添加了一个文本来显示剩余时间的倒计时,这样做更有意义:)不客气:)要测试,在密码表单中有两个按钮就足够了,其中一个设置DialogResult.OK,另一个设置DialogResult.Cancel。然后您可以简单地测试代码。如果您对答案有任何疑问,请告诉我。@RezaAghaei我可以用其他方式与您联系吗?虽然我在outlook.com上有电子邮件地址r.aghaei,但就问题与我联系的最佳方式是使用StackOverflow。我不使用电子邮件回答问题:)