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?
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?
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.
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.
Re: Need help selecting textboxes at runtime
Check out the links in my signature for some GDI tutorials.
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. :(
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
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.
Re: Need help selecting textboxes at runtime