1,105,331 Community Members

how to access child control of a user control

Member Avatar
jainendra.shah
Light Poster
44 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi frds,
I have created a user control with the Imagebox control, label and a checkedlistbox control.
then I want to access checkedlistbox control from my application form. But unable to find any properties or event who manage directly to checkedlistbox control.

Plz provide way how to access that control event on my form In C# .
Thanks in advance

Member Avatar
AmirBedair
Light Poster
26 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Dear

I' m created for you a user control and I added for it properites like this :

public PictureBox PicBox
        {
            get
            {
                return picBox1;
            }
            set
            {
                picBox1 = value;
            }
        }


        public Label LblText
        {
            get
            {
                return lbl1;
            }
            set
            {
                lbl1 = value;
            }
        }


        public CheckedListBox checkBoxList
        {
            get
            {
                return clb1;
            }
            set
            {
                clb1 = value;
            }
        }

as you see I made properties for all controls and every properety get and set the label m checkedlistbox, or Picturebox direct..

after rebuild the solution you will see the user control in toolbox, drag and drop it,
and you will find all the defined controls in user control properites w(PicBox, LblText, and checkBoxList).
so you can edit them from designer or from the code .

Sample code:

// UserCntrl.Designer.cs
//====================
namespace winUserCntrlToutrial
{
    partial class UserCntrl
    {
        /// <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.clb1 = new System.Windows.Forms.CheckedListBox();
            this.lbl1 = new System.Windows.Forms.Label();
            this.picBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.picBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // clb1
            // 
            this.clb1.FormattingEnabled = true;
            this.clb1.Items.AddRange(new object[] {
            "Item 1",
            "Item 2",
            "Item 3"});
            this.clb1.Location = new System.Drawing.Point(120, 42);
            this.clb1.Name = "clb1";
            this.clb1.Size = new System.Drawing.Size(101, 64);
            this.clb1.TabIndex = 1;
            // 
            // lbl1
            // 
            this.lbl1.AutoSize = true;
            this.lbl1.Location = new System.Drawing.Point(28, 16);
            this.lbl1.Name = "lbl1";
            this.lbl1.Size = new System.Drawing.Size(57, 13);
            this.lbl1.TabIndex = 2;
            this.lbl1.Text = "Text Label";
            // 
            // picBox1
            // 
            this.picBox1.Location = new System.Drawing.Point(12, 42);
            this.picBox1.Name = "picBox1";
            this.picBox1.Size = new System.Drawing.Size(93, 64);
            this.picBox1.TabIndex = 3;
            this.picBox1.TabStop = false;
            // 
            // UserCntrl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.picBox1);
            this.Controls.Add(this.lbl1);
            this.Controls.Add(this.clb1);
            this.Name = "UserCntrl";
            this.Size = new System.Drawing.Size(240, 119);
            ((System.ComponentModel.ISupportInitialize)(this.picBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.CheckedListBox clb1;
        private System.Windows.Forms.Label lbl1;
        private System.Windows.Forms.PictureBox picBox1;
    }
}
// UserCntrl.cs
//==========
using System;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace winUserCntrlToutrial
{
    public partial class UserCntrl : UserControl
    {
        public PictureBox PicBox
        {
            get
            {
                return picBox1;
            }
            set
            {
                picBox1 = value;
            }
        }


        public Label LblText
        {
            get
            {
                return lbl1;
            }
            set
            {
                lbl1 = value;
            }
        }


        public CheckedListBox checkBoxList
        {
            get
            {
                return clb1;
            }
            set
            {
                clb1 = value;
            }
        }

        public UserCntrl()
        {
            InitializeComponent();
        }
    }
}
// Form1.Designer.cs
//==========
namespace winUserCntrlToutrial
{
    partial class Form1
    {
        /// <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 Windows Form 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.userCntrl1 = new winUserCntrlToutrial.UserCntrl();
            this.SuspendLayout();
            // 
            // userCntrl1
            // 
            this.userCntrl1.Location = new System.Drawing.Point(28, 44);
            this.userCntrl1.Name = "userCntrl1";
            this.userCntrl1.Size = new System.Drawing.Size(240, 119);
            this.userCntrl1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.userCntrl1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private winUserCntrlToutrial.UserCntrl userCntrl1;
    }
}
// Form1.cs
//==========
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace winUserCntrlToutrial
{
    public partial class Form1 : Form
    {
        public Form1()
        {
 
            InitializeComponent();

            userCntrl1.checkBoxList.Items.Add("Item 4");
            userCntrl1.checkBoxList.SelectionMode = SelectionMode.One;

            userCntrl1.LblText.Text = "This a user control sample";

            userCntrl1.PicBox.BackColor = Color.Blue;
        }
    }
}
Member Avatar
jainendra.shah
Light Poster
44 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a Lot sir,
given code is working

Member Avatar
AmirBedair
Light Poster
26 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a Lot sir,
given code is working

your welcome at anytime to ask me..
pelase mark this thread as solved ;)

Question Answered as of 5 Years Ago by AmirBedair
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article