【问题标题】:Match column A button with column B panel c#将 A 列按钮与 B 列面板匹配 c#
【发布时间】:2018-06-05 04:01:20
【问题描述】:

我制作了一个拖放程序,我将一个按钮移动到面板中,如下图所示,但我不知道如何验证 panel1 是否包含 button1,因为我想制作一个程序来匹配从 A 列到 B 列的项目(将按钮 x 匹配到面板 x 并验证所有匹配项是否正确:面板 1 中的按钮 1,面板 2 中的按钮 2...)。

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 proiect_istorie
{
    public partial class DragAndDrop : Form
    {
        public DragAndDrop()
        {
            InitializeComponent();
        }

        private void panel_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void panel_DragDrop(object sender, DragEventArgs e)
        {
            Button bt = ((Button)e.Data.GetData(typeof(Button)));
            bt.Parent = (Panel)sender;
            bt.Dock = DockStyle.Fill;
            bt.BringToFront();
        }

        private void button_MouseDown(object sender, MouseEventArgs e)
        {
            Button bt = (sender as Button);
            bt.DoDragDrop(sender, DragDropEffects.Move);
        }
    }
}

并且我已经与每个按钮和面板相关联,就像图片中的事件一样,我不知道如何验证匹配项是否是面板 1 中的正确按钮 ...

event on panel

event on button

【问题讨论】:

    标签: c# button match panel


    【解决方案1】:

    如果您将按钮和面板的Tag 属性设置为相同的字符串,您可以使用以下代码来验证面板和它们上的按钮是否匹配:

    private void check_Click(object sender, EventArgs e)
    {
        textbox.Text = "";
    
        // loop over all controls of the Form
        foreach(var ctl in Controls)
        {
            var pnl = ctl as Panel;
            if (pnl != null)
            {
                // loop over the Controls in a Panel
                foreach(var pnlctl in pnl.Controls)
                {
                    // find any buttons
                    var bt = pnlctl as Button;
                    if (bt != null)
                    {
                        // check if the Tag property of the Panel matches that of the Button
                        textbox.AppendText( pnl.Name + " = " + ((bt.Tag ==  pnl.Tag)?"OK": "Not OK") + "\r\n");                
                    }
                }
            }
       }
    }
    

    这就是它的实际效果:

    我把它作为一个练习来实现当面板上没有或更多一个按钮被拖放时的处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多