0

我正在尝试设置在 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;
}
4

1 回答 1

2

有几个问题。
首先 - 文本框坐标相对于 flowLayoutPanel。因此,您的 PointToClient 必须相同:

selectionStart = flowLayoutPanelGreatGooglyMoogly.PointToClient(MousePosition);

第二 - 你有一个逻辑错误;最后的比较被颠倒了。(tb.Location.Y >= selectionEnd.Y)) {

简化过程并允许向任何方向拖动的建议是使用矩形:

private void HighlightAllTextBoxValsBetweenPoints()
{
    var selectionBounds = new Rectangle(
        selectionStart.X, 
        selectionStart.Y, 
        selectionEnd.X - selectionStart.X, 
        selectionEnd.Y - selectionStart.Y);
    foreach (Control ctrl in flowLayoutPanel1.Controls)
    {
        var tb = ctrl as TextBox;
        if (tb == null)
            continue;

        if (tb.Bounds.IntersectsWith(selectionBounds))
            tb.BackColor = PSEUDO_HIGHLIGHT_COLOR;
    }
}
于 2012-04-26T23:21:14.850 回答