C#制作自己的按钮并传递参数

时间:2015-10-23 11:19:57

标签: c# winforms custom-controls

我们使用C#表单应用程序作为自动化项目的HMI。 执行此操作时,我们通常使用按钮单击事件。这些事件的布尔值从false更改为true。我的问题从这里开始。 有很多布尔值,如果我逐个添加按钮事件,我会花很多次。所以我想做自己的按钮组件。我将从工具箱中拖放我的按钮,并使用属性窗口将其与布尔值相关联。这样做之后,它会自动完成所有事情,而不是我。按钮设计对我来说并不重要。

示例:

//Define Variables
public class PLCVars
{
    public bool PLCVar1;
    public bool PLCVar2;
    public bool PLCVar3;
    public bool PLCVar4;
    public bool PLCVar5;
    public bool PLCVar6;
    public bool PLCVar7;
    public bool PLCVar8;
}

//(!)I have a PLCVars object defined as PLCVariables
//Button Events 
private void button_JogPLCVar1_MouseDown(object sender, MouseEventArgs e)
{
    PLCVariables.PLCVar1 = true;
}

private void button_JogPLCVar1_MouseUp(object sender, MouseEventArgs e
{
    PLCVariables.PLCVar1 = false;
}

如上例所示,我想将我的按钮组件与plc变量相关联。它将自动生成所有mouseup和mousedown事件,并更改plc变量的值。

在搜索时,我应该使用自定义控件。但我不知道与Custom Control组件相关的表单变量是posibble。如果有可能如何?

3 个答案:

答案 0 :(得分:1)

您可以通过将Panel放入Form中并动态创建Button控件并将事件附加到创建的控件来实现此目的。

这是一个行示例(您必须修改它)

public class PLCVars
{
    public bool PLCVar1;
    public bool PLCVar2;
    public bool PLCVar3;
    public bool PLCVar4;
    public bool PLCVar5;
    public bool PLCVar6;
    public bool PLCVar7;
    public bool PLCVar8;

    public bool[] BooleanVars()
    {
        return new bool[] { PLCVar1, PLCVar2, PLCVar3, PLCVar4, PLCVar5 };
    }
}

public void Method()
{
     PLCVars v = new PLCVars();

     panel1.Controls.Clear();

     for (int i = 1; i < 5; i++)
     {
         Button button = new Button();
         button.Name = "Button" + i;
         button.MouseUp += delegate 
         {
             v.BooleanVars()[i] = false;
         };
         button.MouseDown += delegate
         {
             v.BooleanVars()[i] = true;
         };

         panel1.Controls.Add(button);
     }
}

答案 1 :(得分:1)

更新后,我看到您要创建一些按钮并使用这些按钮管理某些对象的属性。这是更好的解决方案。

首先 - 您应该使用FlowLayoutPanel来托管您的按钮。按钮将动态定位。

第二 - 你不应该使用按钮。使用CheckBoxes按钮的外观。因为您需要管理布尔属性。

他们 - 您应该将PLCVars字段更改为属性。因为我们要使用数据绑定。它不适用于字段:

public class PLCVars
{
    public bool PLCVar1 { get; set; }
    public bool PLCVar2 { get; set; }
    public bool PLCVar3 { get; set; }
    public bool PLCVar4 { get; set; }
    public bool PLCVar5 { get; set; }
    public bool PLCVar6 { get; set; }
    public bool PLCVar7 { get; set; }
    public bool PLCVar8 { get; set; }
}

现在您不需要任何事件处理程序或自定义按钮。如果要创建用于控制PLCVariables对象属性的UI,只需向面板添加复选框并将其绑定到对象即可。易:

int numberOfVlcVariables = typeof(PLCVars).GetProperties()
      .Count(p => p.Name.StartsWith("PLCVar"));

for(int i = 1; i <= numberOfVlcVariables ; i++)
{
    CheckBox cb = new CheckBox();
    cb.Appearance = Appearance.Button;
    cb.Text = "PLCVar" + i;
    cb.DataBindings.Add("Checked", PLCVariables, "PLCVar" + i);
    flowLayoutPanel1.Controls.Add(cb);
}

创建并安排切换'按钮'。每个按钮控制PLCVariables对象的适当属性。即按下按钮时,表示属性设置为true。如果按钮已启用,则属性等于false

enter image description here

更简单但不太酷的选项 - 您只需将PropertyGrid控件添加到表单并为其指定PLCVariables对象:

propertyGrid1.SelectedObject = PLCVariables;

结果:

enter image description here

答案 2 :(得分:0)

无需自定义控件。将所有按钮订阅到这两个事件处理程序。您需要做的就是获取PLCVar#字段名称。您可以使用按钮的Tag属性。将字段名称分配给标记PLCVar1PLCVar2等。然后在事件处理程序中获取字段名称并通过反射分配值:

private void button_JogPLCVar_MouseDown(object sender, MouseEventArgs e)
{
    Button button = (Button)sender;
    SetPLCVariable((string)button.Tag, true);
}

private void button_JogPLCVar_MouseUp(object sender, MouseEventArgs e
{
    Button button = (Button)sender;
    SetPLCVariable((string)button.Tag, false);
}

private void SetPLCVariable(string fieldName, bool value)
{
    Type type = typeof(PLCVars);
    FieldInfo fieldInfo = type.GetField(fieldName);
    fieldInfo.SetValue(PLCVariables, true);
}

创建和订阅所有按钮

 panel1.Controls.Clear();

 for (int i = 1; i < 5; i++)
 {
     Button button = new Button();
     button.Name = "Button" + i;
     button.Tag = "PLCVar" + i;
     button.MouseUp += button_JogPLCVar_MouseUp;
     button.MouseDown += button_JogPLCVar_MouseDown;
     panel1.Controls.Add(button);
 }

注意:使用属性而不是公共字段会更好。对于属性,您需要使用type.GetProperty方法。使用FlowLayoutPanel也更好。