Using UserControl in Windows Forms Application using C#

Last Reply 3 months ago By kalpesh

Posted 3 months ago

Add User Defined Control [Button] .Add property 'security Level' 

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 PMS_Pro_Win.Custom_Controls
{
    public partial class btn : System.Windows.Forms.Button
    {
        public btn()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            if (SecurityLevel > 0)
            {
                base.Enabled = false;
            }
            else
            {
                base.Enabled = true;
            }
        }

        private int level;
        [Category("Security Level"), Description("Enter Security Level")]

        public int SecurityLevel
        {
            get
            {
                return level;
            }
            set
            {
                level = value;
                Invalidate();
            }
        }
    }
}

How to set property give by varibale name which is declare in another Class file 

Posted 3 months ago Modified on 3 months ago

You just need to set its property by user defined control name which you used in your Form with the name of user control in your Form like below.

usercontrolaliesName.SecurityLevel = 1;
//or
usercontrolaliesName.SecurityLevel = 0;

Check the below Sample code for your reference.

User control : btn

FORM : Form1

Form1.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        btnSubmit.SecurityLevel = 1;
        //btnSubmit.SecurityLevel = 0;
    }
}

Screenshot

 

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html