I m attempting to make a C# from program that is a simple box with four buttons: left, right, up, and down. then when a button is initially pressed(but not let go) it will send a a serial line. then when the button is unclicked(user lets go of left mouse button) i want to send another serial command.
问题是,每一条<代码>都避免了OnClick...(标注器,系统)。 活动Args e)与他人分离,系列港口“港口”在整个方案中都有工作。 i 主要用于序列用途:
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();
i 每一次都希望开放和关闭序列港,因为港口可能过于接近(按时间计算),可能会造成“航空港正在使用”问题,或造成发送序列线的滞后。
.。
法典:
public class EAS_ControlForm : System.Windows.Forms.Form
{
private Button Up4;
private Button Down3;
private Button Left2;
private Button Right1;
public EAS_ControlForm()
{
Text = "Etch-a-Sketch Control";
Down3 = new Button ();
Up4 = new Button ();
Left2 = new Button ();
Right1 = new Button ();
Down3.Text = "Down";
Down3.Name = "Down3";
Down3.Size = new System.Drawing.Size (72, 30);
Down3.Location = new System.Drawing.Point ((ClientRectangle.Width - Down3.Size.Width) / 2, ClientRectangle.Height - 10);
Controls.AddRange(new System.Windows.Forms.Control[] {this.Down3});
Down3.Click += new System.EventHandler(OnClickDown3);
Up4.Text = "Up";
///...button up4 stuff here like down 3 above.
Left2.Text = "Left";
///...button left2 stuff here like down 3 above.
Right1.Text = "Right";
///...button right1 stuff here like down 3 above.
}
static public void Main()
{
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();
Application.Run(new EAS_ControlForm());
}
void OnClickDown3 (object sender, System.EventArgs e)
{
port.Write("<3,100>");
}
void OnClickUp4 (object sender, System.EventArgs e)
{
port.Write("<4,100>"); //error here because of port initialization not in same code
}
void OnClickLeft2 (object sender, System.EventArgs e)
{
port.Write("<2,100>");
}
void OnClickRight1 (object sender, System.EventArgs e)
{
port.Write("<1,100>");
}
}
}