13.04.2021, 14:52. Показов 611. Ответов 0
Всем привет. Помогите решить задачку пожалуйста. Нужно чтобы при нажатии на кнопку "Остановить" таймер (имеется текст Осталось сек. -) останавливался, либо пропадал, пока не нажмёшь снова кнопку действия "Открыть дверь" или "Закрыть дверь". Как реализовать такую штуку идей нет, может натолкнёте на мысль, или кодом поможете

Всяко буду благодарен. Скрин программы приложил, и все используемые коды (их 2).
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsTestDoor
{
public partial class FormTestDoor : Form
{
Door door;
public FormTestDoor()
{
InitializeComponent();
openFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
saveFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
door = new Door(this); //Форма только запускает управляющий класс
door.eventClosing += () => OnEventClosing();
door.eventClose += () => OnEventClose(); //Подписка на сообщение событий
door.eventOpening += () => { OnEventOpening(); }; //Подписка на сообщение состояния
door.eventOpen += () => OnEventOpen();
door.eventWait += () => OnEventWait();
door.eventStop += () => OnEventStopTimer();
door.Reset();
}
private double Str2Double(String text)
{
if (String.IsNullOrEmpty(text)) return 0.0;
if (text == "+" || text == "-") return 0.0;
Double result;
if (Double.TryParse(text, out result))
return result;
else
throw new Exception("Double.TryParse(text,result)");
}
public void onEventStatus(string message)
{
OutLog("door->" + message);
}
public void onEventState(string message)
{
OutLog("onEventState->" + message);
OutState(message);
}
public void OnEventClosing()
{
Log("onEventClosing");
Outstate("Дверь закрывается");
}
public void OnEventClose()
{
Log("onEventClose");
Outstate("Дверь закрыта");
}
public void OnEventOpening()
{
Log("onEventOpening");
Outstate("Дверь открывается");
}
public void OnEventOpen()
{
Log("onEventOpen");
Outstate("Дверь открыта");
}
public void OnEventWait()
{
Log("onEventWait");
Outstate("Ожидание");
}
public void OnEventStopTimer()
{
Log("onEventStop");
Outstate("Остановка");
}
private void OutLog(string mess)
{
listBoxLog.Items.Add(mess);
}
private void ClearLog()
{
listBoxLog.Items.Clear();
}
private void OutState(string mess)
{
textBoxState.Text = mess;
}
private void buttonClearLog_Click(object sender, EventArgs e)
{
listBoxLog.Items.Clear();
}
private void Log(string mess)
{
listBoxLog.Items.Add(mess);
}
private void Outstate(string mess)
{
textBoxState.Text = mess;
}
private void buttonClose_Click(object sender, EventArgs e)
{
Log("buttonClose_Click");
InitInterval();
door.Close();
}
private void buttonOpen_Click(object sender, EventArgs e)
{
Log("buttonOpen_Click");
InitInterval();
door.Open();
}
private void InitInterval()
{
door.IntervalOpening = Str2Double(textBoxOpenDoor.Text);
door.IntervalClosing = Str2Double(textBoxCloseDoor.Text);
door.IntervalWait = Str2Double(textBoxWait.Text);
}
private void buttonStop_Click(object sender, EventArgs e)
{
Log("buttonStop_Click");
door.StopTimer();
}
private void buttonLoad_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
return;
string filename = openFileDialog1.FileName;
string fileText = System.IO.File.ReadAllText(filename);
string[] parametrs = fileText.Split(',');
MessageBox.Show("Файл открыт");
textBoxOpenDoor.Text = parametrs[0];
textBoxCloseDoor.Text = parametrs[1];
textBoxWait.Text = parametrs[2];
Log("buttonLoad_Click");
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
return;
string filename = saveFileDialog1.FileName;
System.IO.File.WriteAllText(filename, textBoxOpenDoor.Text + "," + textBoxCloseDoor.Text + "," + textBoxWait.Text);
MessageBox.Show("Файл сохранён");
Log("buttonSave_Click");
}
private void timerRemains_Tick(object sender, EventArgs e)
{
double remains = door.RemainsSec;
if (remains != 0.0)
labelRemains.Text = remains.ToString();
else
labelRemains.Text = "";
}
}
} |
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Forms;
namespace WindowsFormsTestDoor
{
class Door
{
private Form form;
private TimerElapsed timerOpen;
private TimerElapsed timerClose;
private TimerElapsed timerWait;
public delegate void DoorHandler();
public event DoorHandler eventClose;
public event DoorHandler eventOpening;
public event DoorHandler eventOpen;
public event DoorHandler eventClosing;
public event DoorHandler eventWait;
public event DoorHandler eventStop;
public void OnTimeOpen()
{
SetEvent(eventOpen);
Wait();
}
public void OnTimeClose()
{
SetEvent(eventClose);
}
public void OnTimeWait()
{
SetEvent(eventClosing);
timerClose.Interval = intervalClosing;
timerClose.Start();
}
public double RemainsSec
{
get
{
if (timerClose.Active) return timerClose.RemainsSec;
if (timerOpen.Active) return timerOpen.RemainsSec;
if (timerWait.Active) return timerWait.RemainsSec;
return 0.0;
}
}
private int intervalOpening;
public double IntervalOpening
{
get { return (double)intervalOpening / 1000.0; }
set { intervalOpening = (int)(value * 1000); }
}
private int intervalClosing;
public double IntervalClosing
{
get { return (double)intervalClosing / 1000.0; }
set { intervalClosing = (int)(value * 1000); }
}
private int intervalWait;
public double IntervalWait
{
get { return (double)intervalWait / 1000.0; }
set { intervalWait = (int)(value * 1000); }
}
private void StateEvent(DoorHandler pEvent)
{
if (pEvent != null) pEvent();
}
public Door(Form pForm)
{
form = pForm;
timerOpen = new TimerElapsed(form);
timerClose = new TimerElapsed(form);
timerWait = new TimerElapsed(form);
timerOpen.eventStop += () => { OnTimeOpen(); };
timerClose.eventStop += () => { OnTimeClose(); };
timerWait.eventStop += () => { OnTimeWait(); };
}
/*private void OutRemains()
{
if (myTimer.Active)
labelRemains.Text = myTimer.Remains;
else
labelRemains.Text = "0.0";
}
public string Remains
{
get
{
TimeSpan delta = DateTime.Now - CurrentDateTime;
double dremains = timer.Interval - delta.TotalMilliseconds;
return (dremains / 1000).ToString("F1");
}
}*/
public void Reset()
{
SetEvent(eventClose);
}
public void Open()
{
timerOpen.Interval = intervalOpening;
timerOpen.Start();
SetEvent(eventOpening);
}
public void Close()
{
timerClose.Interval = intervalClosing;
timerClose.Start();
SetEvent(eventClosing);
}
public void Wait()
{
timerWait.Interval = intervalWait;
timerWait.Start();
SetEvent(eventWait);
}
private void SetEvent(DoorHandler pEvent)
{
if (pEvent != null) pEvent();
}
public void StopTimer()
{
SetEvent(eventStop);
}
}
} |
|