我正在尝试设置在 MouseDown 和 MouseUp 事件之间定义的矩形内的每个 TextBox 的背景颜色,其中 MouseUp 事件发生在 MouseDown 事件的右侧和下方。
我以这种方式捕获积分:
static readonly Color PSEUDO_HIGHLIGHT_COLOR = Color.Gainsboro;
private Point selectionStart;
private Point selectionEnd;
. . .
private void flowLayoutPanelGreatGooglyMoogly_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
selectionStart = PointToClient(MousePosition);
}
}
private void flowLayoutPanelGreatGooglyMoogly_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
selectionEnd = PointToClient(MousePosition);
HighlightAllTextBoxValsBetweenPoints();
}
}
...下面的代码应该将 TextBoxes 的 BackColor 设置在点描述的虚拟矩形“下方”:
private void HighlightAllTextBoxValsBetweenPoints() {
TextBox tb;
foreach (Control ctrl in flowLayoutPanelGreatGooglyMoogly.Controls) {
if (ctrl is TextBox) {
tb = (TextBox)ctrl;
if ((tb.Location.X >= selectionStart.X) &&
(tb.Location.Y >= selectionStart.Y) &&
(tb.Location.X <= selectionEnd.X) &&
(tb.Location.Y >= selectionEnd.Y)) {
tb.BackColor = PSEUDO_HIGHLIGHT_COLOR;
}
}
}
}
...但是,尽管代码正在执行,但没有一个 TextBox 被视为在这些约束范围内。
就好像我在拖着一张好美国的地图,但桥下的巨魔告诉我堪萨斯州真的在加拿大。
我的逻辑可能混淆或倒退(未能正确比较 X 和 Y 坐标),或者我未能将点从相对转换为绝对,反之亦然。
更新:
答案几乎是完美的(约翰怎么知道我的 FlowLayoutPanel 位于它所在的位置);为了让它工作,我只需要在他的代码上方添加这个:
// Have to subtract the Top (Y) value of the Panel
int DeltaFromFormTopToPanelTop = flowLayoutPanelGreatGooglyMoogly.Location.Y;
selectionStart.Y = selectionStart.Y - DeltaFromFormTopToPanelTop;
selectionEnd.Y = selectionEnd.Y - DeltaFromFormTopToPanelTop;
更新到更新
当用户只选择一个控件时,为了防止着色/提示,我也必须添加以下代码:
if (e.Button == MouseButtons.Left) {
selectionEnd = PointToClient(MousePosition);
if (MouseHasNotMovedFar()) {
return;
}
HighlightAllTextBoxValsBetweenPoints();
PromptForAndAssignInputValue();
}
private bool MouseHasNotMovedFar() {
// The "X" or horizontal, is TextBoxWidth*2 + LabelWidth*1
// The "Y" or vertical, is TextBoxHeight*2
// If the user has moved the mouse less than these between
// MouseDown and MouseUp, they probably have not dragged to
// select multiple TextBoxes.
const int ACCEPTABLE_X_DELTA = 74;
const int ACCEPTABLE_Y_DELTA = 40;
return (selectionEnd.X - selectionStart.X) <= ACCEPTABLE_X_DELTA &&
(selectionEnd.Y - selectionStart.Y) <= ACCEPTABLE_Y_DELTA;
}