Click here to Skip to main content
15,793,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've found some code online and it works good (credit to the original author).
But if I use SendMessage to auto scroll flow layout panel while mouse moving, it will not work as expected.

I cannot figure out how to correctly edit SetSelectionRect() using AutoScrollPosition.Y or VerticalScroll.Value.

Any help will be appreciated.

What I have tried:

private Point selectionStart;
        private Point selectionEnd;
        private Rectangle selection;
        private bool mouseDown;

        private void GetSelectedTextBoxes() {

            List<textbox> selected = new List<textbox>();

            foreach (Control c in Controls) {
                if (c is TextBox) {
                    if (selection.IntersectsWith(c.Bounds)) {

            // Replace with your input box
            MessageBox.Show("You selected " + selected.Count + 
                            " textbox controls.");

        protected override void OnMouseDown(MouseEventArgs e) {
            selectionStart = PointToClient(MousePosition);
            mouseDown = true;

        protected override void OnMouseUp(MouseEventArgs e) {
            mouseDown = false;



        protected override void OnMouseMove(MouseEventArgs e) {
            if (!mouseDown) {

            selectionEnd = PointToClient(MousePosition);


        protected override void OnPaint(PaintEventArgs e) {

            if (mouseDown) {
                using (Pen pen = new Pen(Color.Black, 1F)) {
                    pen.DashStyle = DashStyle.Dash;
                    e.Graphics.DrawRectangle(pen, selection);

        private void SetSelectionRect() {
            int x, y;
            int width, height;

            x = selectionStart.X > selectionEnd.X ? 
                selectionEnd.X : selectionStart.X;
            y = selectionStart.Y > selectionEnd.Y ? 
                selectionEnd.Y : selectionStart.Y;

            width = selectionStart.X > selectionEnd.X ? 
                    selectionStart.X - selectionEnd.X : 
                    selectionEnd.X - selectionStart.X;
            height = selectionStart.Y > selectionEnd.Y ? 
                     selectionStart.Y - selectionEnd.Y : 
                     selectionEnd.Y - selectionStart.Y;

            selection = new Rectangle(x, y, width, height);
Updated 2-Oct-23 4:42am
Gerry Schmitz 27-Sep-23 21:06pm    
When it comes to "custom layouts", it seems easier to "tap to select" than using a bounding box. Use a toggle or shift key to select multiple controls vs deselecting on subsequent taps. Each control knows when it's "tapped" via PointerPressed, Click, PreviewMouseDown, etc.. I use a (toggled) colored border or BG to show "selected" on a control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900