Results 1 to 9 of 9

Thread: Need help selecting textboxes at runtime

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    94

    Need help selecting textboxes at runtime

    I have a field of textboxes. I want the user to be able to select a few of them and to add them to a collection. I prefer a method that allows the user to click-and-hold to select an area but a method that involves clicking on each individual textbox is fine too. Can someone give me an idea how to do this?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help selecting textboxes at runtime

    You could place a small checkbox next to each textbox as a way of selecting which textboxes to include. You can not highlight more then one textbox contents at a time.

    Or double click in each textbox and have the backcolor change as a way of indicating its selected?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Need help selecting textboxes at runtime

    So you want something like in VS where you click an area and then drag to create a rectangle, and anything in that rectangle is selected?

    If so, I would probably create a custom container, that uses gdi to draw the rectangle, and have a property that contains a list of the selected controls.

    So mousedown event you would set the start coordinates for the rectangle, then in the mouse move draw that rectangle out.

    Then on the mouse up, use those coordinates you have to find all the controls that in the area and add them to a property you created for selected Controls.

    If you want to get fancy, then use gdi to draw a little square around each selected control, so the user knows they are selected.

    Just a note, this was off the type of my head, so there are probably a few flaws in my thinking, but it shoudl give you a start, if you are wanting to do, what I think you want to do.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    94

    Re: Need help selecting textboxes at runtime

    Thanks mpdeglau, that was just what I had in mind. But I don't know how to do that. Can you give me a quick starter or a link to a relevant tutorial? I have never used GDI.

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Need help selecting textboxes at runtime

    Check out the links in my signature for some GDI tutorials.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    94

    Re: Need help selecting textboxes at runtime

    Using the tutorials, i am now able to draw rectangles on mousedown and on mouseup. But I cannot even let them be drawn at the cursor position, let alone let the rectangle follow the cursor. All the tutorials seem to focus on simple drawings.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Need help selecting textboxes at runtime

    Im new to GDI aswell and this thing seemed like a good challenge! So I came up with this (Made in 2005):

    VB Code:
    1. Dim X1, X2, Y1, Y2 As Integer
    2.     Dim blnDown As Boolean = False
    3.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    4.         blnDown = True
    5.         X1 = Windows.Forms.Cursor.Position.X - Me.Left
    6.         Y1 = Windows.Forms.Cursor.Position.Y - Me.Top
    7.     End Sub
    8.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    9.         If blnDown Then
    10.             Dim g As Graphics = Me.CreateGraphics
    11.             Dim a As New PaintEventArgs(g, New Rectangle(0, 0, Me.Width, Me.Height))
    12.             X2 = Windows.Forms.Cursor.Position.X - Me.Left
    13.             Y2 = Windows.Forms.Cursor.Position.Y - Me.Top
    14.             a.Graphics.Clear(Me.BackColor)
    15. 'Im pretty sure this part can be re-written into something nicer looking and more efficient
    16.             If (X2 > X1) And (Y2 > Y1) Then
    17.                 a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X1, Y1, X2 - X1, Y2 - Y1))
    18.             ElseIf (X2 > X1) And (Y2 < Y1) Then
    19.                 a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X1, Y2, X2 - X1, Y1 - Y2))
    20.             ElseIf (X2 < X1) And (Y2 < Y1) Then
    21.                 a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X2, Y2, X1 - X2, Y1 - Y2))
    22.             ElseIf (X2 < X1) And (Y2 > Y1) Then
    23.                 a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X2, Y1, X1 - X2, Y2 - Y1))
    24.             End If
    25.  
    26.         End If
    27.     End Sub
    28.  
    29.     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    30.         blnDown = False
    31.         Dim g As Graphics = Me.CreateGraphics
    32.         Dim a As New PaintEventArgs(g, New Rectangle(0, 0, Me.Width, Me.Height))
    33.         a.Graphics.Clear(Me.BackColor)
    34.         For Each c As Control In Me.Controls
    35.             If TypeOf (c) Is TextBox Then
    36.  
    37.             End If
    38.         Next
    39.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    94

    Re: Need help selecting textboxes at runtime

    Thanks Atheist, it works! I would never have found out how it works by myself.

    The only thing I have to do is to correct for the position of the form on the screen. Even with the "- Me.Left" correction, the rectangle is still offset to the lower right corner. Perhaps because I am using a MDI application with a menu. Both the MDI container and the menu offset the form to the lower right corner.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Need help selecting textboxes at runtime

    your welcome
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width