Question How to implement control adding on form

TATARPRO

New member
Joined
Jul 18, 2018
Messages
1
Programming Experience
1-3
I created a custom control called 'Option', now I want to use this 'Option' control which I created in another control called 'Question'. I created a button on the
question control so that whenever I click on it, a new instance of the 'Option' control will be created and added to the interface.
I'm having problem adding the control on the form whenever the button is clicked; though the control adds successfully but i have to do some extra dirty work which
I see not appropriate. Whenever I click the button, I have to add the instantiate the control and add it to the list of options which is a property of the 'Question'
control and then call 'Question.Controls.Add(NewOption)' to display the Option control on the interface. Prior to how .Net does its own, whenever you call
this.Controls.Add(NewButton), it adds a button to the interface automatically and the form knows all its controls. How can I achieve this?
C#:
 public partial class Question : UserControl
    {
        public int QuestionNumber { get; set; }
        public string QuestionString { get; set; }
        public int NumberOfOptions { get; set; }//this var keeps the number of questions in the control------another setback
        public Dictionary<Char, Option> Options { get; set; }        
        public bool IsWithImage { get; set; }
        public Image QuestionImage { get; set; }
        public bool IsFirstQuestion { get; set; }
 private void Question_Load(object sender, EventArgs e)
        {
          
            this.Options = new Dictionary<char, Option>();
            while (NumberOfOptions < 4)
            {
                AddOption();//Add four options as default
            }
        }


 public void AddOption()
        {
            Option NewOption = new Option();//create new option
            string[] OptionChars = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };//array of option letters


            if (this.NumberOfOptions != 0)
            {
                //there is/are options already in the option list
                NewOption.SetOptionLetter(OptionChars[this.NumberOfOptions]);//set the option letter depending on the number of available options
                //NewOption.Name = "Question" + Options.Count + 1;
                //this.Options.Add(NewOption.OptionLetter, NewOption);//add the option to the Options list----this one also is giving me problem adding to the dictionary
		//it throws a nullObjectReference
                NewOption.Location = GetLocation();//set the location of the option
                this.grpOptions.Controls.Add(NewOption);//display the control on the interface
                this.NumberOfOptions++;//increase the number of options
            }
            else
            {
                //there are no options in the option list
                NewOption.SetOptionLetter(OptionChars[0]);//set the option letter to A
               // NewOption.Name = "Question" + Options.Count + 1;
                this.Options.Add(NewOption.OptionLetter, NewOption);//add the option to the Options list
                NewOption.Location = GetLocation();//set the location of the option
                this.grpOptions.Controls.Add(NewOption);//display the control on the interface
                this.NumberOfOptions++;//increase the number of options
            }
        }
}
 
Back
Top Bottom