-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
222 lines (194 loc) · 7.15 KB
/
Form1.cs
File metadata and controls
222 lines (194 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GridDragDropApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeControls();
}
private void InitializeControls()
{
// Assign the MouseDown event handler to each control object (Label, Textbox, etc.)
foreach (Control control in tableLayoutPanel1.Controls)
{
if (control is Button)
{
control.MouseDown += Button_MouseDown;
}
if (control is Label)
{
control.MouseDown += Label_MouseDown;
}
if (control is TextBox)
{
control.MouseDown += TextBox_MouseDown;
}
}
// Assign the DragEnter and DragDrop event handlers to the TableLayoutPanel
tableLayoutPanel1.DragEnter += TableLayoutPanel1_DragEnter;
tableLayoutPanel1.DragDrop += TableLayoutPanel1_DragDrop;
tableLayoutPanel1.AllowDrop = true;
}
private void Button_MouseDown(object sender, MouseEventArgs e)
{
Button button = sender as Button;
if (button != null && e.Button == MouseButtons.Left)
{
button.DoDragDrop(button, DragDropEffects.Move);
}
}
private void Label_MouseDown(object sender, MouseEventArgs e)
{
Label label = sender as Label;
if (label != null && e.Button == MouseButtons.Left)
{
label.DoDragDrop(label, DragDropEffects.Move);
}
}
private void TextBox_MouseDown(object sender, MouseEventArgs e)
{
TextBox textbox = sender as TextBox;
if (textbox != null && e.Button == MouseButtons.Left)
{
textbox.DoDragDrop(textbox, DragDropEffects.Move);
}
}
private void TableLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Button)))
{
e.Effect = DragDropEffects.Move;
}
else if (e.Data.GetDataPresent(typeof(Label)))
{
e.Effect = DragDropEffects.Move;
}
else if (e.Data.GetDataPresent(typeof(TextBox)))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void TableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Button)))
{
Button button = (Button)e.Data.GetData(typeof(Button));
Label label = (Label)e.Data.GetData(typeof(Label));
TextBox textbox = (TextBox)e.Data.GetData(typeof(TextBox));
Point clientPoint = tableLayoutPanel1.PointToClient(new Point(e.X, e.Y));
int row = GetRow(clientPoint.Y);
int column = GetColumn(clientPoint.X);
if (row != -1 && column != -1)
{
tableLayoutPanel1.Controls.Remove(button);
tableLayoutPanel1.Controls.Add(button, column, row);
}
}
if (e.Data.GetDataPresent(typeof(Label)))
{
Label label = (Label)e.Data.GetData(typeof(Label));
Point clientPoint = tableLayoutPanel1.PointToClient(new Point(e.X, e.Y));
int row = GetRow(clientPoint.Y);
int column = GetColumn(clientPoint.X);
if (row != -1 && column != -1)
{
tableLayoutPanel1.Controls.Remove(label);
tableLayoutPanel1.Controls.Add(label, column, row);
}
}
if (e.Data.GetDataPresent(typeof(TextBox)))
{
TextBox textbox = (TextBox)e.Data.GetData(typeof(TextBox));
Point clientPoint = tableLayoutPanel1.PointToClient(new Point(e.X, e.Y));
int row = GetRow(clientPoint.Y);
int column = GetColumn(clientPoint.X);
if (row != -1 && column != -1)
{
tableLayoutPanel1.Controls.Remove(textbox);
tableLayoutPanel1.Controls.Add(textbox, column, row);
}
}
}
private int GetRow(int y)
{
int totalHeight = 0;
for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
{
totalHeight += tableLayoutPanel1.GetRowHeights()[i];
if (y < totalHeight)
{
return i;
}
}
return -1;
}
private int GetColumn(int x)
{
int totalWidth = 0;
for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
{
totalWidth += tableLayoutPanel1.GetColumnWidths()[i];
if (x < totalWidth)
{
return i;
}
}
return -1;
}
private void button4_Click(object sender, EventArgs e)
{
DeactivateDragDrop();
}
private void DeactivateDragDrop()
{
// Detach the DragEnter and DragDrop event handlers from the TableLayoutPanel
tableLayoutPanel1.DragEnter -= TableLayoutPanel1_DragEnter;
tableLayoutPanel1.DragDrop -= TableLayoutPanel1_DragDrop;
tableLayoutPanel1.AllowDrop = false;
// Detach the MouseDown event handlers from the buttons
foreach (Control control in tableLayoutPanel1.Controls)
{
if (control is Button)
{
control.MouseDown -= Button_MouseDown;
control.MouseDown += Label_MouseDown;
control.MouseDown += TextBox_MouseDown;
}
}
}
private void button5_Click(object sender, EventArgs e)
{
// Assign and activate the DragEnter and DragDrop event handlers to the TableLayoutPanel in Designer
tableLayoutPanel1.DragEnter += TableLayoutPanel1_DragEnter;
tableLayoutPanel1.DragDrop += TableLayoutPanel1_DragDrop;
tableLayoutPanel1.AllowDrop = true;
// Attach the MouseDown event handlers from the buttons
foreach (Control control in tableLayoutPanel1.Controls)
{
if (control is Button)
{
control.MouseDown += Button_MouseDown;
}
if (control is Label)
{
control.MouseDown += Label_MouseDown;
}
if (control is TextBox)
{
control.MouseDown += TextBox_MouseDown;
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}