Solved

How to select many controls together in VB.Net

Posted on 2004-04-01
18
420 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:binu_star
  • 7
  • 6
18 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10738820
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
 
LVL 1

Author Comment

by:binu_star
ID: 11075159
I haven't recieved any suitable answer in dotnet. So i request you to close the question and refund my points.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 11075519
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
 
LVL 1

Author Comment

by:binu_star
ID: 11117428
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
 
LVL 1

Author Comment

by:binu_star
ID: 11124630
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 11125259
I am worling with this. You may reopen question, however.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 11125910
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 11125923
If you want to continue using EE, try to talk more with experts and not with moderators.
0
 
LVL 1

Author Comment

by:binu_star
ID: 11268488
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
 
LVL 1

Author Comment

by:binu_star
ID: 11268512
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
 
LVL 1

Author Comment

by:binu_star
ID: 11276945
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
 
LVL 1

Author Comment

by:binu_star
ID: 11276958
Hi GhostMod,
    I appreciate your response and look forward to further co-ordination.
Thanks,
 Binu.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 11277290
Do you want this inside of group box or on the whole form, both inside and outside of group box?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now