2022년 4월 19일 화요일

Winform class-based dynamic UI creation(winform 클래스기반 동적 UI 생성)

 


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)











댓글 없음:

댓글 쓰기

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...