I have made an drag and drop program where i move a button into a panel, like in this picture below, but i don't know how to verify if panel1 contains button1, because i want to make a program where to match items from column A to column B(match buttons x to panel x and verify if all matches were right: button1 in panel1, button2 in panel2...).
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);
}
}
}
and i have associated with every button and panel an event like in the pictures, and i don't know how to verify if the matches were right button1 in panel1...
If you set the Tag property of your button and your Panel to the same string you can use the following code to verify if the panels and buttons on them match:
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");
}
}
}
}
}
This is how it looks in action:
I left it as an exercise to implement the handling when no or more the one button are dropped on the panel.