emre can

emre can

  • 1.7k
  • 29
  • 492

C# User Control Winform

Dec 22 2023 1:59 PM

In my C# project, there are 2 user Controls named OkullasmaControl and RibbonControl. There is also a main form called MainForm. I added MainPanel to MainForm and added RibbonControl to MainPanel. Then I added OkullasmaControl(User Control) into RibbonControl. However, I could not manage to open a new WinForm with Button1 on OkullasmaControl. Please can anyone help me with this? My Project Codes are below.

RIBBON CONTROL CODS

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MEIS
{
    public partial class RibbonControl : UserControl
    {
        public RibbonControl()
        {
            InitializeComponent();
        }
        private void ButonSekme1_Click(object sender, EventArgs e)
        {
            ButonSekme1.ChangeColorMouseHC = false;
            ButonSekme2.ChangeColorMouseHC = true;
            ButonSekme3.ChangeColorMouseHC = true;
            ButonSekme4.ChangeColorMouseHC = true;
            ButonSekme5.ChangeColorMouseHC = true;
            ButonSekme6.ChangeColorMouseHC = true;
            ButonSekme7.ChangeColorMouseHC = true;
            ButonSekme1.ForeColor = Color.FromArgb(200, 200, 200);
            ButonSekme2.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme3.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme4.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme5.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme6.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme7.ForeColor = Color.FromArgb(233, 233, 233);          
            ButonSekme1.ShowBorder = true;
            ButonSekme2.ShowBorder = false;
            ButonSekme3.ShowBorder = false;
            ButonSekme4.ShowBorder = false;
            ButonSekme5.ShowBorder = false;
            ButonSekme6.ShowBorder = false;
            ButonSekme7.ShowBorder = false;
            tabPanel.SkipXRange = new Point(ButonSekme1.Location.X, ButonSekme1.Location.X + ButonSekme1.Width);
        }
        private void ButonSekme2_Click(object sender, EventArgs e)
        {
            ButonSekme2.ChangeColorMouseHC = false;
            ButonSekme1.ChangeColorMouseHC = true;
            ButonSekme3.ChangeColorMouseHC = true;
            ButonSekme4.ChangeColorMouseHC = true;
            ButonSekme5.ChangeColorMouseHC = true;
            ButonSekme6.ChangeColorMouseHC = true;
            ButonSekme7.ChangeColorMouseHC = true;
            ButonSekme2.ForeColor = Color.FromArgb(200, 200, 200);
            ButonSekme1.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme3.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme4.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme5.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme6.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme7.ForeColor = Color.FromArgb(233, 233, 233);     
            ButonSekme2.ShowBorder = true;
            ButonSekme1.ShowBorder = false;
            ButonSekme3.ShowBorder = false;
            ButonSekme4.ShowBorder = false;
            ButonSekme5.ShowBorder = false;
            ButonSekme6.ShowBorder = false;
            ButonSekme7.ShowBorder = false;
            tabPanel.SkipXRange = new Point(ButonSekme2.Location.X,ButonSekme2.Location.X + ButonSekme2.Width);
        }
        private void ButonSekme3_Click(object sender, EventArgs e)
        {
            ButonSekme3.ChangeColorMouseHC = false;
            ButonSekme1.ChangeColorMouseHC = true;
            ButonSekme2.ChangeColorMouseHC = true;
            ButonSekme4.ChangeColorMouseHC = true;
            ButonSekme5.ChangeColorMouseHC = true;
            ButonSekme6.ChangeColorMouseHC = true;
            ButonSekme7.ChangeColorMouseHC = true;
            ButonSekme3.ForeColor = Color.FromArgb(200, 200, 200);          
            ButonSekme1.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme2.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme4.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme5.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme6.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme7.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme3.ShowBorder = true;
            ButonSekme1.ShowBorder = false;
            ButonSekme2.ShowBorder = false;
            ButonSekme4.ShowBorder = false;
            ButonSekme5.ShowBorder = false;
            ButonSekme6.ShowBorder = false;
            ButonSekme7.ShowBorder = false;
            tabPanel.SkipXRange = new Point(ButonSekme3.Location.X, ButonSekme3.Location.X + ButonSekme3.Width);
        }
        private void ButonSekme4_Click(object sender, EventArgs e)
        {
            ButonSekme4.ChangeColorMouseHC = false;
            ButonSekme1.ChangeColorMouseHC = true;
            ButonSekme2.ChangeColorMouseHC = true;
            ButonSekme3.ChangeColorMouseHC = true;
            ButonSekme5.ChangeColorMouseHC = true;
            ButonSekme6.ChangeColorMouseHC = true;
            ButonSekme7.ChangeColorMouseHC = true;
            ButonSekme4.ForeColor = Color.FromArgb(200, 200, 200);
            ButonSekme1.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme2.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme3.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme5.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme6.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme7.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme4.ShowBorder = true;
            ButonSekme1.ShowBorder = false;
            ButonSekme2.ShowBorder = false;
            ButonSekme3.ShowBorder = false;
            ButonSekme5.ShowBorder = false;
            ButonSekme6.ShowBorder = false;
            ButonSekme7.ShowBorder = false;
            tabPanel.SkipXRange = new Point(ButonSekme4.Location.X, ButonSekme4.Location.X + ButonSekme4.Width);
        }
        private void ButonSekme5_Click(object sender, EventArgs e)
        {
            ButonSekme5.ChangeColorMouseHC = false;
            ButonSekme1.ChangeColorMouseHC = true;
            ButonSekme2.ChangeColorMouseHC = true;
            ButonSekme3.ChangeColorMouseHC = true;
            ButonSekme4.ChangeColorMouseHC = true;
            ButonSekme6.ChangeColorMouseHC = true;
            ButonSekme7.ChangeColorMouseHC = true;
            ButonSekme5.ForeColor = Color.FromArgb(200, 200, 200);          
            ButonSekme1.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme2.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme3.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme4.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme6.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme7.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme5.ShowBorder = true;
            ButonSekme1.ShowBorder = false;
            ButonSekme2.ShowBorder = false;
            ButonSekme3.ShowBorder = false;
            ButonSekme4.ShowBorder = false;
            ButonSekme6.ShowBorder = false;
            ButonSekme7.ShowBorder = false;
            tabPanel.SkipXRange = new Point(ButonSekme5.Location.X, ButonSekme5.Location.X + ButonSekme5.Width);
        }
        private void ButonSekme6_Click(object sender, EventArgs e)
        {
            ButonSekme6.ChangeColorMouseHC = false;
            ButonSekme1.ChangeColorMouseHC = true;
            ButonSekme2.ChangeColorMouseHC = true;
            ButonSekme3.ChangeColorMouseHC = true;
            ButonSekme4.ChangeColorMouseHC = true;
            ButonSekme5.ChangeColorMouseHC = true;
            ButonSekme7.ChangeColorMouseHC = true;
            ButonSekme6.ForeColor = Color.FromArgb(200, 200, 200);         
            ButonSekme1.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme2.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme3.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme4.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme5.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme7.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme6.ShowBorder = true;         
            ButonSekme1.ShowBorder = false;
            ButonSekme2.ShowBorder = false;
            ButonSekme3.ShowBorder = false;
            ButonSekme4.ShowBorder = false;
            ButonSekme5.ShowBorder = false;
            ButonSekme7.ShowBorder = false;
            tabPanel.SkipXRange = new Point(ButonSekme6.Location.X,  ButonSekme6.Location.X + ButonSekme6.Width);
        }
        private void ButonSekme7_Click(object sender, EventArgs e)
        {
            ButonSekme7.ChangeColorMouseHC = false;
            ButonSekme1.ChangeColorMouseHC = true;
            ButonSekme2.ChangeColorMouseHC = true;
            ButonSekme3.ChangeColorMouseHC = true;
            ButonSekme4.ChangeColorMouseHC = true;
            ButonSekme5.ChangeColorMouseHC = true;
            ButonSekme6.ChangeColorMouseHC = true;
            ButonSekme7.ForeColor = Color.FromArgb(200, 200, 200);
            ButonSekme1.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme2.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme3.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme4.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme5.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme6.ForeColor = Color.FromArgb(233, 233, 233);
            ButonSekme7.ShowBorder = true;
            ButonSekme1.ShowBorder = false;
            ButonSekme2.ShowBorder = false;
            ButonSekme3.ShowBorder = false;
            ButonSekme4.ShowBorder = false;
            ButonSekme5.ShowBorder = false;
            ButonSekme6.ShowBorder = false;
            tabPanel.SkipXRange = new Point(ButonSekme7.Location.X,    ButonSekme7.Location.X + ButonSekme7.Width);
        }
        private void WordabControl_Load(object sender, EventArgs e)
        {
            ButonSekme1_Click(sender, e);
        }
    }
}

 

