Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:45:38+00:00 2026-06-11T13:45:38+00:00

In this user control the designer adds items to this control on the form

  • 0

In this user control the designer adds items to this control on the form and it dynamically places them on the form. Unfortunately once these controls are shown on the form in designer I can then grab one and pull it right outside of the custom control.

How do I prevent this?

EDIT

Please note that I am not looking for a solution or alternative to handling radio buttons, that is just the control of choice in this scenario. If it helps insert any type of control you want here (Button, Label, TextBox, CustomControl1), this problem is about handling a property with a collection/array type. The bottom line is that the user shouldn’t be able to grab any of these controls in designer where an instance of this control has been defined and move it elsewhere. Just like a DataGridView or ListView albeit different types of objects, the user must use properties to change the contents of these controls. That is the beharior I am looking for.

RBLTest.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WFCL_Library
{
    public partial class RBLTest : UserControl
    {
        private RadioButton[] _items;

        private int leftSpacing = 100;
        private int topSpacing = 25;

        public RBLTest()
        {
            InitializeComponent();
        }

        private void RadioButtonList_Load(object sender, EventArgs e)
        {
            int curLeftPos = 0;
            int curTopPos = 0;
            foreach (RadioButton rb in _items)
            {
                rb.Location = new Point(curLeftPos, curTopPos);
                rb.Size = new Size(85, 17);

                curLeftPos += leftSpacing;

                if (curLeftPos > this.Width)
                {
                    curLeftPos = 0;
                    curTopPos += topSpacing;
                }

                this.Controls.Add(rb);                
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RadioButton[] Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
            }
        }
    }       
}

RBLTest.Designer.cs

namespace WFCL_Library
{
    partial class RBLTest
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // RBLTest
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "RBLTest";
            this.Size = new System.Drawing.Size(407, 44);
            this.Load += new System.EventHandler(this.RadioButtonList_Load);
            this.ResumeLayout(false);

        }

        #endregion

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:45:40+00:00Added an answer on June 11, 2026 at 1:45 pm

    Well, it depends on how you want to do it. First off, I’m not a big fan of allowing direct access to controls on my user controls. For this reason, I make sure none of the controls are publically accessable. What I do is make everything private (or sometimes protected since it’s nice for derived classes to have some flexibility) then I create properties to access the controls’ properties that must be accessible and events to be handled by parent containers.

    If all that really doesn’t matter to you, then I believe the property you are looking for is the Control.Locked property. In your code above once you’ve added the dynamic control to the Controls collection, set the Locked to true. So:

                this.Controls.Add(rb);
                rb.Locked = true;
    


    Try something like this:

        public class RadioButtonProperties
        {
            private RadioButton _realtedRadioButton = new RadioButton();
    
            public RadioButtonProperties(RadioButton radio)
            {
                _realtedRadioButton = radio;
    
                Name = radio.Name;
                Checked = radio.Checked;
                //add other properties
            }
    
            public string Name
            {
                get
                {
                    //logic to to handle if checkbox is null here
    
                    return _realtedRadioButton.Name;
                }
    
                set
                {
                    //logic to to handle if checkbox is null here
    
                    _realtedRadioButton.Name = value;
                }
            }
    
            public bool Checked
            {
                get
                {
                    //logic to to handle if checkbox is null here
    
                    return _realtedRadioButton.Checked;
                }
    
                set
                {
                    //logic to to handle if checkbox is null here
    
                    _realtedRadioButton.Checked = value;
                }
            }
        }
    

    Now in your custom control, instead of giving public access to your entire radiobox array, you give access to a representation of the array, exposing only the radiobox properties that you want. For example:

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]  
        public List<RadioButtonProperties> Items { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are dozens of user control in our project. All these controls load into
When you create a form or a user control, the WinForms designer generates a
this my code in my user control's designer file <%@ Control Language=C# AutoEventWireup=true CodeBehind=ucImageList.ascx.cs
I have this User Control XAML: <Window x:Class=MyProj.Dialog xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:shell=http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell mc:Ignorable=d>
I have a user control let say UC1 . This user control have viewmodel
I have an ASP.NET user control (.ascx file). In this user control I want
I have created a UserControl in WPF. This user control consists of a border,
I have a user control with a div like this: <div runat=server id=pnlShippingMethods class=checkoutstep>
This is how my web page looks like: PosisionDataView is a web user control,
I just created a user control. This control also makes use of my static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.