From magician s blog
There will always be a fixed set of buttons and operation conventions in the design of the device Operation Panel, but it is very inefficient to write the same set of code for every project.
Most device operation buttons include start, pause, and stop buttons. These three buttons are also required on the software interface.
To disable or enable different button combinations. For example, when the software is started, the start button is enabled, the pause and stop buttons are disabled, and so on.
In general design, a Button is used, and some design uses ToolStripButton. to make it compatible with more controls, a generic type is used. The Code is as follows:
Public enum OperateState {Run, Pause, Stop };
Public class OperateStateRule <T> where T: ToolStripItem
{
Private T start;
Private T pause;
Private T stop;
Private OperateState state = OperateState. Stop;
Private OperateState State
{
Get {return state ;}
// Set {state = value ;}
}
Public OperateStateRule (T _ start, T _ pause, T _ stop)
{
Start = _ start;
Pause = _ pause;
Stop = _ stop;
Update (OperateState. Stop );
}
Public void Update (OperateState _ state)
{
State = _ state;
Try
{
Switch (state)
{
Case OperateState. Stop:
Start. Enabled = true;
Pause. Enabled = false;
Stop. Enabled = false;
Break;
Case OperateState. Run:
Start. Enabled = false;
Pause. Enabled = true;
Stop. Enabled = true;
Break;
Case OperateState. Pause:
Start. Enabled = true;
Pause. Enabled = false;
Stop. Enabled = true );
Break;
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
}
Call example:
ToolStripButton btnStart;
ToolStripButton btnPause;
ToolStripButton btnStop;
// Bind the corresponding button when generating the object
OperateStateRule <ToolStripButton> rule = new OperateStateRule <ToolStripButton> (btnStart, btnPause, btnStop );
// Update the status in the Click Event
Private void btnStart_Click (object sender, EventArgs e)
{
Rule. Update (OperateState. Run );
}
Private void btnPause_Click (object sender, EventArgs e)
{
Rule. Update (OperateState. Pause );
}
Private void btnStop_Click (object sender, EventArgs e)
{
Rule. Update (OperateState. Stop );
}
There is a where T: ToolStripItem statement in the class declaration. This is a c # type constraint. If this constraint is not added, the compiler reports an error and cannot find the Enabled attribute,
However, there is a problem that the buttons and ToolStripButton classes are not derived from the same base class, so they cannot be compatible with the two classes, because this constraint must be imposed to make c ++
Some generic practices are restricted.
There is still no better way to achieve the desired effect. The answer is yes, that is, reflection. See the following improved code example:
Public enum OperateState {Run, Pause, Stop };
Public class OperateStateRule <T>
{
Private T start;
Private T pause;
Private T stop;
Private OperateState state = OperateState. Stop;
Private OperateState State
{
Get {return state ;}
// Set {