Jump to content
  • Advertisement
Sign in to follow this  
Hurp

C# and User Controls

This topic is 3757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a user control (MainUserControl) and I was it to have a list of user controls in it. I want to do this because I want to be able to change the controls in the list during run time. I made a list of UserControl's, however, how do I render the user controls (that are the children)? Here is what I have been working with (TitleSelection is a user control already made).
        List<UserControl> m_mChildren = new List<UserControl>();
        public List<UserControl> Children
        {
            get { return m_mChildren ; }
            set { m_mChildren = value; }
        }

        public OptionsContainer()
        {
            InitializeComponent();

            Children.Add(new TitleSelection());
            Children[0].Location = new Point(0, 0);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            for (int i = 0; i < Children.Count; ++i)
            {    
                Children.Refresh();
                Children.Invalidate();
            }
            base.OnPaint(e);
        }

Share this post


Link to post
Share on other sites
Advertisement
Do not override OnPaint unless you are doing an owner-draw control.

UserControls are just like regular controls. The have a property called Controls. Add your sub-user controls to that. They will be taken care of.

this.Controls.Add(ChildControl);

theTroll

Share this post


Link to post
Share on other sites
I tried that, however, I had a problem that hopefully you know the solution to.

On the MainUserControl I had added a prebuilt user control(AddedUserControlOne) to it. Inside of AddedUserControlOne there is also a user control added (AddedUserControlTwo). The problem is that AddedUserControlTwo when I go to changed its location, it moves its parent. Do you happen to know why? This is the code I use.


Controls.Add(new General.Location());
Controls[0].Location = new Point(0, 10);

Share this post


Link to post
Share on other sites
Yes because you really added it to the main user control

Controls[0].Controls[0].Location is what you want to use.

But the reality is you really don't want to you indexes.

You want to use the control name. That allows you to access the Controls using a name and not an index.

AddedUserControlOne.Name = "AddedUserControlOne";
Controls.Add(AddedUserControlOne);

AddedUserControlTwo.Name = "AddedUserControlTwo";
AddedUserControlOne.Controls.Add(AddedUserControlTwo);

then you can access the controls by usercontrol temp = Controls["AddedUserControlOne"];

Now if you want to find a control even if it is a child of a child of a child.

Controls[] foundControls = Controls.Find("TheNameOfTheControl", true);

Hope this helps.
theTroll

Share this post


Link to post
Share on other sites
I still have the same problem the wrong user control moving. In the main user control I do this

Controls.Add(new TitleSelection());
Controls[0].Controls.Add(new General.AnotherForm());
Controls[0].Controls[0].Location = new Point(0, 10);

Howver, the Controls[0] location is really the one moving instead of Controls[0].Controls[0] location. Is this really what I want to be doing?

Share this post


Link to post
Share on other sites
As I posted above for the most part you really don't want to be using indexes because it is a pain to keep track of and also you are never sure which control you are getting. The easiest is to name them and use the find command. That way you can get a reference to even deeply nested controls and move the one you want.

I just tested my way again to make sure it works and it works great.

Good luck, if you have any more questions, please ask.

theTroll

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!