Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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
Posted
Comments
BillWoodruff 1-Nov-15 9:17am    
What public fields, properties, or methods have you exposed in your UserControl ?

"I want to join the text written in rich textbox to my string" ... what does this mean ? What method do you use now to detect the user selecting, or entering, an answer ?

Start by breaking it up a little:
C#
for (int i = 0; i < content.Value; i++)
{
         Questions q = new Questions());
         q.Enumeration = i + 1;
         flowLayoutPanel1.Controls.Add(q);
}
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:
 
Share this answer
 
Comments
PeMaCN 1-Nov-15 8:59am    
can you please be more precise how to add enumeration property and how to use getter
i'm relatively new in c#
OriginalGriff 1-Nov-15 9:29am    
Do you know how to add a property to a class?
One strategy is to create an overloaded constructor for your 'Questions UserControl (but, do not delete the default Constructor !).
C#
// 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();
}
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:
C#
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);}
}
There are other good reasons for using Properties, rather than Fields, to expose members of a Class to outside "consumers:" enforce encapsulation, etc.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900