How to select many controls together in VB.Net

Hi,
  I am creating a windows application and have a groupbox on my form. Few controls are created at runtime and are placed on this groupbox. Now i need to select multiple controls at runtime by just clicking and dragging the mouse as allowed in Windows operating system. As of now, i am doing by clicking on the each controls, but i want to use my mouse to select many controls together by just clicking and dragging the mouse towards the controls that need to be selected. This is very much important for my project and i would like to express my thanks in advance for all the comments.
LVL 1
binu_starAsked:
Who is Participating?
 
AlexFMConnect With a Mentor Commented:
Create new VB.NET Windows Forms application and paste this code to it. See notes in Form1_MouseUp.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
        Me.Text = "Form1"
    End Sub

#End Region

    Dim rectDrawing As Boolean
    Dim startPoint As Point
    Dim endPoint As Point

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rectDrawing = False
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            rectDrawing = True
            Me.Capture = True
            startPoint = New Point(e.X, e.Y)
            endPoint = New Point(e.X, e.Y)
            Invalidate()
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If rectDrawing = True Then
            endPoint = New Point(e.X, e.Y)
            Invalidate()
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        If rectDrawing = True Then
            rectDrawing = False
            Me.Capture = False
            Invalidate()

            ' Replace this code with your code - selecting of controls within rectangle
            ' You have startPoint and endPoint here, this is nough information to select controls\
            ' which intersect with this rectangle
            Me.Text = String.Format("({0}, {1}) - ({2}, {3})", _
                startPoint.X, startPoint.Y, endPoint.X, endPoint.Y)

        End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim xStart As Integer
        Dim yStart As Integer
        Dim width As Integer
        Dim height As Integer

        If rectDrawing = True Then
            If endPoint.X > startPoint.X Then
                xStart = startPoint.X
                width = endPoint.X - startPoint.X
            Else
                xStart = endPoint.X
                width = startPoint.X - endPoint.X
            End If

            If endPoint.Y > startPoint.Y Then
                yStart = startPoint.Y
                height = endPoint.Y - startPoint.Y
            Else
                yStart = endPoint.Y
                height = startPoint.Y - endPoint.Y
            End If

            Dim r As Rectangle = New Rectangle(xStart, yStart, width, height)

            ControlPaint.DrawFocusRectangle(e.Graphics, r, Color.Black, Color.Transparent)
        End If
    End Sub
End Class
0
 
AlexFMCommented:
I made such things on VC++ and have no ready .NET code for this. So, I only can describe the general idea, hope this can help. I will talk about selection of any objects in the window client area (controls, graphic objects etc.).

1) Left mouse button down event. If point doesn't belong to any object, start net selection mode. Set capture to this window, Keep two points: srart of selection rectangle and current point (they are equal in the beginning). Let's call them pStart and pCurrent. Use DrawFocusRect API to draw rectangle (pStart, pCurrent) - it draws point in the beginning.

2) Mouse move event. If we are in net selection mode:
Call DrawFocusRect to draw (pStart, pCurrent). This will remove previous rectangle - calling DrawFocusRect  a second time with the same rectangle removes the rectangle from the screen. Keep current mouse position in pCurrent and call DrawFocusRect again - in the new place.

3) Left mouse button up event. Release capture. If we are in net selection mode:
Call DrawFocusRect to draw (pStart, pCurrent) - remove rectangle.
Keep current mouse position in pCurrent.
Select all objects intersecting with (pStart, pCurrent) rectangle.

If you know VC++, you can read this algorithm in MFC sample DRAWCLI.
0
 
binu_starAuthor Commented:
I haven't recieved any suitable answer in dotnet. So i request you to close the question and refund my points.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
AlexFMCommented:
binu_star, you got algorithm how to do this. Did you try to implement it? Do you have some specific problems? You ask the same question third time and get first reply. However, you don't post any comment, and now you ask to close the question.
0
 
binu_starAuthor Commented:
Hii Alex,
   First of all, i would like to apologise to you for not getting back to these question. I had tried to do with your algorithm, but couldn't use the API due to the arguments to be passed. I would be happy if you can send me the code for that in .Net.
   Actually since i had to go on to further module of my project, i left off this module as of now. But i don't have any problems in giving you the points. I would appreciate if you can send me the code in .net as i am new to .Net.
0
 
binu_starAuthor Commented:
I don't feel this is justified. Neither did i get the correct solution nor did i get my points back. I request you to reopen the question as i need a solution for that.

Binu
0
 
AlexFMCommented:
I am worling with this. You may reopen question, however.
0
 
AlexFMCommented:
If you want to continue using EE, try to talk more with experts and not with moderators.
0
 
binu_starAuthor Commented:
Hi
  I am sorry. I was not in station and couldn't access these comments for last few days. I have got a new job and am moving to Seattle, Washington. Anyway thanks for sending me these valuable comments. I must accept Alex's comments as i think it might work. I couldn't implement as i am no longer working in the project. But i will try to use it in a sample and will send you feedback soon. Thanks a lot.
0
 
binu_starAuthor Commented:
Hii
   I would like to suggest moderators to give the users more time before closing any question. As in my case, i was so busy fot last 2 months, that i could hardly use experts exchange. But that doesn't mean that i am not gonna use it ahead. i have got so many useful comments and wanna get your service ahead too. But try to give more time before closing any question.
0
 
binu_starAuthor Commented:
Hi Alex,
   Thanks for your comment. Its working on the form but i need to draw this rectangle on the groupbox. I tried to change the event handling, but it's not working. Can you please tell me, if you know, what all changes i have to make to display rectangle on groupbox.
0
 
binu_starAuthor Commented:
Hi GhostMod,
    I appreciate your response and look forward to further co-ordination.
Thanks,
 Binu.
0
 
AlexFMCommented:
Do you want this inside of group box or on the whole form, both inside and outside of group box?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.