如何动态添加用户控件,然后从中获取值 [英] How do I add user control dynamically and then get value from it

查看:41
本文介绍了如何动态添加用户控件,然后从中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了名为Questions的用户控件,其中包含一个标签和一个富文本框。

我将此用户控件添加到我的表单x(用户插入的数字) - 次。



I made user control named Questions which contains one label and one rich textbox.
I'm adding this user control to my form x(the number that user inserts) - times.

content.Value = numberOfQuestions;

for (int i = 0; i < content.Value; i++)
{
         flowLayoutPanel1.Controls.Add(new Questions());
}





现在我想在每个用户控件的标签中设置枚举(每个问题的编号)想要将富文本框中写的文字加入我的字符串。

我该怎么做?

请帮忙。

谢谢



Now I want to set the enumeration (number of each question) in the label of each user control and I want to join the text written in rich textbox to my string.
How can I do that?
Please help.
Thank you

推荐答案

首先分解一下:

Start by breaking it up a little:
for (int i = 0; i < content.Value; i++)
{
         Questions q = new Questions());
         q.Enumeration = i + 1;
         flowLayoutPanel1.Controls.Add(q);
}

将Enumeration属性添加到控件,并在那里设置值。使用Getter从标签中检索值并解析它。



至于想要将富文本框中写入的文本加入我的字符串,我有根本不知道你在谈论什么......你需要澄清一点,记住我们看不到你的屏幕,访问你的硬盘或读你的想法! :laugh:

Add the Enumeration property to the control, and set the value there. Use the Getter to retrieve the value from the label and parse it.

As far as "want to join the text written in rich textbox to my string" goes I have no idea at all what you are talking about...you will need to clarify that, remembering that we can't see your screen, access your HDD, or read your mind! :laugh:


一种策略是为'Questions UserControl创建一个重载的构造函数(但是,不要删除默认的构造函数!)。
One strategy is to create an overloaded constructor for your 'Questions UserControl (but, do not delete the default Constructor !).
// in the UserControl
// assuming RichTextBox 'rtbQuestion holds the question text
// assuming RichTextBox 'rtbAnswer is where the user types an answer
// assuming Label 'lblQuestionId holds the integer Id

// the use of : this() here insures the default initializer is executed
// and any internal Controls are created
public Questions(Panel container, int id, string questiontext) : this()
{
    lblQuestionId.Text = id.ToString();
    rtbQuestion.Text = questiontext;
    container.Controls.Add(this);
    this.Dock = DockStyle.Top;
    this.BringToFront();
}

请注意,有些人认为在WinForms中使用带参数的重载构造函数是个坏主意:[ ^ ]。



上述讨论当然不包括如何你访问用户的回复,因为我们不知道如何/在哪里/那是什么。



如果你想更多的标准路线实现Properties而不是创建重载的构造函数,和/或您需要在UserControl中控件内部状态的更多运行时可用性(set,get,values):



然后,请确保您已公开(通过声明它们公开)字段,属性,方法,您希望UserControl的使用者(Form或ContainerControl,UserControl位于/托管)具有访问权限至。例如:

Be aware that some think it's a bad idea in WinForms to use overloaded Constructors with parameters: [^].

The above discussion, of course, does not cover how you access the user's response, since we don't know the how/where/what of that, yet.

If you want to go the more "standard route" of implementing Properties rather than creating an overloaded Constructor, and/or you need more run-time availability (set, get, values) of the internal state of your Controls in the UserControl:

Then, be sure you have exposed (by declaring them Public) Fields, Properties, Methods, you want a "consumer" of the UserControl (the Form, or ContainerControl, the UserControl is sited/hosted in) to have access to. For Example:

public string QuestionText
{
    set { rtbQuestion.Text = value; }
    get { return rtbQuestion.Text; }
}

// depending on your app design, you may not
// need a 'setter here ?
public string AnswerText
{
    get { return rtbAnswer.Text; }
}

public int QuestionId
{
    set { lblQuestionId.Text = value.ToString(); }
    get { return Convert.ToInt32(lblQuestionId.Text);}
}

使用Properties而不是Fields,还有其他充分的理由将类的成员暴露给consumer:强制封装等。

There are other good reasons for using Properties, rather than Fields, to expose members of a Class to outside "consumers:" enforce encapsulation, etc.


这篇关于如何动态添加用户控件,然后从中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