|
-
Aug 21st, 2006, 02:51 PM
#1
Thread Starter
Lively Member
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?
-
Aug 21st, 2006, 03:02 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 21st, 2006, 03:11 PM
#3
Frenzied Member
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.
-
Aug 24th, 2006, 01:01 PM
#4
Thread Starter
Lively Member
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.
-
Aug 24th, 2006, 01:30 PM
#5
Frenzied Member
Re: Need help selecting textboxes at runtime
Check out the links in my signature for some GDI tutorials.
-
Aug 25th, 2006, 04:38 PM
#6
Thread Starter
Lively Member
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.
-
Aug 25th, 2006, 04:55 PM
#7
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:
Dim X1, X2, Y1, Y2 As Integer
Dim blnDown As Boolean = False
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
blnDown = True
X1 = Windows.Forms.Cursor.Position.X - Me.Left
Y1 = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If blnDown Then
Dim g As Graphics = Me.CreateGraphics
Dim a As New PaintEventArgs(g, New Rectangle(0, 0, Me.Width, Me.Height))
X2 = Windows.Forms.Cursor.Position.X - Me.Left
Y2 = Windows.Forms.Cursor.Position.Y - Me.Top
a.Graphics.Clear(Me.BackColor)
'Im pretty sure this part can be re-written into something nicer looking and more efficient
If (X2 > X1) And (Y2 > Y1) Then
a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X1, Y1, X2 - X1, Y2 - Y1))
ElseIf (X2 > X1) And (Y2 < Y1) Then
a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X1, Y2, X2 - X1, Y1 - Y2))
ElseIf (X2 < X1) And (Y2 < Y1) Then
a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X2, Y2, X1 - X2, Y1 - Y2))
ElseIf (X2 < X1) And (Y2 > Y1) Then
a.Graphics.DrawRectangle(Pens.Black, New Rectangle(X2, Y1, X1 - X2, Y2 - Y1))
End If
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
blnDown = False
Dim g As Graphics = Me.CreateGraphics
Dim a As New PaintEventArgs(g, New Rectangle(0, 0, Me.Width, Me.Height))
a.Graphics.Clear(Me.BackColor)
For Each c As Control In Me.Controls
If TypeOf (c) Is TextBox Then
End If
Next
End Sub
-
Aug 26th, 2006, 06:03 AM
#8
Thread Starter
Lively Member
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.
-
Aug 26th, 2006, 09:07 AM
#9
Re: Need help selecting textboxes at runtime
your welcome
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|