winform--穿梭框

新建一个用户控件:

 直接上代码:

  /*
     * 作者:pengyan zhang
     * 邮箱:3073507793@qq.com
     * 博客:https://www.cnblogs.com/zpy1993-09
     * 时间:2024-04-10 16:36
     */
    public partial class ShuttleFrameControl : UserControl
    {


        private Color lb_BackColor { get; set; } = Color.Transparent;
        private Color lb_FonceColor { get; set; } = Color.Black;
        private Color pic_CheckBackColor { get; set; } = Color.FromArgb(95, 184, 120);
        private Color pic_UnCheckBackColor { get; set; } = Color.White;

        private Image pic_Img { get; set; } = Properties.Resources.Check;
        private List<ShuttleFrameData> InitData=new List<ShuttleFrameData>();
     
        public ShuttleFrameControl()
        {
            InitializeComponent();
           
            Init();



        }
        private void Init()
        {
            Input_Left.Text = "关键字搜索";
            Input_Left.ForeColor = Color.DarkGray;
            Input_Right.Text = "关键字搜索";
            Input_Right.ForeColor = Color.DarkGray;
            this.pic_Left.Tag = false;
            this.pic_Right.Tag = false;
            this.L_RunLfet.Tag=false;
            this.L_RunRight.Tag = false;

            this.Flow_Left_Bottom.AutoScroll = true;
            this.Flow_Right_Bottom.AutoScroll = true;
            this.P_Contain.Paint += this_Paint;
            this.Flow_Left_Bottom.Paint += this_Paint;
            this.L_RunLfet.Paint += this_Paint;
            this.L_RunLfet.Click += ShuttleFrame_Click;
            this.L_RunLfet.MouseEnter += ShuttleFrame_MouseEnter;
            this.L_RunRight.Paint += this_Paint;
            this.L_RunRight.Click += ShuttleFrame_Click;
            this.L_RunRight.MouseEnter += ShuttleFrame_MouseEnter;
            this.pic_Left.Paint += pic_Paint;
            this.pic_Left.MouseEnter += ShuttleFrame_MouseEnter;
            this.pic_Right.Paint += pic_Paint;
            this.pic_Right.MouseEnter += ShuttleFrame_MouseEnter;
            this.pic_Left.Click += ShuttleFrame_Click;
            this.pic_Right.Click += ShuttleFrame_Click;
            this.L_Left.Click += ShuttleFrame_Click;
            this.L_Right.Click += ShuttleFrame_Click;
            this.L_Left.MouseEnter += ShuttleFrame_MouseEnter;
            this.L_Right.MouseEnter+= ShuttleFrame_MouseEnter;
            this.panel_Left.Paint += this_Paint;
            this.panel_Right.Paint += this_Paint;
            this.Input_Left.Enter += Input_Enter;
            this.Input_Left.Leave += Input_Leave;
            this.panel_Flow.Paint += this_Paint;
            this.Input_Right.Enter += Input_Enter;
            this.Input_Right.Leave += Input_Leave;
            ReSet(this.Flow_Right_Bottom,1);
            ReSet(this.Flow_Left_Bottom,1);
        }
        private void Input_Leave(object? sender, EventArgs e)
        {
            var control = sender as System.Windows.Forms.TextBox;
            if (control.Text == "")
            {
                control.Text = "关键字搜索";
                control.ForeColor = Color.DarkGray;
            }
        }
        private void Input_Enter(object? sender, EventArgs e)
        {
            var control = sender as System.Windows.Forms.TextBox;
            if (control.Text == "关键字搜索")
            {
                control.Text = "";
                control.ForeColor = Color.Black;
            }
        }
        private void ShuttleFrame_MouseEnter(object? sender, EventArgs e)
        {
            var control = sender as Control;
            control.Cursor = Cursors.Hand; ;
            switch (control.Name)
            {
                case "Itempic":
                    var data = control.Parent.Tag as ShuttleFrameData;
                    if (data.IsShield) control.Cursor = Cursors.No;
                    else if(control is Label) control.BackColor = Color.FromArgb(224, 224, 224);
                    break;
                case "L_RunLfet":
                case "L_RunRight":
                   if( (bool)control.Tag == false)control.Cursor = Cursors.No;
                    break;
            }
        }
        private void ShuttleFrame_Click(object? sender, EventArgs e)
        {
            var control = sender as Control;
            bool ischeck;
            switch (control.Name)
            {
                case "L_Left":
                case "pic_Left":
                     ischeck = !(bool)this.pic_Left.Tag;
                    this.pic_Left.Tag = ischeck;
                    DealAllCheck(this.Flow_Left_Bottom, ischeck);
                    break;
                case "L_Right":
                case "pic_Right":
                     ischeck = !(bool)this.pic_Right.Tag;
                    this.pic_Right.Tag = ischeck;
                    DealAllCheck(this.Flow_Right_Bottom, ischeck);
                    break;
                case "Itempic":
                    DealLeftORRight(control.Parent as Panel);
                    break;
                case "L_RunLfet":
                    if ((bool)control.Tag == false) return;
                     ReMoveItem(Flow_Left_Bottom, Flow_Right_Bottom);
                    break;
                case "L_RunRight":
                    if ((bool)control.Tag == false) return;
                    ReMoveItem(Flow_Right_Bottom, Flow_Left_Bottom);
                    break;
            }
            ReSet(this.Flow_Right_Bottom);
            ReSet(this.Flow_Left_Bottom);
        }
        private void ReMoveItem(FlowLayoutPanel flow1,FlowLayoutPanel flow2)
        {
            List<Panel> list = new List<Panel>();
            int count = 0;
            foreach (Panel item in flow1.Controls)
            {
                var result = item.Tag as ShuttleFrameData;
                if (!result.IsCheck && result.IsShield == false && item.Visible == true) count++;
                if (result.IsCheck && result.IsShield == false && item.Visible == true) list.Add(item);
               
            }
            list.ForEach(item =>
            {
                flow1.Controls.Remove(item);
                var result = item.Tag as ShuttleFrameData;
                result.IsLeftORRight = !result.IsLeftORRight;
                result.IsCheck = false;
                if(Input_Right.Text != "关键字搜索" && Input_Right.Text != ""&&Input_Left.Text != "关键字搜索"&&Input_Left.Text!="")item.Visible = false;
                var pic = item.Controls[0] as PictureBox;
                pic.BackColor = pic_UnCheckBackColor;
                pic.Image = null;
                item.Tag = result;
                flow2.Controls.Add(item);

            });
           
        }
        public void SetShuttleFrame(List<ShuttleFrameData> shuttleFrameDatas)
        {
            InitData = shuttleFrameDatas;
            shuttleFrameDatas.ForEach(m => { DealData(m); });
        }
        private void DealData(ShuttleFrameData shuttleFrameData)
        {
            Panel panel = new Panel();
            panel.Height = 25;
            panel.Tag = shuttleFrameData;
            PictureBox pictureBox = new PictureBox();
            pictureBox.Parent = panel;
            pictureBox.BackColor = pic_UnCheckBackColor;
            pictureBox.Image = null;
            pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
            pictureBox.Size = new Size(25, 25);
            pictureBox.Location = new Point(0, 0);
            pictureBox.Paint += pic_Paint;
            pictureBox.Name = "Itempic";
            pictureBox.Click += ShuttleFrame_Click;
            pictureBox.MouseEnter += ShuttleFrame_MouseEnter;
            Label label = new Label();
            label.Parent = panel;
            label.Name = "Itempic";
            label.BackColor = Color.White;
            if (shuttleFrameData.IsShield) label.ForeColor = Color.DarkGray;
            else label.ForeColor = Color.Black;
            label.Text = shuttleFrameData.Title;
            label.Size= new Size(150, 25);
            label.Location = new Point(25, 0);
            label.Click += ShuttleFrame_Click;
            label.MouseEnter += ShuttleFrame_MouseEnter;
            label.MouseLeave += Label_MouseLeave;
             if (shuttleFrameData.IsLeftORRight)
            {
                panel.Width = Flow_Left_Bottom.Width-35;
                Flow_Left_Bottom.Controls.Add(panel);
            }
            else
            {
                panel.Width = Flow_Right_Bottom.Width-30;
                Flow_Right_Bottom.Controls.Add(panel);
            }
        }
        private void Label_MouseLeave(object? sender, EventArgs e)
        {
            var label = sender as Label;
            label.BackColor = Color.White; ;
        }
        /// <summary>
        /// 当所有操作都执行完,检查穿梭框中是否有选中的,或者是否全部选中
        /// </summary>
        private void ReSet(FlowLayoutPanel flow,int type=0)
        {
           // bool IsAllCheck = flow.Name == "Flow_Left_Bottom" ? (bool)pic_Left.Tag : (bool)pic_Right.Tag;
            bool IsAllCheck = true;

            bool IsItemCheck = false;
            int count = 0;
            int Sumcount = 0;
            foreach (Panel item in flow.Controls)
            {
                var result = item.Tag as ShuttleFrameData;
                if (result.IsShield == false && item.Visible == true) count++;
                if (result.IsCheck == false && result.IsShield == false && item.Visible == true) IsAllCheck = false;
                if(result.IsCheck==true&&result.IsShield==false&&item.Visible==true)IsItemCheck = true;
              
            }
          if(type==1)IsAllCheck = false;
            if (flow.Name== "Flow_Left_Bottom")
            {
                if (IsAllCheck&&count>0)
                {
                    pic_Left.BackColor = pic_CheckBackColor;
                    pic_Left.Image = pic_Img;
                    pic_Left.SizeMode = PictureBoxSizeMode.CenterImage;
                    pic_Left.Tag = true;
                }
                else
                {
                    pic_Left.BackColor = pic_UnCheckBackColor;
                    pic_Left.Image = null;
                    pic_Left.Tag = false;
                }
                if (IsItemCheck)
                {
                    L_RunLfet.Tag = true;
                    L_RunLfet.BackColor = pic_CheckBackColor;
                    L_RunLfet.Image = Properties.Resources.Check_Right_White;

                }
                else
                {
                    L_RunLfet.Tag = false;
                    L_RunLfet.BackColor = pic_UnCheckBackColor;
                    L_RunLfet.Image = Properties.Resources.Check_Right_Grag;
                }
            }
            else
            {
                if (IsAllCheck && count > 0)
                {
                    pic_Right.BackColor = pic_CheckBackColor;
                    pic_Right.Image = pic_Img;
                    pic_Right.SizeMode = PictureBoxSizeMode.CenterImage;
                    pic_Right.Tag = true;
                }
                else
                {
                    pic_Right.BackColor = pic_UnCheckBackColor;
                    pic_Right.Image = null;
                    pic_Right.Tag = false;
                }
                if (IsItemCheck)
                {
                    L_RunRight.Tag = true;
                    L_RunRight.BackColor = pic_CheckBackColor;
                    L_RunRight.Image = Properties.Resources.Check_Left_White;

                }
                else
                {
                    L_RunRight.Tag = false;
                    L_RunRight.BackColor = pic_UnCheckBackColor;
                    L_RunRight.Image = Properties.Resources.Check_Left_Grag;

                }
            }
        }
        private void pic_Paint(object? sender, PaintEventArgs e)
        {
            Control control = sender as Control;
            ControlPaint.DrawBorder(e.Graphics,//获取进行绘制的图形
             new Rectangle(0, 0, control.Width, control.Height),//绘制控件所在工作区域
             Color.LimeGreen, 1, ButtonBorderStyle.Solid,//边框类型,选择实线边框,也有虚线等种类;下面这四个重复的传参是设置工作区上下左右边框的功能
             Color.LimeGreen, 1, ButtonBorderStyle.Solid,
             Color.LimeGreen, 1, ButtonBorderStyle.Solid,
             Color.LimeGreen, 1, ButtonBorderStyle.Solid);
        }
        private void this_Paint(object? sender, PaintEventArgs e)
        {
            Control control = sender as Control;
            ControlPaint.DrawBorder(e.Graphics,//获取进行绘制的图形
           new Rectangle(0, 0, control.Width, control.Height),//绘制控件所在工作区域
            Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid,//边框类型,选择实线边框,也有虚线等种类;下面这四个重复的传参是设置工作区上下左右边框的功能
            Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid,
            Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid,
            Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid);
        }

        protected override void OnResize(EventArgs ea)
        {
            //穿梭框的长和宽定死
            this.Height = 400;
            this.Width = 500;
            base.OnResize(ea);
        }
        private void Input_Left_TextChanged(object sender, EventArgs e)
        {
            if (Input_Left.Text == "关键字搜索" || Input_Left.Text == "")
            {
                foreach (Panel item in Flow_Left_Bottom.Controls)if (item.Visible == false)item.Visible = true;
                return;
            }
            foreach (Panel item in Flow_Left_Bottom.Controls)
            if (!(item.Tag as ShuttleFrameData).Title.Contains(Input_Left.Text)) item.Visible = false;
            ReSet(this.Flow_Right_Bottom);
            ReSet(this.Flow_Left_Bottom);
        }
      

        private void Input_Right_TextChanged(object sender, EventArgs e)
        {
            
            if (Input_Right.Text == "关键字搜索" || Input_Right.Text == "")
            {
                foreach (Panel item in Flow_Right_Bottom.Controls) if (item.Visible == false) item.Visible = true;

                ReSet(this.Flow_Right_Bottom);
                ReSet(this.Flow_Left_Bottom);
                
            }
            foreach (Panel item in Flow_Right_Bottom.Controls)
            if (!(item.Tag as ShuttleFrameData).Title.Contains(Input_Right.Text)) item.Visible = false;

            ReSet(this.Flow_Right_Bottom);
            ReSet(this.Flow_Left_Bottom);
        }
        private void DealAllCheck(FlowLayoutPanel flow, bool ischeck)
        {
            foreach (Panel item in flow.Controls)
            {
                var result = item.Tag as ShuttleFrameData;
                if (result.IsShield == false && result.IsCheck == !ischeck&&item.Visible==true)
                {
                    result.IsCheck = ischeck;
                    if (ischeck)
                    {

                        item.Controls[0].BackColor = pic_CheckBackColor;
                        var pic = item.Controls[0] as PictureBox;
                        pic.Image = pic_Img;
                        pic.SizeMode = PictureBoxSizeMode.CenterImage;

                    }
                    else
                    {
                        item.Controls[0].BackColor = pic_UnCheckBackColor;
                        var pic = item.Controls[0] as PictureBox;
                        pic.Image = null;
                    }
                    item.Tag = result;
                }

            }
        }
        private void DealLeftORRight(Panel panel)
        {
            var data = panel.Tag as ShuttleFrameData;
            if (data.IsShield) return;
            data.IsCheck = !data.IsCheck;
            panel.Tag = data;
            if (data.IsCheck)
            {
                var pic = panel.Controls[0] as PictureBox;
                pic.BackColor = pic_CheckBackColor;
                pic.Image = pic_Img;
                pic.SizeMode = PictureBoxSizeMode.CenterImage;
            }
            else
            {
                var pic = panel.Controls[0] as PictureBox;
                pic.BackColor = pic_UnCheckBackColor;
                pic.Image = null;

            }
        }
    }

    public class ShuttleFrameData
    {
        /// <summary>
        /// Id  可以对应数据库主键,方存储
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 是否选中,用于穿梭框的操作,默认False
        /// </summary>
        public bool IsCheck { get; set; }
        /// <summary>
        /// 是左侧值还是右侧值,这个用于初始化。
        /// </summary>
        
        public bool IsLeftORRight { get; set; }
        /// <summary>
        /// 是否屏蔽某些值
        /// </summary>
        public bool IsShield { get; set; }
    }
 private void Init()
        {
            List<ShuttleFrameData> list = new List<ShuttleFrameData>()
          {
              new ShuttleFrameData(){Id="1",Title="李白",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="杜甫",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="白居易",IsShield=true,IsLeftORRight=true },
               new ShuttleFrameData(){Id="1",Title="李清照",IsShield=false,IsLeftORRight=false },
              new ShuttleFrameData(){Id="1",Title="秦观",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="苏轼",IsShield=false,IsLeftORRight=false },
               new ShuttleFrameData(){Id="1",Title="李煜",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="王勃",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="柳永",IsShield=false,IsLeftORRight=true },
                 new ShuttleFrameData(){Id="1",Title="秦观",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="苏轼",IsShield=false,IsLeftORRight=false },
               new ShuttleFrameData(){Id="1",Title="李煜",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="王勃",IsShield=false,IsLeftORRight=true },
              new ShuttleFrameData(){Id="1",Title="柳永",IsShield=false,IsLeftORRight=true },


          };
            shuttleFrameControl1.SetShuttleFrame(list);

        }

 

 

 

posted @ 2024-07-22 15:20  代码如风~~~  阅读(24)  评论(0编辑  收藏  举报