public void DynamicButton_Load(object sender, EventArgs e)
{
PLCDataButton(TargetClass 원하는클래스);
}
public void PLCDataButton<T>(T target)
{
List<string> fieldNames = new List<string>();
foreach (var prop in target.GetType().GetFields())
{
fieldNames.Add(prop.Name);
}
m_PLC_Data.Field_Write(m_PLC_Data, "posX",100);
Console.WriteLine(m_PLC_Data.Field_GetValue(m_PLC_Data, "posX"));
Control[] BTN = new Control[fieldNames.Count];
Control[] TB = new Control[fieldNames.Count];
for (int idx = 0; idx < fieldNames.Count; idx++)
{
BTN[idx] = new Button();
BTN[idx].Name = "btn_"+fieldNames[idx];
BTN[idx].Parent = this;
BTN[idx].Size = new Size(90, 30);
TB[idx] = new TextBox();
TB[idx].Name = "tb_"+fieldNames[idx];
TB[idx].Parent = this;
TB[idx].Size = new Size(90, 30);
BTN[idx].Text = fieldNames[idx];
if (m_PLC_Data.Field_GetValue(m_PLC_Data, fieldNames[idx]) == null)
TB[idx].Text = "0";
else
TB[idx].Text = m_PLC_Data.Field_GetValue(m_PLC_Data, fieldNames[idx]).ToString();
//BTN[idx].Click += TB_PLC_Change;
TB[idx].TextChanged += TB_PLC_Change;
WIDTH += 80;
HEIGHT += 30;
///[flp_btns] 이것은 따로 만들어주는게 맘이편함
///[flp_btns] I like to make this separately.
flp_btns.Controls.Add(BTN[idx]);
flp_btns.Controls.Add(TB[idx]);
}
this.Controls.Add(flp_btns);
}
특정 클래스에 있는 Field 부분만 UI로 만들어서
UI에서 변경하면 직접 변경되게 하는 코드(버튼은 의미없긴함)
By making only the Field part in a specific class as UI
Code that makes changes directly when changed in UI (buttons are meaningless)
댓글 없음:
댓글 쓰기