Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

c# - 如何从C#中动态创建的用户控件中检索数据(How to retrieve data from dynamically created user control in C#)

I have a user control which has text boxes and date picker.

(我有一个带有文本框和日期选择器的用户控件。)

When the form load, Dynamically generate the user control and display in the form.

(加载表单后,动态生成用户控件并在表单中显示。)

(EventComponent (user control) image ).

((EventComponent(用户控件) 图片 )。)

I want to get each of these user control values when the user clicks the save button.

(当用户单击“保存”按钮时,我想获取每个这些用户控件值。)

namespace WeekNote.Forms
{
public partial class InsertEventForm : Form
{
    private int numberOfEvent;

    public InsertEventForm(int numberOfEvent)
    {
        InitializeComponent();
        this.numberOfEvent = numberOfEvent;
    }

    private void InsertEventForm_Load(object sender, EventArgs e)
    {

        EventComponent[] eventComponents = new EventComponent[(int)this.numberOfEvent];
        int i = 0;

        Button backBTN = new Button();
        backBTN.Text = "Back";
        backBTN.Location = new Point(0, 10);
        backBTN.Size = new Size(50, 30);
        this.Controls.Add(backBTN);

    // generate user control dynamically here

        for ( i = 0; i < eventComponents.Length; i++)
        {
            eventComponents[i] = new EventComponent();
            System.Drawing.Point pLabel = new System.Drawing.Point(36, 50 + i * 280);
            eventComponents[i].Location = pLabel;
            eventComponents[i].Size = new System.Drawing.Size(358, 259);
            this.Controls.Add(eventComponents[i]);
        }

        Button saveBTN = new Button();
        saveBTN.Text = "Save";
        saveBTN.Location = new Point(200, 50 + i * 280);
        saveBTN.Size = new Size(100, 50);
        this.Controls.Add(saveBTN);
        saveBTN.MouseEnter += new System.EventHandler(this.SaveBTN_click);
    }

    private void SaveBTN_click(object sender, EventArgs e)
    {
     // need to retieve value from user control
    }

    }
}
  ask by Gayathra Madubashana translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...