基于服务调用动态创建winform控件

时间:2018-01-05 15:07:40

标签: c# winforms

我最初有一个带有多个选项的单选按钮。在选择中,我调用一个Web服务,它为我提供了一个问题列表和要为每个问题创建的组件类型(单选按钮,文本框,下拉列表等)。那么,如何根据服务响应动态创建.net winform控件呢?

示例服务响应:

{
 question: 'Did you verify your account?',
 Control: 'Radio Button',
 Options: { 'Yes','No'}
}

2 个答案:

答案 0 :(得分:0)

以下是一些如何以编程方式添加控件的示例。然后你只需要解析Web服务并基于数据添加控件

        Label qs_label = new Label();
        qs_label.Font = new Font("Arial", 12F, FontStyle.Regular);
        qs_label.Size = new Size(150, 18);
        qs_label.Text = item.ToString();
        this.selectionPanel.Controls.Add(qs_label);

        Label m_label = new Label();
        m_label.Font = new Font("Arial", 12F, FontStyle.Regular);
        m_label.Size = new Size(150, 18);
        m_label.Text = "Maturity standard";
        this.selectionPanel.Controls.Add(m_label);

        Label c_label = new Label();
        c_label.Font = new Font("Arial", 12F, FontStyle.Regular);
        c_label.Size = new Size(170, 18);
        c_label.Text = "Compliance standard";
        this.selectionPanel.Controls.Add(c_label);

        ComboBox m_input = new ComboBox();
        m_input.Font = new Font("Arial", 12F, FontStyle.Regular);
        m_input.Size = new Size(200, 22);
        m_input.DropDownStyle = ComboBoxStyle.DropDownList;
        this.selectionPanel.Controls.Add(m_input);

        ComboBox c_input = new ComboBox();
        c_input.Font = new Font("Arial", 12F, FontStyle.Regular);
        c_input.Size = new Size(200, 22);
        c_input.DropDownStyle = ComboBoxStyle.DropDownList;
        this.selectionPanel.Controls.Add(c_input);

答案 1 :(得分:0)

<。>以.cs文件格式

this.Controls.Add(//Control you want to add);

你必须手动初始化它们(或以某种方式隐藏它们)。

其他解决方案是让它们不可见,并根据呼叫响应为每个解决方案显示真/假。

相关问题