Figure - Form showing selected Label control, following selection
Introduction
The PickBox
class provides sizing handles that allow the positioning and sizing on simple controls on a containing form. This C# sample was adapted from an earlier version of the class written in VB (version 6). The sample was prepared using Borland's C# Builder IDE and exported to a VS project.
Using the code
The PickBox
class exposes a "WireControl
" method that attaches events to a passed control, implementing �pickbox� behavior. Clicking on a �wired� control displays eight sizing handles around the perimeter of the control and enables sizing and dragging of the control via mouse event handlers provided by the class instance (see commented code for details). The following snippet illustrates the use of the PickBox class and this function from within the Sample Form:
private PickBox pb = new PickBox();
public WinForm()
{
InitializeComponent();
foreach (Control c in this.Controls) {
pb.WireControl(c);
}
}
The "WireControl
" method attaches a Click
event handler to each passed control. When called the event handler then attaches the "pickbox", made up of eight Label
controls that act as sizing handles, to the clicked control. In addition, mouse event handlers are attached to the control allowing for dragging of the control on its parent form.
private void SelectControl(object sender, EventArgs e) {
if (m_control is Control) {
m_control.Cursor = oldCursor;
m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
m_control.Click -= new EventHandler(this.SelectControl);
m_control = null;
}
m_control = (Control)sender;
m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp);
for (int i = 0; i<8; i++) {
m_control.Parent.Controls.Add(lbl[i]);
lbl[i].BringToFront();
}
MoveHandles();
ShowHandles();
oldCursor = m_control.Cursor;
m_control.Cursor = Cursors.SizeAll;
}
The sizing handles are Label
s that are created, initialized and stored in an array of Label
controls when the instance of the PickBox
class is constructed. MouseDown
, MouseMove
, and MouseUp
events service the array of Label
s during control sizing operations.
Points of Interest
The class sample works well for simple applications, but may exhibit some interaction within applications employing more complicated, time-critical event handling. In it�s current form it provides for the selection of only one control at a time.
The PickBox
sample is a simpler, and probably less versatile C# example of the functionality presented in the C++ sample �A Sizing/Moving widget� by Andrew JM Hall.
History
- This is the initial submission of the sample.