MAINFORM CODS

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MEIS
{
    public partial class MainForm : Form
    {
        OkullasmaControl okullasmaCtrl = new OkullasmaControl();
        public MainForm()
        {
            InitializeComponent();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            FormYazisi.Location = new Point((TopPanel.Width / 2) - FormYazisi.Text.Length * 2, 9);
            wordTabControl.DosyaButton.Click += new EventHandler(this.DosyaButton_Click);
            wordTabControl.OkullasmaButton.Click += new EventHandler(this.OkullasmaButton_Click);
            wordTabControl.CagButton.Click += new EventHandler(this.CagButton_Click);
            wordTabControl.IstatistiklerButton.Click += new EventHandler(this.Istatistikler_Click);
            wordTabControl.MezunButton.Click += new EventHandler(this.MezunButton_Click);
            wordTabControl.TasimaliButton.Click += new EventHandler(this.TasimaliButton_Click);
            wordTabControl.BinaButton.Click += new EventHandler(this.BinaButton_Click);
            wordTabControl.PansiyonButton.Click += new EventHandler(this.PansiyonButton_Click);
            OkullasmaButton_Click(sender, e);
        }
        enum CTRLTypes
        {
            None,
            Sekme1Type,
            Sekme2Type,
            Sekme3Type,
            Sekme4Type,
            Sekme5Type,
            Sekme6Type,
            SekmeType
        }
        static CTRLTypes addedControlType = CTRLTypes.None;
        private void Sekme1Button_Click(object sender, EventArgs e)
        {
            if (addedControlType != CTRLTypes.Sekme1Type)
            {
                wordTabControl.tabPanel.Controls.Clear();
                OkullasmaControl okullasmaCtrl = new OkullasmaControl();
                okullasmaCtrl.Location = new Point(1, 1);
                okullasmaCtrl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
                wordTabControl.tabPanel.Controls.Add(okullasmaCtrl);
                addedControlType = CTRLTypes. Sekme1Type;
            }
        }
    }
}

screenshots


Answers (3)